View Javadoc
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  
19  import java.io.File;
20  import java.io.Serializable;
21  import java.nio.file.FileVisitResult;
22  import java.nio.file.Files;
23  import java.nio.file.Path;
24  import java.nio.file.attribute.BasicFileAttributes;
25  
26  /**
27   * This filter accepts {@link File}s that are hidden.
28   * <p>
29   * Example, showing how to print out a list of the
30   * current directory's <i>hidden</i> files:
31   * </p>
32   * <h2>Using Classic IO</h2>
33   * <pre>
34   * File dir = FileUtils.current();
35   * String[] files = dir.list(HiddenFileFilter.HIDDEN);
36   * for (String file : files) {
37   *     System.out.println(file);
38   * }
39   * </pre>
40   *
41   * <p>
42   * Example, showing how to print out a list of the
43   * current directory's <i>visible</i> (i.e. not hidden) files:
44   * </p>
45   *
46   * <pre>
47   * File dir = FileUtils.current();
48   * String[] files = dir.list(HiddenFileFilter.VISIBLE);
49   * for (String file : files) {
50   *     System.out.println(file);
51   * }
52   * </pre>
53   *
54   * <h2>Using NIO</h2>
55   * <pre>
56   * final Path dir = PathUtils.current();
57   * final AccumulatorPathVisitor visitor = AccumulatorPathVisitor.withLongCounters(HiddenFileFilter.HIDDEN);
58   * //
59   * // Walk one dir
60   * Files.<b>walkFileTree</b>(dir, Collections.emptySet(), 1, visitor);
61   * System.out.println(visitor.getPathCounters());
62   * System.out.println(visitor.getFileList());
63   * //
64   * visitor.getPathCounters().reset();
65   * //
66   * // Walk dir tree
67   * Files.<b>walkFileTree</b>(dir, visitor);
68   * System.out.println(visitor.getPathCounters());
69   * System.out.println(visitor.getDirList());
70   * System.out.println(visitor.getFileList());
71   * </pre>
72   * <h2>Deprecating Serialization</h2>
73   * <p>
74   * <em>Serialization is deprecated and will be removed in 3.0.</em>
75   * </p>
76   *
77   * @since 1.3
78   */
79  public class HiddenFileFilter extends AbstractFileFilter implements Serializable {
80  
81      /** Singleton instance of <i>hidden</i> filter */
82      public static final IOFileFilter HIDDEN  = new HiddenFileFilter();
83  
84      private static final long serialVersionUID = 8930842316112759062L;
85  
86      /** Singleton instance of <i>visible</i> filter */
87      public static final IOFileFilter VISIBLE = HIDDEN.negate();
88  
89      /**
90       * Restrictive constructor.
91       */
92      protected HiddenFileFilter() {
93      }
94  
95      /**
96       * Checks to see if the file is hidden.
97       *
98       * @param file  the File to check
99       * @return {@code true} if the file is
100      *  <i>hidden</i>, otherwise {@code false}.
101      */
102     @Override
103     public boolean accept(final File file) {
104         return file == null || file.isHidden();
105     }
106 
107     /**
108      * Checks to see if the file is hidden.
109      * @param file  the File to check
110      *
111      * @return {@code true} if the file is
112      *  <i>hidden</i>, otherwise {@code false}.
113      * @since 2.9.0
114      */
115     @Override
116     public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) {
117         return get(() -> toFileVisitResult(file == null || Files.isHidden(file)));
118     }
119 
120 }