    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      https://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.io.filefilter;
    018
    019import java.io.File;
    020import java.io.Serializable;
    021import java.nio.file.FileVisitResult;
    022import java.nio.file.Files;
    023import java.nio.file.Path;
    024import java.nio.file.attribute.BasicFileAttributes;
    025
    026/**
    027 * This filter accepts {@link File}s that can be executed.
    028 * <p>
    029 * Example, showing how to print out a list of the
    030 * current directory's <em>executable</em> files:
    031 * </p>
    032 * <h2>Using Classic IO</h2>
    033 * <pre>
    034 * File dir = FileUtils.current();
    035 * String[] files = dir.list(CanExecuteFileFilter.CAN_EXECUTE);
    036 * for (String file : files) {
    037 *     System.out.println(file);
    038 * }
    039 * </pre>
    040 *
    041 * <p>
    042 * Example, showing how to print out a list of the
    043 * current directory's <em>non-executable</em> files:
    044 * </p>
    045 *
    046 * <pre>
    047 * File dir = FileUtils.current();
    048 * String[] files = dir.list(CanExecuteFileFilter.CANNOT_EXECUTE);
    049 * for (int i = 0; i &lt; files.length; i++) {
    050 *     System.out.println(files[i]);
    051 * }
    052 * </pre>
    053 * <h2>Deprecating Serialization</h2>
    054 * <p>
    055 * <em>Serialization is deprecated and will be removed in 3.0.</em>
    056 * </p>
    057 *
    058 * @since 2.7
    059 */
    060public class CanExecuteFileFilter extends AbstractFileFilter implements Serializable {
    061
    062    /** Singleton instance of <em>executable</em> filter */
    063    public static final IOFileFilter CAN_EXECUTE = new CanExecuteFileFilter();
    064
    065    /** Singleton instance of not <em>executable</em> filter */
    066    public static final IOFileFilter CANNOT_EXECUTE = CAN_EXECUTE.negate();
    067
    068    private static final long serialVersionUID = 3179904805251622989L;
    069
    070    /**
    071     * Restrictive constructor.
    072     */
    073    protected CanExecuteFileFilter() {
    074        // empty.
    075    }
    076
    077    /**
    078     * Tests to see if the file can be executed.
    079     *
    080     * @param file  the File to check.
    081     * @return {@code true} if the file can be executed, otherwise {@code false}.
    082     */
    083    @Override
    084    public boolean accept(final File file) {
    085        return file != null && file.canExecute();
    086    }
    087
    088    /**
    089     * Tests to see if the file can be executed.
    090     *
    091     * @param file  the File to check.
    092     * @param attributes the path's basic attributes (may be null).
    093     * @return {@code true} if the file can be executed, otherwise {@code false}.
    094     * @since 2.9.0
    095     */
    096    @Override
    097    public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) {
    098        return toFileVisitResult(file != null && Files.isExecutable(file));
    099    }
    100
    101}