    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 {@code 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 = new File(".");
    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 = new File(".");
    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 *
    054 * @since 2.7
    055 */
    056public class CanExecuteFileFilter extends AbstractFileFilter implements Serializable {
    057
    058    /** Singleton instance of <i>executable</i> filter */
    059    public static final IOFileFilter CAN_EXECUTE = new CanExecuteFileFilter();
    060
    061    /** Singleton instance of not <i>executable</i> filter */
    062    public static final IOFileFilter CANNOT_EXECUTE = CAN_EXECUTE.negate();
    063
    064    private static final long serialVersionUID = 3179904805251622989L;
    065
    066    /**
    067     * Restrictive constructor.
    068     */
    069    protected CanExecuteFileFilter() {
    070        // empty.
    071    }
    072
    073    /**
    074     * Checks to see if the file can be executed.
    075     *
    076     * @param file  the File to check.
    077     * @return {@code true} if the file can be executed, otherwise {@code false}.
    078     */
    079    @Override
    080    public boolean accept(final File file) {
    081        return file.canExecute();
    082    }
    083
    084    /**
    085     * Checks to see if the file can be executed.
    086     * @param file  the File to check.
    087     *
    088     * @return {@code true} if the file can be executed, otherwise {@code false}.
    089     * @since 2.9.0
    090     */
    091    @Override
    092    public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) {
    093        return toFileVisitResult(Files.isExecutable(file), file);
    094    }
    095
    096}