DirectoryFileFilter.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.io.filefilter;

  18. import java.io.File;
  19. import java.io.Serializable;
  20. import java.nio.file.FileVisitResult;
  21. import java.nio.file.Files;
  22. import java.nio.file.Path;
  23. import java.nio.file.attribute.BasicFileAttributes;

  24. /**
  25.  * This filter accepts {@link File}s that are directories.
  26.  * <p>
  27.  * For example, here is how to print out a list of the current directory's subdirectories:
  28.  * </p>
  29.  * <h2>Using Classic IO</h2>
  30.  *
  31.  * <pre>
  32.  * File dir = FileUtils.current();
  33.  * String[] files = dir.list(DirectoryFileFilter.INSTANCE);
  34.  * for (String file : files) {
  35.  *     System.out.println(file);
  36.  * }
  37.  * </pre>
  38.  *
  39.  * <h2>Using NIO</h2>
  40.  *
  41.  * <pre>
  42.  * final Path dir = PathUtils.current();
  43.  * final AccumulatorPathVisitor visitor = AccumulatorPathVisitor.withLongCounters(DirectoryFileFilter.INSTANCE);
  44.  * //
  45.  * // Walk one directory
  46.  * Files.<strong>walkFileTree</strong>(dir, Collections.emptySet(), 1, visitor);
  47.  * System.out.println(visitor.getPathCounters());
  48.  * System.out.println(visitor.getFileList());
  49.  * //
  50.  * visitor.getPathCounters().reset();
  51.  * //
  52.  * // Walk directory tree
  53.  * Files.<strong>walkFileTree</strong>(dir, visitor);
  54.  * System.out.println(visitor.getPathCounters());
  55.  * System.out.println(visitor.getDirList());
  56.  * System.out.println(visitor.getFileList());
  57.  * </pre>
  58.  * <h2>Deprecating Serialization</h2>
  59.  * <p>
  60.  * <em>Serialization is deprecated and will be removed in 3.0.</em>
  61.  * </p>
  62.  *
  63.  * @since 1.0
  64.  * @see FileFilterUtils#directoryFileFilter()
  65.  */
  66. public class DirectoryFileFilter extends AbstractFileFilter implements Serializable {

  67.     /**
  68.      * Singleton instance of directory filter.
  69.      *
  70.      * @since 1.3
  71.      */
  72.     public static final IOFileFilter DIRECTORY = new DirectoryFileFilter();

  73.     /**
  74.      * Singleton instance of directory filter. Please use the identical DirectoryFileFilter.DIRECTORY constant. The new
  75.      * name is more JDK 1.5 friendly as it doesn't clash with other values when using static imports.
  76.      */
  77.     public static final IOFileFilter INSTANCE = DIRECTORY;

  78.     private static final long serialVersionUID = -5148237843784525732L;

  79.     /**
  80.      * Restrictive constructor.
  81.      */
  82.     protected DirectoryFileFilter() {
  83.         // empty.
  84.     }

  85.     /**
  86.      * Checks to see if the file is a directory.
  87.      *
  88.      * @param file the File to check
  89.      * @return true if the file is a directory
  90.      */
  91.     @Override
  92.     public boolean accept(final File file) {
  93.         return file != null && file.isDirectory();
  94.     }

  95.     /**
  96.      * Checks to see if the file is a directory.
  97.      *
  98.      * @param file the File to check
  99.      * @param attributes the path's basic attributes (may be null).
  100.      * @return true if the file is a directory
  101.      * @since 2.9.0
  102.      */
  103.     @Override
  104.     public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) {
  105.         return toFileVisitResult(file != null && Files.isDirectory(file));
  106.     }

  107. }