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.vfs2.filter;
18
19 import java.io.Serializable;
20
21 import org.apache.commons.vfs2.FileFilter;
22 import org.apache.commons.vfs2.FileSelectInfo;
23 import org.apache.commons.vfs2.FileSystemException;
24
25 /**
26 * This filter accepts {@code File}s that can be executed.
27 * <p>
28 * Example, showing how to print out a list of the current directory's
29 * <em>executable</em> files:
30 * </p>
31 *
32 * <pre>
33 * FileSystemManager fsManager = VFS.getManager();
34 * FileObject dir = fsManager.toFileObject(new File("."));
35 * FileObject[] files = dir.findFiles(new FileFilterSelector(CanReadFileFilter.CAN_EXECUTE));
36 * for (int i = 0; i < files.length; i++) {
37 * System.out.println(files[i]);
38 * }
39 * </pre>
40 *
41 * <p>
42 * Example, showing how to print out a list of the current directory's
43 * <em>un-executable</em> files:
44 * </p>
45 *
46 * <pre>
47 * FileSystemManager fsManager = VFS.getManager();
48 * FileObject dir = fsManager.toFileObject(new File("."));
49 * FileObject[] files = dir.findFiles(new FileFilterSelector(CanReadFileFilter.CANNOT_EXECUTE));
50 * for (int i = 0; i < files.length; i++) {
51 * System.out.println(files[i]);
52 * }
53 * </pre>
54 *
55 * @see "https://commons.apache.org/proper/commons-io/"
56 * @since 2.4
57 */
58 public class CanExecuteFileFilter implements FileFilter, Serializable {
59
60 /** Singleton instance of <em>executed</em> filter. */
61 public static final FileFilter CAN_EXECUTE = new CanExecuteFileFilter();
62
63 /** Singleton instance of not <em>executed</em> filter. */
64 public static final FileFilter CANNOT_EXECUTE = new NotFileFilter(CAN_EXECUTE);
65
66 private static final long serialVersionUID = 1L;
67
68 /**
69 * Restrictive constructor.
70 */
71 protected CanExecuteFileFilter() {
72 }
73
74 /**
75 * Checks to see if the file can be executed.
76 *
77 * @param fileSelectInfo the File to check.
78 * @return {@code true} if the file can be executed, otherwise {@code false}.
79 * @throws FileSystemException Thrown for file system errors.
80 */
81 @Override
82 public boolean accept(final FileSelectInfo fileSelectInfo) throws FileSystemException {
83 return fileSelectInfo.getFile().isExecutable();
84 }
85
86 }