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 {@code 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 = new File(".");
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 = Paths.get("");
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 *
061 * @since 1.0
062 * @see FileFilterUtils#directoryFileFilter()
063 */
064public class DirectoryFileFilter extends AbstractFileFilter implements Serializable {
065
066    /**
067     * Singleton instance of directory filter.
068     *
069     * @since 1.3
070     */
071    public static final IOFileFilter DIRECTORY = new DirectoryFileFilter();
072
073    /**
074     * Singleton instance of directory filter. Please use the identical DirectoryFileFilter.DIRECTORY constant. The new
075     * name is more JDK 1.5 friendly as it doesn't clash with other values when using static imports.
076     */
077    public static final IOFileFilter INSTANCE = DIRECTORY;
078
079    private static final long serialVersionUID = -5148237843784525732L;
080
081    /**
082     * Restrictive constructor.
083     */
084    protected DirectoryFileFilter() {
085        // empty.
086    }
087
088    /**
089     * Checks to see if the file is a directory.
090     *
091     * @param file the File to check
092     * @return true if the file is a directory
093     */
094    @Override
095    public boolean accept(final File file) {
096        return file.isDirectory();
097    }
098
099    /**
100     * Checks to see if the file is a directory.
101     * @param file the File to check
102     *
103     * @return true if the file is a directory
104     * @since 2.9.0
105     */
106    @Override
107    public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) {
108        return toFileVisitResult(Files.isDirectory(file), file);
109    }
110
111}