    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.Path;
    023import java.nio.file.attribute.BasicFileAttributes;
    024import java.util.List;
    025import java.util.Objects;
    026import java.util.stream.Stream;
    027
    028import org.apache.commons.io.IOCase;
    029import org.apache.commons.io.file.PathUtils;
    030
    031/**
    032 * Filters file names for a certain prefix.
    033 * <p>
    034 * For example, to print all files and directories in the
    035 * current directory whose name starts with {@code Test}:
    036 * </p>
    037 * <h2>Using Classic IO</h2>
    038 * <pre>
    039 * File dir = FileUtils.current();
    040 * String[] files = dir.list(new PrefixFileFilter("Test"));
    041 * for (String file : files) {
    042 *     System.out.println(file);
    043 * }
    044 * </pre>
    045 *
    046 * <h2>Using NIO</h2>
    047 * <pre>
    048 * final Path dir = PathUtils.current();
    049 * final AccumulatorPathVisitor visitor = AccumulatorPathVisitor.withLongCounters(new PrefixFileFilter("Test"));
    050 * //
    051 * // Walk one directory
    052 * Files.<strong>walkFileTree</strong>(dir, Collections.emptySet(), 1, visitor);
    053 * System.out.println(visitor.getPathCounters());
    054 * System.out.println(visitor.getFileList());
    055 * //
    056 * visitor.getPathCounters().reset();
    057 * //
    058 * // Walk directory tree
    059 * Files.<strong>walkFileTree</strong>(dir, visitor);
    060 * System.out.println(visitor.getPathCounters());
    061 * System.out.println(visitor.getDirList());
    062 * System.out.println(visitor.getFileList());
    063 * </pre>
    064 * <h2>Deprecating Serialization</h2>
    065 * <p>
    066 * <em>Serialization is deprecated and will be removed in 3.0.</em>
    067 * </p>
    068 *
    069 * @since 1.0
    070 * @see FileFilterUtils#prefixFileFilter(String)
    071 * @see FileFilterUtils#prefixFileFilter(String, IOCase)
    072 */
    073public class PrefixFileFilter extends AbstractFileFilter implements Serializable {
    074
    075    private static final long serialVersionUID = 8533897440809599867L;
    076
    077    /** The file name prefixes to search for */
    078    private final String[] prefixes;
    079
    080    /** Whether the comparison is case-sensitive. */
    081    private final IOCase isCase;
    082
    083    /**
    084     * Constructs a new Prefix file filter for a list of prefixes.
    085     *
    086     * @param prefixes  the prefixes to allow, must not be null
    087     * @throws NullPointerException if the prefix list is null
    088     * @throws ClassCastException if the list does not contain Strings
    089     */
    090    public PrefixFileFilter(final List<String> prefixes) {
    091        this(prefixes, IOCase.SENSITIVE);
    092    }
    093
    094    /**
    095     * Constructs a new Prefix file filter for a list of prefixes
    096     * specifying case-sensitivity.
    097     *
    098     * @param prefixes  the prefixes to allow, must not be null
    099     * @param ioCase  how to handle case sensitivity, null means case-sensitive
    100     * @throws NullPointerException if the prefix list is null
    101     * @throws ClassCastException if the list does not contain Strings
    102     * @since 1.4
    103     */
    104    public PrefixFileFilter(final List<String> prefixes, final IOCase ioCase) {
    105        Objects.requireNonNull(prefixes, "prefixes");
    106        this.prefixes = prefixes.toArray(EMPTY_STRING_ARRAY);
    107        this.isCase = IOCase.value(ioCase, IOCase.SENSITIVE);
    108    }
    109
    110    /**
    111     * Constructs a new Prefix file filter for a single prefix.
    112     *
    113     * @param prefix  the prefix to allow, must not be null
    114     * @throws IllegalArgumentException if the prefix is null
    115     */
    116    public PrefixFileFilter(final String prefix) {
    117        this(prefix, IOCase.SENSITIVE);
    118    }
    119
    120    /**
    121     * Constructs a new Prefix file filter for any of an array of prefixes.
    122     * <p>
    123     * The array is not cloned, so could be changed after constructing the
    124     * instance. This would be inadvisable however.
    125     * </p>
    126     *
    127     * @param prefixes  the prefixes to allow, must not be null
    128     * @throws IllegalArgumentException if the prefix array is null
    129     */
    130    public PrefixFileFilter(final String... prefixes) {
    131        this(prefixes, IOCase.SENSITIVE);
    132    }
    133
    134    /**
    135     * Constructs a new Prefix file filter for a single prefix
    136     * specifying case-sensitivity.
    137     *
    138     * @param prefix  the prefix to allow, must not be null
    139     * @param ioCase  how to handle case sensitivity, null means case-sensitive
    140     * @throws IllegalArgumentException if the prefix is null
    141     * @since 1.4
    142     */
    143    public PrefixFileFilter(final String prefix, final IOCase ioCase) {
    144        Objects.requireNonNull(prefix, "prefix");
    145        this.prefixes = new String[] {prefix};
    146        this.isCase = IOCase.value(ioCase, IOCase.SENSITIVE);
    147    }
    148
    149    /**
    150     * Constructs a new Prefix file filter for any of an array of prefixes
    151     * specifying case-sensitivity.
    152     *
    153     * @param prefixes  the prefixes to allow, must not be null
    154     * @param ioCase  how to handle case sensitivity, null means case-sensitive
    155     * @throws IllegalArgumentException if the prefix is null
    156     * @since 1.4
    157     */
    158    public PrefixFileFilter(final String[] prefixes, final IOCase ioCase) {
    159        Objects.requireNonNull(prefixes, "prefixes");
    160        this.prefixes = prefixes.clone();
    161        this.isCase = IOCase.value(ioCase, IOCase.SENSITIVE);
    162    }
    163
    164    /**
    165     * Checks to see if the file name starts with the prefix.
    166     *
    167     * @param file  the File to check
    168     * @return true if the file name starts with one of our prefixes
    169     */
    170    @Override
    171    public boolean accept(final File file) {
    172        return accept(file == null ? null : file.getName());
    173    }
    174
    175    /**
    176     * Checks to see if the file name starts with the prefix.
    177     *
    178     * @param file  the File directory
    179     * @param name  the file name
    180     * @return true if the file name starts with one of our prefixes
    181     */
    182    @Override
    183    public boolean accept(final File file, final String name) {
    184        return accept(name);
    185    }
    186
    187    /**
    188     * Checks to see if the file name starts with the prefix.
    189     *
    190     * @param file  the File to check
    191     * @param attributes the path's basic attributes (may be null).
    192     * @return true if the file name starts with one of our prefixes
    193     * @since 2.9.0
    194     */
    195    @Override
    196    public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) {
    197        return toFileVisitResult(accept(PathUtils.getFileName(file, Path::toFile)));
    198    }
    199
    200    private boolean accept(final String name) {
    201        return Stream.of(prefixes).anyMatch(prefix -> isCase.checkStartsWith(name, prefix));
    202    }
    203
    204    /**
    205     * Provide a String representation of this file filter.
    206     *
    207     * @return a String representation
    208     */
    209    @Override
    210    public String toString() {
    211        final StringBuilder buffer = new StringBuilder();
    212        buffer.append(super.toString());
    213        buffer.append("(");
    214        append(prefixes, buffer);
    215        buffer.append(")");
    216        return buffer.toString();
    217    }
    218
    219}