~ubuntu-branches/ubuntu/karmic/commons-io/karmic

« back to all changes in this revision

Viewing changes to src/java/org/apache/commons/io/filefilter/PrefixFileFilter.java

  • Committer: Bazaar Package Importer
  • Author(s): Wolfgang Baer
  • Date: 2005-10-16 13:44:21 UTC
  • Revision ID: james.westby@ubuntu.com-20051016134421-v2gfddy6iovz449t
Tags: upstream-1.0
ImportĀ upstreamĀ versionĀ 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2002-2004 The Apache Software Foundation.
 
3
 * 
 
4
 * Licensed under the Apache License, Version 2.0 (the "License");
 
5
 * you may not use this file except in compliance with the License.
 
6
 * You may obtain a copy of the License at
 
7
 * 
 
8
 *      http://www.apache.org/licenses/LICENSE-2.0
 
9
 * 
 
10
 * Unless required by applicable law or agreed to in writing, software
 
11
 * distributed under the License is distributed on an "AS IS" BASIS,
 
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
13
 * See the License for the specific language governing permissions and
 
14
 * limitations under the License.
 
15
 */
 
16
package org.apache.commons.io.filefilter;
 
17
 
 
18
import java.io.File;
 
19
import java.util.List;
 
20
 
 
21
/**
 
22
 * Filters filenames for a certain prefix.
 
23
 * <p>
 
24
 * For example, to print all files and directories in the 
 
25
 * current directory whose name starts with <code>Test</code>:
 
26
 *
 
27
 * <pre>
 
28
 * File dir = new File(".");
 
29
 * String[] files = dir.list( new PrefixFileFilter("Test") );
 
30
 * for ( int i = 0; i &lt; files.length; i++ ) {
 
31
 *     System.out.println(files[i]);
 
32
 * }
 
33
 * </pre>
 
34
 *
 
35
 * @since Commons IO 1.0
 
36
 * @version $Revision: 1.8 $ $Date: 2004/02/23 04:37:57 $
 
37
 * 
 
38
 * @author Henri Yandell
 
39
 * @author Stephen Colebourne
 
40
 * @author Federico Barbieri
 
41
 * @author Serge Knystautas
 
42
 * @author Peter Donald
 
43
 */
 
44
public class PrefixFileFilter extends AbstractFileFilter {
 
45
    
 
46
    /** The filename prefixes to search for */
 
47
    private String[] prefixes;
 
48
 
 
49
    /**
 
50
     * Constructs a new Prefix file filter for a single prefix.
 
51
     * 
 
52
     * @param prefix  the prefix to allow, must not be null
 
53
     * @throws IllegalArgumentException if the prefix is null
 
54
     */
 
55
    public PrefixFileFilter(String prefix) {
 
56
        if (prefix == null) {
 
57
            throw new IllegalArgumentException("The prefix must not be null");
 
58
        }
 
59
        this.prefixes = new String[] {prefix};
 
60
    }
 
61
 
 
62
    /**
 
63
     * Constructs a new Prefix file filter for any of an array of prefixes.
 
64
     * <p>
 
65
     * The array is not cloned, so could be changed after constructing the
 
66
     * instance. This would be inadvisable however.
 
67
     * 
 
68
     * @param prefixes  the prefixes to allow, must not be null
 
69
     * @throws IllegalArgumentException if the prefix array is null
 
70
     */
 
71
    public PrefixFileFilter(String[] prefixes) {
 
72
        if (prefixes == null) {
 
73
            throw new IllegalArgumentException("The array of prefixes must not be null");
 
74
        }
 
75
        this.prefixes = prefixes;
 
76
    }
 
77
 
 
78
    /**
 
79
     * Constructs a new Prefix file filter for a list of prefixes.
 
80
     * 
 
81
     * @param prefixes  the prefixes to allow, must not be null
 
82
     * @throws IllegalArgumentException if the prefix list is null
 
83
     * @throws ClassCastException if the list does not contain Strings
 
84
     */
 
85
    public PrefixFileFilter(List prefixes) {
 
86
        if (prefixes == null) {
 
87
            throw new IllegalArgumentException("The list of prefixes must not be null");
 
88
        }
 
89
        this.prefixes = (String[]) prefixes.toArray(new String[prefixes.size()]);
 
90
    }
 
91
 
 
92
    /**
 
93
     * Checks to see if the filename starts with the prefix.
 
94
     * 
 
95
     * @param file  the File to check
 
96
     * @return true if the filename starts with one of our prefixes
 
97
     */
 
98
    public boolean accept(File file) {
 
99
        String name = file.getName();
 
100
        for (int i = 0; i < this.prefixes.length; i++) {
 
101
            if (name.startsWith(this.prefixes[i])) {
 
102
                return true;
 
103
            }
 
104
        }
 
105
        return false;
 
106
    }
 
107
    
 
108
    /**
 
109
     * Checks to see if the filename starts with the prefix.
 
110
     * 
 
111
     * @param file  the File directory
 
112
     * @param name  the filename
 
113
     * @return true if the filename starts with one of our prefixes
 
114
     */
 
115
    public boolean accept(File file, String name) {
 
116
        for (int i = 0; i < prefixes.length; i++) {
 
117
            if (name.startsWith(prefixes[i])) {
 
118
                return true;
 
119
            }
 
120
        }
 
121
        return false;
 
122
    }
 
123
    
 
124
}