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 can be executed.
28 * <p>
29 * Example, showing how to print out a list of the
30 * current directory's <em>executable</em> files:
31 * </p>
32 * <h2>Using Classic IO</h2>
33 * <pre>
34 * File dir = FileUtils.current();
35 * String[] files = dir.list(CanExecuteFileFilter.CAN_EXECUTE);
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 <em>non-executable</em> files:
44 * </p>
45 *
46 * <pre>
47 * File dir = FileUtils.current();
48 * String[] files = dir.list(CanExecuteFileFilter.CANNOT_EXECUTE);
49 * for (int i = 0; i < files.length; i++) {
50 * System.out.println(files[i]);
51 * }
52 * </pre>
53 * <h2>Deprecating Serialization</h2>
54 * <p>
55 * <em>Serialization is deprecated and will be removed in 3.0.</em>
56 * </p>
57 *
58 * @since 2.7
59 */
60 public class CanExecuteFileFilter extends AbstractFileFilter implements Serializable {
61
62 /** Singleton instance of <em>executable</em> filter */
63 public static final IOFileFilter CAN_EXECUTE = new CanExecuteFileFilter();
64
65 /** Singleton instance of not <em>executable</em> filter */
66 public static final IOFileFilter CANNOT_EXECUTE = CAN_EXECUTE.negate();
67
68 private static final long serialVersionUID = 3179904805251622989L;
69
70 /**
71 * Restrictive constructor.
72 */
73 protected CanExecuteFileFilter() {
74 // empty.
75 }
76
77 /**
78 * Tests to see if the file can be executed.
79 *
80 * @param file the File to check.
81 * @return {@code true} if the file can be executed, otherwise {@code false}.
82 */
83 @Override
84 public boolean accept(final File file) {
85 return file != null && file.canExecute();
86 }
87
88 /**
89 * Tests to see if the file can be executed.
90 *
91 * @param file the File to check.
92 * @param attributes the path's basic attributes (may be null).
93 * @return {@code true} if the file can be executed, otherwise {@code false}.
94 * @since 2.9.0
95 */
96 @Override
97 public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) {
98 return toFileVisitResult(file != null && Files.isExecutable(file));
99 }
100
101 }