    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.util.List;
    022
    023import org.apache.commons.io.IOCase;
    024
    025/**
    026 * Filters file names for a certain name.
    027 * <p>
    028 * For example, to print all files and directories in the
    029 * current directory whose name is <code>Test</code>:
    030 *
    031 * <pre>
    032 * File dir = new File(".");
    033 * String[] files = dir.list( new NameFileFilter("Test") );
    034 * for ( int i = 0; i &lt; files.length; i++ ) {
    035 *     System.out.println(files[i]);
    036 * }
    037 * </pre>
    038 *
    039 * @since 1.0
    040 *
    041 * @see FileFilterUtils#nameFileFilter(String)
    042 * @see FileFilterUtils#nameFileFilter(String, IOCase)
    043 */
    044public class NameFileFilter extends AbstractFileFilter implements Serializable {
    045
    046    private static final long serialVersionUID = 176844364689077340L;
    047    /** The file names to search for */
    048    private final String[] names;
    049    /** Whether the comparison is case sensitive. */
    050    private final IOCase caseSensitivity;
    051
    052    /**
    053     * Constructs a new case-sensitive name file filter for a single name.
    054     *
    055     * @param name  the name to allow, must not be null
    056     * @throws IllegalArgumentException if the name is null
    057     */
    058    public NameFileFilter(final String name) {
    059        this(name, null);
    060    }
    061
    062    /**
    063     * Construct a new name file filter specifying case-sensitivity.
    064     *
    065     * @param name  the name to allow, must not be null
    066     * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive
    067     * @throws IllegalArgumentException if the name is null
    068     */
    069    public NameFileFilter(final String name, final IOCase caseSensitivity) {
    070        if (name == null) {
    071            throw new IllegalArgumentException("The wildcard must not be null");
    072        }
    073        this.names = new String[] {name};
    074        this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
    075    }
    076
    077    /**
    078     * Constructs a new case-sensitive name file filter for an array of names.
    079     * <p>
    080     * The array is not cloned, so could be changed after constructing the
    081     * instance. This would be inadvisable however.
    082     *
    083     * @param names  the names to allow, must not be null
    084     * @throws IllegalArgumentException if the names array is null
    085     */
    086    public NameFileFilter(final String... names) {
    087        this(names, null);
    088    }
    089
    090    /**
    091     * Constructs a new name file filter for an array of names specifying case-sensitivity.
    092     *
    093     * @param names  the names to allow, must not be null
    094     * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive
    095     * @throws IllegalArgumentException if the names array is null
    096     */
    097    public NameFileFilter(final String[] names, final IOCase caseSensitivity) {
    098        if (names == null) {
    099            throw new IllegalArgumentException("The array of names must not be null");
    100        }
    101        this.names = new String[names.length];
    102        System.arraycopy(names, 0, this.names, 0, names.length);
    103        this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
    104    }
    105
    106    /**
    107     * Constructs a new case-sensitive name file filter for a list of names.
    108     *
    109     * @param names  the names to allow, must not be null
    110     * @throws IllegalArgumentException if the name list is null
    111     * @throws ClassCastException if the list does not contain Strings
    112     */
    113    public NameFileFilter(final List<String> names) {
    114        this(names, null);
    115    }
    116
    117    /**
    118     * Constructs a new name file filter for a list of names specifying case-sensitivity.
    119     *
    120     * @param names  the names to allow, must not be null
    121     * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive
    122     * @throws IllegalArgumentException if the name list is null
    123     * @throws ClassCastException if the list does not contain Strings
    124     */
    125    public NameFileFilter(final List<String> names, final IOCase caseSensitivity) {
    126        if (names == null) {
    127            throw new IllegalArgumentException("The list of names must not be null");
    128        }
    129        this.names = names.toArray(EMPTY_STRING_ARRAY);
    130        this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
    131    }
    132
    133    //-----------------------------------------------------------------------
    134    /**
    135     * Checks to see if the file name matches.
    136     *
    137     * @param file  the File to check
    138     * @return true if the file name matches
    139     */
    140    @Override
    141    public boolean accept(final File file) {
    142        final String name = file.getName();
    143        for (final String name2 : this.names) {
    144            if (caseSensitivity.checkEquals(name, name2)) {
    145                return true;
    146            }
    147        }
    148        return false;
    149    }
    150
    151    /**
    152     * Checks to see if the file name matches.
    153     *
    154     * @param dir  the File directory (ignored)
    155     * @param name  the file name
    156     * @return true if the file name matches
    157     */
    158    @Override
    159    public boolean accept(final File dir, final String name) {
    160        for (final String name2 : names) {
    161            if (caseSensitivity.checkEquals(name, name2)) {
    162                return true;
    163            }
    164        }
    165        return false;
    166    }
    167
    168    /**
    169     * Provide a String representation of this file filter.
    170     *
    171     * @return a String representation
    172     */
    173    @Override
    174    public String toString() {
    175        final StringBuilder buffer = new StringBuilder();
    176        buffer.append(super.toString());
    177        buffer.append("(");
    178        if (names != null) {
    179            for (int i = 0; i < names.length; i++) {
    180                if (i > 0) {
    181                    buffer.append(",");
    182                }
    183                buffer.append(names[i]);
    184            }
    185        }
    186        buffer.append(")");
    187        return buffer.toString();
    188    }
    189
    190}