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

  78.     /** Singleton instance of <em>hidden</em> filter */
  79.     public static final IOFileFilter HIDDEN  = new HiddenFileFilter();

  80.     private static final long serialVersionUID = 8930842316112759062L;

  81.     /** Singleton instance of <em>visible</em> filter */
  82.     public static final IOFileFilter VISIBLE = HIDDEN.negate();

  83.     /**
  84.      * Restrictive constructor.
  85.      */
  86.     protected HiddenFileFilter() {
  87.     }

  88.     /**
  89.      * Checks to see if the file is hidden.
  90.      *
  91.      * @param file  the File to check
  92.      * @return {@code true} if the file is
  93.      *  <em>hidden</em>, otherwise {@code false}.
  94.      */
  95.     @Override
  96.     public boolean accept(final File file) {
  97.         return file == null || file.isHidden();
  98.     }

  99.     /**
  100.      * Checks to see if the file is hidden.
  101.      *
  102.      * @param file       the File to check
  103.      * @param attributes the path's basic attributes (may be null).
  104.      * @return {@code true} if the file is <em>hidden</em>, otherwise {@code false}.
  105.      * @since 2.9.0
  106.      */
  107.     @Override
  108.     public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) {
  109.         return get(() -> toFileVisitResult(file == null || Files.isHidden(file)));
  110.     }

  111. }