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 can be executed. 28 * <p> 29 * Example, showing how to print out a list of the 30 * current directory's <i>executable</i> 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 <i>non-executable</i> 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 <i>executable</i> filter */ 63 public static final IOFileFilter CAN_EXECUTE = new CanExecuteFileFilter(); 64 65 /** Singleton instance of not <i>executable</i> 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 * Checks 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 * Checks to see if the file can be executed. 90 * @param file the File to check. 91 * 92 * @return {@code true} if the file can be executed, otherwise {@code false}. 93 * @since 2.9.0 94 */ 95 @Override 96 public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) { 97 return toFileVisitResult(file != null && Files.isExecutable(file)); 98 } 99 100 }