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.vfs2.filter;
018
019import java.io.Serializable;
020import java.util.ArrayList;
021import java.util.Arrays;
022import java.util.List;
023
024import org.apache.commons.vfs2.FileFilter;
025import org.apache.commons.vfs2.FileSelectInfo;
026
027/**
028 * Filters file names for a certain name.
029 * <p>
030 * For example, to print all files and directories in the current directory
031 * whose name is {@code Test}:
032 * </p>
033 *
034 * <pre>
035 * FileSystemManager fsManager = VFS.getManager();
036 * FileObject dir = fsManager.toFileObject(new File(&quot;.&quot;));
037 * FileObject[] files = dir.findFiles(new FileFilterSelector(new NameFileFilter(&quot;Test&quot;)));
038 * for (int i = 0; i &lt; files.length; i++) {
039 *     System.out.println(files[i]);
040 * }
041 * </pre>
042 *
043 * @author This code was originally ported from Apache Commons IO File Filter
044 * @see "https://commons.apache.org/proper/commons-io/"
045 * @since 2.4
046 */
047public class NameFileFilter implements FileFilter, Serializable {
048
049    private static final long serialVersionUID = 1L;
050
051    /** Whether the comparison is case-sensitive. */
052    private final IOCase caseSensitivity;
053
054    /** The file names to search for. */
055    private final List<String> names;
056
057    /**
058     * Constructs a new name file filter for a list of names specifying
059     * case-sensitivity.
060     *
061     * @param caseSensitivity how to handle case sensitivity, null means
062     *                        case-sensitive
063     * @param names           the names to allow, must not be null
064     */
065    public NameFileFilter(final IOCase caseSensitivity, final List<String> names) {
066        if (names == null) {
067            throw new IllegalArgumentException("The list of names must not be null");
068        }
069        this.names = new ArrayList<>(names);
070        this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
071    }
072
073    /**
074     * Constructs a new name file filter for an array of names specifying
075     * case-sensitivity.
076     *
077     * @param caseSensitivity how to handle case sensitivity, null means
078     *                        case-sensitive
079     * @param names           the names to allow, must not be null
080     */
081    public NameFileFilter(final IOCase caseSensitivity, final String... names) {
082        if (names == null) {
083            throw new IllegalArgumentException("The array of names must not be null");
084        }
085        this.names = new ArrayList<>(Arrays.asList(names));
086        this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
087    }
088
089    /**
090     * Constructs a new case-sensitive name file filter for a list of names.
091     *
092     * @param names the names to allow, must not be null
093     */
094    public NameFileFilter(final List<String> names) {
095        this(null, names);
096    }
097
098    /**
099     * Constructs a new case-sensitive name file filter for an array of names.
100     * <p>
101     * The array is not cloned, so could be changed after constructing the instance.
102     * This would be inadvisable however.
103     * </p>
104     *
105     * @param names the names to allow, must not be null
106     */
107    public NameFileFilter(final String... names) {
108        this(null, names);
109    }
110
111    /**
112     * Checks to see if the file name matches.
113     *
114     * @param fileSelectInfo the File to check
115     * @return true if the file name matches
116     */
117    @Override
118    public boolean accept(final FileSelectInfo fileSelectInfo) {
119        final String name = fileSelectInfo.getFile().getName().getBaseName();
120        return names.stream().anyMatch(name2 -> caseSensitivity.checkEquals(name, name2));
121    }
122
123    /**
124     * Provide a String representation of this file filter.
125     *
126     * @return a String representation
127     */
128    @Override
129    public String toString() {
130        final StringBuilder buffer = new StringBuilder();
131        buffer.append(super.toString());
132        buffer.append("(");
133        if (names != null) {
134            buffer.append(String.join(",", names));
135        }
136        buffer.append(")");
137        return buffer.toString();
138    }
139
140}