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 * https://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 directories.
28 * <p>
29 * For example, here is how to print out a list of the current directory's subdirectories:
30 * </p>
31 * <h2>Using Classic IO</h2>
32 *
33 * <pre>
34 * File dir = FileUtils.current();
35 * String[] files = dir.list(DirectoryFileFilter.INSTANCE);
36 * for (String file : files) {
37 * System.out.println(file);
38 * }
39 * </pre>
40 *
41 * <h2>Using NIO</h2>
42 *
43 * <pre>
44 * final Path dir = PathUtils.current();
45 * final AccumulatorPathVisitor visitor = AccumulatorPathVisitor.withLongCounters(DirectoryFileFilter.INSTANCE);
46 * //
47 * // Walk one directory
48 * Files.<strong>walkFileTree</strong>(dir, Collections.emptySet(), 1, visitor);
49 * System.out.println(visitor.getPathCounters());
50 * System.out.println(visitor.getFileList());
51 * //
52 * visitor.getPathCounters().reset();
53 * //
54 * // Walk directory tree
55 * Files.<strong>walkFileTree</strong>(dir, visitor);
56 * System.out.println(visitor.getPathCounters());
57 * System.out.println(visitor.getDirList());
58 * System.out.println(visitor.getFileList());
59 * </pre>
60 * <h2>Deprecating Serialization</h2>
61 * <p>
62 * <em>Serialization is deprecated and will be removed in 3.0.</em>
63 * </p>
64 *
65 * @since 1.0
66 * @see FileFilterUtils#directoryFileFilter()
67 */
68 public class DirectoryFileFilter extends AbstractFileFilter implements Serializable {
69
70 /**
71 * Singleton instance of directory filter.
72 *
73 * @since 1.3
74 */
75 public static final IOFileFilter DIRECTORY = new DirectoryFileFilter();
76
77 /**
78 * Singleton instance of directory filter. Please use the identical DirectoryFileFilter.DIRECTORY constant. The new
79 * name is more JDK 1.5 friendly as it doesn't clash with other values when using static imports.
80 */
81 public static final IOFileFilter INSTANCE = DIRECTORY;
82
83 private static final long serialVersionUID = -5148237843784525732L;
84
85 /**
86 * Restrictive constructor.
87 */
88 protected DirectoryFileFilter() {
89 // empty.
90 }
91
92 /**
93 * Tests to see if the file is a directory.
94 *
95 * @param file the File to check
96 * @return true if the file is a directory
97 */
98 @Override
99 public boolean accept(final File file) {
100 return isDirectory(file);
101 }
102
103 /**
104 * Tests to see if the file is a directory.
105 *
106 * @param file the File to check
107 * @param attributes the path's basic attributes (may be null).
108 * @return true if the file is a directory
109 * @since 2.9.0
110 */
111 @Override
112 public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) {
113 return toFileVisitResult(file != null && Files.isDirectory(file));
114 }
115
116 }