    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017
    018package org.apache.commons.io.file;
    019
    020import java.io.IOException;
    021import java.nio.file.FileVisitResult;
    022import java.nio.file.Files;
    023import java.nio.file.Path;
    024import java.nio.file.attribute.BasicFileAttributes;
    025import java.util.Objects;
    026
    027import org.apache.commons.io.file.Counters.PathCounters;
    028
    029/**
    030 * Counts files, directories, and sizes, as a visit proceeds.
    031 *
    032 * @since 2.7
    033 */
    034public class CountingPathVisitor extends SimplePathVisitor {
    035
    036    static final String[] EMPTY_STRING_ARRAY = new String[0];
    037
    038    /**
    039     * Creates a new instance configured with a BigInteger {@link PathCounters}.
    040     *
    041     * @return a new instance configured with a BigInteger {@link PathCounters}.
    042     */
    043    public static CountingPathVisitor withBigIntegerCounters() {
    044        return new CountingPathVisitor(Counters.bigIntegerPathCounters());
    045    }
    046
    047    /**
    048     * Creates a new instance configured with a long {@link PathCounters}.
    049     *
    050     * @return a new instance configured with a long {@link PathCounters}.
    051     */
    052    public static CountingPathVisitor withLongCounters() {
    053        return new CountingPathVisitor(Counters.longPathCounters());
    054    }
    055
    056    private final PathCounters pathCounters;
    057
    058    /**
    059     * Constructs a new instance.
    060     *
    061     * @param pathCounter How to count path visits.
    062     */
    063    public CountingPathVisitor(final PathCounters pathCounter) {
    064        super();
    065        this.pathCounters = Objects.requireNonNull(pathCounter, "pathCounter");
    066    }
    067
    068    @Override
    069    public boolean equals(Object obj) {
    070        if (this == obj) {
    071            return true;
    072        }
    073        if (!(obj instanceof CountingPathVisitor)) {
    074            return false;
    075        }
    076        CountingPathVisitor other = (CountingPathVisitor) obj;
    077        return Objects.equals(pathCounters, other.pathCounters);
    078    }
    079
    080    /**
    081     * Gets the visitation counts.
    082     *
    083     * @return the visitation counts.
    084     */
    085    public PathCounters getPathCounters() {
    086        return pathCounters;
    087    }
    088
    089    @Override
    090    public int hashCode() {
    091        return Objects.hash(pathCounters);
    092    }
    093
    094    @Override
    095    public FileVisitResult postVisitDirectory(final Path dir, final IOException exc) throws IOException {
    096        pathCounters.getDirectoryCounter().increment();
    097        return FileVisitResult.CONTINUE;
    098    }
    099
    100    @Override
    101    public String toString() {
    102        return pathCounters.toString();
    103    }
    104
    105    /**
    106     * Updates the counters for visiting the given file.
    107     *
    108     * @param file the visited file.
    109     * @param attributes the visited file attributes.
    110     */
    111    protected void updateFileCounters(final Path file, final BasicFileAttributes attributes) {
    112        pathCounters.getFileCounter().increment();
    113        pathCounters.getByteCounter().add(attributes.size());
    114    }
    115
    116    @Override
    117    public FileVisitResult visitFile(final Path file, final BasicFileAttributes attributes) throws IOException {
    118        if (Files.exists(file)) {
    119            updateFileCounters(file, attributes);
    120        }
    121        return FileVisitResult.CONTINUE;
    122    }
    123
    124}