~ubuntu-branches/ubuntu/quantal/commons-io/quantal

« back to all changes in this revision

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