View Javadoc
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.vfs2.filter;
18  
19  import java.io.Serializable;
20  import java.util.ArrayList;
21  import java.util.Arrays;
22  import java.util.List;
23  
24  import org.apache.commons.vfs2.FileFilter;
25  import org.apache.commons.vfs2.FileSelectInfo;
26  
27  /**
28   * Filters file names for a certain prefix.
29   * <p>
30   * For example, to print all files and directories in the current directory
31   * whose name starts with a <code>.</code>:
32   * </p>
33   *
34   * <pre>
35   * FileSystemManager fsManager = VFS.getManager();
36   * FileObject dir = fsManager.toFileObject(new File(&quot;.&quot;));
37   * FileObject[] files = dir.findFiles(new FileFilterSelector(new PrefixFileFilter(&quot;.&quot;)));
38   * for (int i = 0; i &lt; files.length; i++) {
39   *     System.out.println(files[i]);
40   * }
41   * </pre>
42   *
43   * @author This code was originally ported from Apache Commons IO File Filter
44   * @see "http://commons.apache.org/proper/commons-io/"
45   * @since 2.4
46   */
47  public class PrefixFileFilter implements FileFilter, Serializable {
48  
49      private static final long serialVersionUID = 1L;
50  
51      /** Whether the comparison is case sensitive. */
52      private final IOCase caseSensitivity;
53  
54      /** The file name prefixes to search for. */
55      private final List<String> prefixes;
56  
57      /**
58       * Constructs a new Prefix file filter for a list of prefixes.
59       *
60       * @param prefixes the prefixes to allow, must not be null
61       */
62      public PrefixFileFilter(final List<String> prefixes) {
63          this(IOCase.SENSITIVE, prefixes);
64      }
65  
66      /**
67       * Constructs a new Prefix file filter for a list of prefixes specifying
68       * case-sensitivity.
69       *
70       * @param caseSensitivity how to handle case sensitivity, null means
71       *                        case-sensitive
72       * @param prefixes        the prefixes to allow, must not be null
73       */
74      public PrefixFileFilter(final IOCase caseSensitivity, final List<String> prefixes) {
75          if (prefixes == null) {
76              throw new IllegalArgumentException("The list of prefixes must not be null");
77          }
78          this.prefixes = new ArrayList<>(prefixes);
79          this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
80      }
81  
82      /**
83       * Constructs a new Prefix file filter for any of an array of prefixes.
84       * <p>
85       * The array is not cloned, so could be changed after constructing the instance.
86       * This would be inadvisable however.
87       *
88       * @param prefixes the prefixes to allow, must not be null
89       */
90      public PrefixFileFilter(final String... prefixes) {
91          this(IOCase.SENSITIVE, prefixes);
92      }
93  
94      /**
95       * Constructs a new Prefix file filter for any of an array of prefixes
96       * specifying case-sensitivity.
97       *
98       * @param prefixes        the prefixes to allow, must not be null
99       * @param caseSensitivity how to handle case sensitivity, null means
100      *                        case-sensitive
101      */
102     public PrefixFileFilter(final IOCase caseSensitivity, final String... prefixes) {
103         if (prefixes == null) {
104             throw new IllegalArgumentException("The array of prefixes must not be null");
105         }
106         this.prefixes = new ArrayList<>(Arrays.asList(prefixes));
107         this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
108     }
109 
110     /**
111      * Checks to see if the file name starts with the prefix.
112      *
113      * @param fileInfo the File to check
114      *
115      * @return true if the file name starts with one of our prefixes
116      */
117     @Override
118     public boolean accept(final FileSelectInfo fileInfo) {
119         final String name = fileInfo.getFile().getName().getBaseName();
120         for (final String prefix : this.prefixes) {
121             if (caseSensitivity.checkStartsWith(name, prefix)) {
122                 return true;
123             }
124         }
125         return false;
126     }
127 
128     /**
129      * Provide a String representation of this file filter.
130      *
131      * @return a String representation
132      */
133     @Override
134     public String toString() {
135         final StringBuilder buffer = new StringBuilder();
136         buffer.append(super.toString());
137         buffer.append("(");
138         if (prefixes != null) {
139             for (int i = 0; i < prefixes.size(); i++) {
140                 if (i > 0) {
141                     buffer.append(",");
142                 }
143                 buffer.append(prefixes.get(i));
144             }
145         }
146         buffer.append(")");
147         return buffer.toString();
148     }
149 
150 }