NameFileFilter.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.Path;
  22. import java.nio.file.attribute.BasicFileAttributes;
  23. import java.util.List;
  24. import java.util.Objects;
  25. import java.util.stream.Stream;

  26. import org.apache.commons.io.IOCase;
  27. import org.apache.commons.io.file.PathUtils;

  28. /**
  29.  * Filters file names for a certain name.
  30.  * <p>
  31.  * For example, to print all files and directories in the
  32.  * current directory whose name is {@code Test}:
  33.  * </p>
  34.  * <h2>Using Classic IO</h2>
  35.  * <pre>
  36.  * File dir = FileUtils.current();
  37.  * String[] files = dir.list(new NameFileFilter("Test"));
  38.  * for (String file : files) {
  39.  *     System.out.println(file);
  40.  * }
  41.  * </pre>
  42.  *
  43.  * <h2>Using NIO</h2>
  44.  * <pre>
  45.  * final Path dir = PathUtils.current();
  46.  * final AccumulatorPathVisitor visitor = AccumulatorPathVisitor.withLongCounters(new NameFileFilter("Test"));
  47.  * //
  48.  * // Walk one directory
  49.  * Files.<strong>walkFileTree</strong>(dir, Collections.emptySet(), 1, visitor);
  50.  * System.out.println(visitor.getPathCounters());
  51.  * System.out.println(visitor.getFileList());
  52.  * //
  53.  * visitor.getPathCounters().reset();
  54.  * //
  55.  * // Walk directory tree
  56.  * Files.<strong>walkFileTree</strong>(dir, visitor);
  57.  * System.out.println(visitor.getPathCounters());
  58.  * System.out.println(visitor.getDirList());
  59.  * System.out.println(visitor.getFileList());
  60.  * </pre>
  61.  * <h2>Deprecating Serialization</h2>
  62.  * <p>
  63.  * <em>Serialization is deprecated and will be removed in 3.0.</em>
  64.  * </p>
  65.  *
  66.  * @since 1.0
  67.  * @see FileFilterUtils#nameFileFilter(String)
  68.  * @see FileFilterUtils#nameFileFilter(String, IOCase)
  69.  */
  70. public class NameFileFilter extends AbstractFileFilter implements Serializable {

  71.     private static final long serialVersionUID = 176844364689077340L;

  72.     /** The file names to search for */
  73.     private final String[] names;

  74.     /** Whether the comparison is case-sensitive. */
  75.     private final IOCase ioCase;

  76.     /**
  77.      * Constructs a new case-sensitive name file filter for a list of names.
  78.      *
  79.      * @param names  the names to allow, must not be null
  80.      * @throws IllegalArgumentException if the name list is null
  81.      * @throws ClassCastException if the list does not contain Strings
  82.      */
  83.     public NameFileFilter(final List<String> names) {
  84.         this(names, null);
  85.     }

  86.     /**
  87.      * Constructs a new name file filter for a list of names specifying case-sensitivity.
  88.      *
  89.      * @param names  the names to allow, must not be null
  90.      * @param ioCase  how to handle case sensitivity, null means case-sensitive
  91.      * @throws NullPointerException if the name list is null
  92.      * @throws ClassCastException if the list does not contain Strings
  93.      */
  94.     public NameFileFilter(final List<String> names, final IOCase ioCase) {
  95.         Objects.requireNonNull(names, "names");
  96.         this.names = names.toArray(EMPTY_STRING_ARRAY);
  97.         this.ioCase = toIOCase(ioCase);
  98.     }

  99.     /**
  100.      * Constructs a new case-sensitive name file filter for a single name.
  101.      *
  102.      * @param name  the name to allow, must not be null
  103.      * @throws IllegalArgumentException if the name is null
  104.      */
  105.     public NameFileFilter(final String name) {
  106.         this(name, IOCase.SENSITIVE);
  107.     }

  108.     /**
  109.      * Constructs a new case-sensitive name file filter for an array of names.
  110.      * <p>
  111.      * The array is not cloned, so could be changed after constructing the
  112.      * instance. This would be inadvisable however.
  113.      * </p>
  114.      *
  115.      * @param names  the names to allow, must not be null
  116.      * @throws IllegalArgumentException if the names array is null
  117.      */
  118.     public NameFileFilter(final String... names) {
  119.         this(names, IOCase.SENSITIVE);
  120.     }

  121.     /**
  122.      * Constructs a new name file filter specifying case-sensitivity.
  123.      *
  124.      * @param name  the name to allow, must not be null
  125.      * @param ioCase  how to handle case sensitivity, null means case-sensitive
  126.      * @throws NullPointerException if the name is null
  127.      */
  128.     public NameFileFilter(final String name, final IOCase ioCase) {
  129.         Objects.requireNonNull(name, "name");
  130.         this.names = new String[] {name};
  131.         this.ioCase = toIOCase(ioCase);
  132.     }

  133.     /**
  134.      * Constructs a new name file filter for an array of names specifying case-sensitivity.
  135.      *
  136.      * @param names  the names to allow, must not be null
  137.      * @param ioCase  how to handle case sensitivity, null means case-sensitive
  138.      * @throws NullPointerException if the names array is null
  139.      */
  140.     public NameFileFilter(final String[] names, final IOCase ioCase) {
  141.         Objects.requireNonNull(names, "names");
  142.         this.names = names.clone();
  143.         this.ioCase = toIOCase(ioCase);
  144.     }

  145.     /**
  146.      * Checks to see if the file name matches.
  147.      *
  148.      * @param file  the File to check
  149.      * @return true if the file name matches
  150.      */
  151.     @Override
  152.     public boolean accept(final File file) {
  153.         return file != null && acceptBaseName(file.getName());
  154.     }

  155.     /**
  156.      * Checks to see if the file name matches.
  157.      *
  158.      * @param dir  the File directory (ignored)
  159.      * @param name  the file name
  160.      * @return true if the file name matches
  161.      */
  162.     @Override
  163.     public boolean accept(final File dir, final String name) {
  164.         return acceptBaseName(name);
  165.     }

  166.     /**
  167.      * Checks to see if the file name matches.
  168.      *
  169.      * @param path  the File to check
  170.      * @param attributes the path's basic attributes (may be null).
  171.      * @return true if the file name matches
  172.      * @since 2.9.0
  173.      */
  174.     @Override
  175.     public FileVisitResult accept(final Path path, final BasicFileAttributes attributes) {
  176.         return toFileVisitResult(acceptBaseName(PathUtils.getFileNameString(path)));
  177.     }

  178.     private boolean acceptBaseName(final String baseName) {
  179.         return Stream.of(names).anyMatch(testName -> ioCase.checkEquals(baseName, testName));
  180.     }

  181.     private IOCase toIOCase(final IOCase ioCase) {
  182.         return IOCase.value(ioCase, IOCase.SENSITIVE);
  183.     }

  184.     /**
  185.      * Provide a String representation of this file filter.
  186.      *
  187.      * @return a String representation
  188.      */
  189.     @Override
  190.     public String toString() {
  191.         final StringBuilder buffer = new StringBuilder();
  192.         buffer.append(super.toString());
  193.         buffer.append("(");
  194.         append(names, buffer);
  195.         buffer.append(")");
  196.         return buffer.toString();
  197.     }

  198. }