~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): Varun Hiremath
  • Date: 2008-02-21 13:26:43 UTC
  • mfrom: (1.1.3 upstream) (3.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080221132643-p4c8f8lhb9rnqnlo
Tags: 1.4-1
* New upstream release
* Bump Standards-Version to 3.7.3
* Bump up debhelper compat to 6
* Replace XS-Vcs headers with Vcs
* debian/patches:
  - remove 01_no_ext_links.dpatch - not required
  - remove 02_no_mkdir_in_homedir.dpatch - not required
* Remove dpatch from Build-Depends
* Update debian/rules and debian/libcommons-io-java-doc.install
  with new target dirs
* debian/copyright: add copyright notice

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
package org.apache.commons.io.filefilter;
18
18
 
19
19
import java.io.File;
 
20
import java.io.Serializable;
20
21
import java.util.List;
21
22
 
 
23
import org.apache.commons.io.IOCase;
 
24
 
22
25
/**
23
26
 * Filters filenames for a certain prefix.
24
27
 * <p>
34
37
 * </pre>
35
38
 *
36
39
 * @since Commons IO 1.0
37
 
 * @version $Revision: 471628 $ $Date: 2006-11-06 05:06:45 +0100 (Mo, 06 Nov 2006) $
 
40
 * @version $Revision: 606381 $ $Date: 2007-12-22 02:03:16 +0000 (Sat, 22 Dec 2007) $
38
41
 * 
39
42
 * @author Stephen Colebourne
40
43
 * @author Federico Barbieri
41
44
 * @author Serge Knystautas
42
45
 * @author Peter Donald
43
46
 */
44
 
public class PrefixFileFilter extends AbstractFileFilter {
 
47
public class PrefixFileFilter extends AbstractFileFilter implements Serializable {
45
48
    
46
49
    /** The filename prefixes to search for */
47
 
    private String[] prefixes;
 
50
    private final String[] prefixes;
 
51
 
 
52
    /** Whether the comparison is case sensitive. */
 
53
    private final IOCase caseSensitivity;
48
54
 
49
55
    /**
50
56
     * Constructs a new Prefix file filter for a single prefix.
53
59
     * @throws IllegalArgumentException if the prefix is null
54
60
     */
55
61
    public PrefixFileFilter(String prefix) {
 
62
        this(prefix, IOCase.SENSITIVE);
 
63
    }
 
64
 
 
65
    /**
 
66
     * Constructs a new Prefix file filter for a single prefix 
 
67
     * specifying case-sensitivity.
 
68
     * 
 
69
     * @param prefix  the prefix to allow, must not be null
 
70
     * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive
 
71
     * @throws IllegalArgumentException if the prefix is null
 
72
     * @since Commons IO 1.4
 
73
     */
 
74
    public PrefixFileFilter(String prefix, IOCase caseSensitivity) {
56
75
        if (prefix == null) {
57
76
            throw new IllegalArgumentException("The prefix must not be null");
58
77
        }
59
78
        this.prefixes = new String[] {prefix};
 
79
        this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
60
80
    }
61
81
 
62
82
    /**
69
89
     * @throws IllegalArgumentException if the prefix array is null
70
90
     */
71
91
    public PrefixFileFilter(String[] prefixes) {
 
92
        this(prefixes, IOCase.SENSITIVE);
 
93
    }
 
94
 
 
95
    /**
 
96
     * Constructs a new Prefix file filter for any of an array of prefixes
 
97
     * specifying case-sensitivity.
 
98
     * <p>
 
99
     * The array is not cloned, so could be changed after constructing the
 
100
     * instance. This would be inadvisable however.
 
101
     * 
 
102
     * @param prefixes  the prefixes to allow, must not be null
 
103
     * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive
 
104
     * @throws IllegalArgumentException if the prefix is null
 
105
     * @since Commons IO 1.4
 
106
     */
 
107
    public PrefixFileFilter(String[] prefixes, IOCase caseSensitivity) {
72
108
        if (prefixes == null) {
73
109
            throw new IllegalArgumentException("The array of prefixes must not be null");
74
110
        }
75
111
        this.prefixes = prefixes;
 
112
        this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
76
113
    }
77
114
 
78
115
    /**
83
120
     * @throws ClassCastException if the list does not contain Strings
84
121
     */
85
122
    public PrefixFileFilter(List prefixes) {
 
123
        this(prefixes, IOCase.SENSITIVE);
 
124
    }
 
125
 
 
126
    /**
 
127
     * Constructs a new Prefix file filter for a list of prefixes
 
128
     * specifying case-sensitivity.
 
129
     * 
 
130
     * @param prefixes  the prefixes to allow, must not be null
 
131
     * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive
 
132
     * @throws IllegalArgumentException if the prefix list is null
 
133
     * @throws ClassCastException if the list does not contain Strings
 
134
     * @since Commons IO 1.4
 
135
     */
 
136
    public PrefixFileFilter(List prefixes, IOCase caseSensitivity) {
86
137
        if (prefixes == null) {
87
138
            throw new IllegalArgumentException("The list of prefixes must not be null");
88
139
        }
89
140
        this.prefixes = (String[]) prefixes.toArray(new String[prefixes.size()]);
 
141
        this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
90
142
    }
91
143
 
92
144
    /**
98
150
    public boolean accept(File file) {
99
151
        String name = file.getName();
100
152
        for (int i = 0; i < this.prefixes.length; i++) {
101
 
            if (name.startsWith(this.prefixes[i])) {
 
153
            if (caseSensitivity.checkStartsWith(name, prefixes[i])) {
102
154
                return true;
103
155
            }
104
156
        }
114
166
     */
115
167
    public boolean accept(File file, String name) {
116
168
        for (int i = 0; i < prefixes.length; i++) {
117
 
            if (name.startsWith(prefixes[i])) {
 
169
            if (caseSensitivity.checkStartsWith(name, prefixes[i])) {
118
170
                return true;
119
171
            }
120
172
        }
121
173
        return false;
122
174
    }
 
175
 
 
176
    /**
 
177
     * Provide a String representaion of this file filter.
 
178
     *
 
179
     * @return a String representaion
 
180
     */
 
181
    public String toString() {
 
182
        StringBuffer buffer = new StringBuffer();
 
183
        buffer.append(super.toString());
 
184
        buffer.append("(");
 
185
        if (prefixes != null) {
 
186
            for (int i = 0; i < prefixes.length; i++) {
 
187
                if (i > 0) {
 
188
                    buffer.append(",");
 
189
                }
 
190
                buffer.append(prefixes[i]);
 
191
            }
 
192
        }
 
193
        buffer.append(")");
 
194
        return buffer.toString();
 
195
    }
123
196
    
124
197
}