    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 */
    017package org.apache.commons.io.filefilter;
    018
    019import java.io.File;
    020import java.io.Serializable;
    021import java.nio.file.FileVisitResult;
    022import java.nio.file.Files;
    023import java.nio.file.Path;
    024import java.nio.file.attribute.BasicFileAttributes;
    025
    026/**
    027 * This filter accepts {@link File}s that are directories.
    028 * <p>
    029 * For example, here is how to print out a list of the current directory's subdirectories:
    030 * </p>
    031 * <h2>Using Classic IO</h2>
    032 *
    033 * <pre>
    034 * File dir = FileUtils.current();
    035 * String[] files = dir.list(DirectoryFileFilter.INSTANCE);
    036 * for (String file : files) {
    037 *     System.out.println(file);
    038 * }
    039 * </pre>
    040 *
    041 * <h2>Using NIO</h2>
    042 *
    043 * <pre>
    044 * final Path dir = PathUtils.current();
    045 * final AccumulatorPathVisitor visitor = AccumulatorPathVisitor.withLongCounters(DirectoryFileFilter.INSTANCE);
    046 * //
    047 * // Walk one dir
    048 * Files.<b>walkFileTree</b>(dir, Collections.emptySet(), 1, visitor);
    049 * System.out.println(visitor.getPathCounters());
    050 * System.out.println(visitor.getFileList());
    051 * //
    052 * visitor.getPathCounters().reset();
    053 * //
    054 * // Walk dir tree
    055 * Files.<b>walkFileTree</b>(dir, visitor);
    056 * System.out.println(visitor.getPathCounters());
    057 * System.out.println(visitor.getDirList());
    058 * System.out.println(visitor.getFileList());
    059 * </pre>
    060 * <h2>Deprecating Serialization</h2>
    061 * <p>
    062 * <em>Serialization is deprecated and will be removed in 3.0.</em>
    063 * </p>
    064 *
    065 * @since 1.0
    066 * @see FileFilterUtils#directoryFileFilter()
    067 */
    068public class DirectoryFileFilter extends AbstractFileFilter implements Serializable {
    069
    070    /**
    071     * Singleton instance of directory filter.
    072     *
    073     * @since 1.3
    074     */
    075    public static final IOFileFilter DIRECTORY = new DirectoryFileFilter();
    076
    077    /**
    078     * Singleton instance of directory filter. Please use the identical DirectoryFileFilter.DIRECTORY constant. The new
    079     * name is more JDK 1.5 friendly as it doesn't clash with other values when using static imports.
    080     */
    081    public static final IOFileFilter INSTANCE = DIRECTORY;
    082
    083    private static final long serialVersionUID = -5148237843784525732L;
    084
    085    /**
    086     * Restrictive constructor.
    087     */
    088    protected DirectoryFileFilter() {
    089        // empty.
    090    }
    091
    092    /**
    093     * Checks to see if the file is a directory.
    094     *
    095     * @param file the File to check
    096     * @return true if the file is a directory
    097     */
    098    @Override
    099    public boolean accept(final File file) {
    100        return file != null && file.isDirectory();
    101    }
    102
    103    /**
    104     * Checks to see if the file is a directory.
    105     * @param file the File to check
    106     *
    107     * @return true if the file is a directory
    108     * @since 2.9.0
    109     */
    110    @Override
    111    public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) {
    112        return toFileVisitResult(file != null && Files.isDirectory(file));
    113    }
    114
    115}