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

  66.     /**
  67.      * Singleton instance of file filter.
  68.      *
  69.      * @since 2.9.0
  70.      */
  71.     public static final IOFileFilter INSTANCE = new FileFileFilter();

  72.     /**
  73.      * Singleton instance of file filter.
  74.      *
  75.      * @deprecated Use {@link #INSTANCE}.
  76.      */
  77.     @Deprecated
  78.     public static final IOFileFilter FILE = INSTANCE;

  79.     private static final long serialVersionUID = 5345244090827540862L;

  80.     /**
  81.      * Restrictive constructor.
  82.      */
  83.     protected FileFileFilter() {
  84.     }

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

  95.     /**
  96.      * Checks to see if the file is a file.
  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 file
  101.      * @since 2.9.0
  102.      */
  103.     @Override
  104.     public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) {
  105.         return toFileVisitResult(file != null && Files.isRegularFile(file));
  106.     }

  107. }