    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 *      http://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 <i>executable</i> 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 <i>non-executable</i> 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 <i>executable</i> filter */
    063    public static final IOFileFilter CAN_EXECUTE = new CanExecuteFileFilter();
    064
    065    /** Singleton instance of not <i>executable</i> 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     * Checks 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     * Checks to see if the file can be executed.
    090     * @param file  the File to check.
    091     *
    092     * @return {@code true} if the file can be executed, otherwise {@code false}.
    093     * @since 2.9.0
    094     */
    095    @Override
    096    public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) {
    097        return toFileVisitResult(file != null && Files.isExecutable(file));
    098    }
    099
    100}