EmptyFileFilter.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. import java.util.stream.Stream;

  25. import org.apache.commons.io.IOUtils;

  26. /**
  27.  * This filter accepts files or directories that are empty.
  28.  * <p>
  29.  * If the {@link File} is a directory it checks that it contains no files.
  30.  * </p>
  31.  * <p>
  32.  * Example, showing how to print out a list of the current directory's empty files/directories:
  33.  * </p>
  34.  * <h2>Using Classic IO</h2>
  35.  * <pre>
  36.  * File dir = FileUtils.current();
  37.  * String[] files = dir.list(EmptyFileFilter.EMPTY);
  38.  * for (String file : files) {
  39.  *     System.out.println(file);
  40.  * }
  41.  * </pre>
  42.  *
  43.  * <p>
  44.  * Example, showing how to print out a list of the current directory's non-empty files/directories:
  45.  * </p>
  46.  *
  47.  * <pre>
  48.  * File dir = FileUtils.current();
  49.  * String[] files = dir.list(EmptyFileFilter.NOT_EMPTY);
  50.  * for (String file : files) {
  51.  *     System.out.println(file);
  52.  * }
  53.  * </pre>
  54.  *
  55.  * <h2>Using NIO</h2>
  56.  * <pre>
  57.  * final Path dir = PathUtils.current();
  58.  * final AccumulatorPathVisitor visitor = AccumulatorPathVisitor.withLongCounters(EmptyFileFilter.EMPTY);
  59.  * //
  60.  * // Walk one directory
  61.  * Files.<strong>walkFileTree</strong>(dir, Collections.emptySet(), 1, visitor);
  62.  * System.out.println(visitor.getPathCounters());
  63.  * System.out.println(visitor.getFileList());
  64.  * //
  65.  * visitor.getPathCounters().reset();
  66.  * //
  67.  * // Walk directory tree
  68.  * Files.<strong>walkFileTree</strong>(dir, visitor);
  69.  * System.out.println(visitor.getPathCounters());
  70.  * System.out.println(visitor.getDirList());
  71.  * System.out.println(visitor.getFileList());
  72.  * </pre>
  73.  * <h2>Deprecating Serialization</h2>
  74.  * <p>
  75.  * <em>Serialization is deprecated and will be removed in 3.0.</em>
  76.  * </p>
  77.  *
  78.  * @since 1.3
  79.  */
  80. public class EmptyFileFilter extends AbstractFileFilter implements Serializable {

  81.     /** Singleton instance of <em>empty</em> filter */
  82.     public static final IOFileFilter EMPTY = new EmptyFileFilter();

  83.     /** Singleton instance of <em>not-empty</em> filter */
  84.     public static final IOFileFilter NOT_EMPTY = EMPTY.negate();

  85.     private static final long serialVersionUID = 3631422087512832211L;

  86.     /**
  87.      * Restrictive constructor.
  88.      */
  89.     protected EmptyFileFilter() {
  90.     }

  91.     /**
  92.      * Checks to see if the file is empty.
  93.      *
  94.      * @param file the file or directory to check
  95.      * @return {@code true} if the file or directory is <em>empty</em>, otherwise {@code false}.
  96.      */
  97.     @Override
  98.     public boolean accept(final File file) {
  99.         if (file == null) {
  100.             return true;
  101.         }
  102.         if (file.isDirectory()) {
  103.             final File[] files = file.listFiles();
  104.             return IOUtils.length(files) == 0;
  105.         }
  106.         return file.length() == 0;
  107.     }

  108.     /**
  109.      * Checks to see if the file is empty.
  110.      *
  111.      * @param file the file or directory to check
  112.      * @param attributes the path's basic attributes (may be null).
  113.      * @return {@code true} if the file or directory is <em>empty</em>, otherwise {@code false}.
  114.      * @since 2.9.0
  115.      */
  116.     @Override
  117.     public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) {
  118.         if (file == null) {
  119.             return toFileVisitResult(true);
  120.         }
  121.         return get(() -> {
  122.             if (Files.isDirectory(file)) {
  123.                 try (Stream<Path> stream = Files.list(file)) {
  124.                     return toFileVisitResult(!stream.findFirst().isPresent());
  125.                 }
  126.             }
  127.             return toFileVisitResult(Files.size(file) == 0);
  128.         });
  129.     }

  130. }