CanExecuteFileFilter.java

  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. import java.io.File;
  19. import java.io.Serializable;
  20. import java.nio.file.FileVisitResult;
  21. import java.nio.file.Files;
  22. import java.nio.file.Path;
  23. import java.nio.file.attribute.BasicFileAttributes;

  24. /**
  25.  * This filter accepts {@link File}s that can be executed.
  26.  * <p>
  27.  * Example, showing how to print out a list of the
  28.  * current directory's <em>executable</em> files:
  29.  * </p>
  30.  * <h2>Using Classic IO</h2>
  31.  * <pre>
  32.  * File dir = FileUtils.current();
  33.  * String[] files = dir.list(CanExecuteFileFilter.CAN_EXECUTE);
  34.  * for (String file : files) {
  35.  *     System.out.println(file);
  36.  * }
  37.  * </pre>
  38.  *
  39.  * <p>
  40.  * Example, showing how to print out a list of the
  41.  * current directory's <em>non-executable</em> files:
  42.  * </p>
  43.  *
  44.  * <pre>
  45.  * File dir = FileUtils.current();
  46.  * String[] files = dir.list(CanExecuteFileFilter.CANNOT_EXECUTE);
  47.  * for (int i = 0; i &lt; files.length; i++) {
  48.  *     System.out.println(files[i]);
  49.  * }
  50.  * </pre>
  51.  * <h2>Deprecating Serialization</h2>
  52.  * <p>
  53.  * <em>Serialization is deprecated and will be removed in 3.0.</em>
  54.  * </p>
  55.  *
  56.  * @since 2.7
  57.  */
  58. public class CanExecuteFileFilter extends AbstractFileFilter implements Serializable {

  59.     /** Singleton instance of <em>executable</em> filter */
  60.     public static final IOFileFilter CAN_EXECUTE = new CanExecuteFileFilter();

  61.     /** Singleton instance of not <em>executable</em> filter */
  62.     public static final IOFileFilter CANNOT_EXECUTE = CAN_EXECUTE.negate();

  63.     private static final long serialVersionUID = 3179904805251622989L;

  64.     /**
  65.      * Restrictive constructor.
  66.      */
  67.     protected CanExecuteFileFilter() {
  68.         // empty.
  69.     }

  70.     /**
  71.      * Checks to see if the file can be executed.
  72.      *
  73.      * @param file  the File to check.
  74.      * @return {@code true} if the file can be executed, otherwise {@code false}.
  75.      */
  76.     @Override
  77.     public boolean accept(final File file) {
  78.         return file != null && file.canExecute();
  79.     }

  80.     /**
  81.      * Checks to see if the file can be executed.
  82.      *
  83.      * @param file  the File to check.
  84.      * @param attributes the path's basic attributes (may be null).
  85.      * @return {@code true} if the file can be executed, otherwise {@code false}.
  86.      * @since 2.9.0
  87.      */
  88.     @Override
  89.     public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) {
  90.         return toFileVisitResult(file != null && Files.isExecutable(file));
  91.     }

  92. }