11/*
2- * Copyright 2002-2004 The Apache Software Foundation.
2+ * Copyright 2002-2004,2006 The Apache Software Foundation.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1818import java .io .File ;
1919import java .util .List ;
2020
21+ import org .apache .commons .io .IOCase ;
22+
2123/**
2224 * Filters filenames for a certain name.
2325 * <p>
@@ -45,22 +47,35 @@ public class NameFileFilter extends AbstractFileFilter {
4547
4648 /** The filenames to search for */
4749 private String [] names ;
50+ /** Whether the comparison is case sensitive. */
51+ private IOCase caseSensitivity ;
4852
4953 /**
50- * Constructs a new name file filter for a single name.
54+ * Constructs a new case-sensitive name file filter for a single name.
5155 *
5256 * @param name the name to allow, must not be null
53- * @throws IllegalArgumentException if the prefix is null
57+ * @throws IllegalArgumentException if the name is null
5458 */
5559 public NameFileFilter (String name ) {
60+ this (name , null );
61+ }
62+
63+ /**
64+ * Construct a new name file filter specifying case-sensitivity.
65+ *
66+ * @param caseSensitivity how to handle case sensitivity, null means case-sensitive
67+ * @throws IllegalArgumentException if the name is null
68+ */
69+ public NameFileFilter (String name , IOCase caseSensitivity ) {
5670 if (name == null ) {
57- throw new IllegalArgumentException ("The name must not be null" );
71+ throw new IllegalArgumentException ("The wildcard must not be null" );
5872 }
5973 this .names = new String [] {name };
74+ this .caseSensitivity = (caseSensitivity == null ? IOCase .SENSITIVE : caseSensitivity );
6075 }
6176
6277 /**
63- * Constructs a new name file filter for any of an array of names.
78+ * Constructs a new case-sensitive name file filter for an array of names.
6479 * <p>
6580 * The array is not cloned, so could be changed after constructing the
6681 * instance. This would be inadvisable however.
@@ -69,26 +84,55 @@ public NameFileFilter(String name) {
6984 * @throws IllegalArgumentException if the names array is null
7085 */
7186 public NameFileFilter (String [] names ) {
87+ this (names , null );
88+ }
89+
90+ /**
91+ * Constructs a new name file filter for an array of names specifying case-sensitivity.
92+ * <p>
93+ * The array is not cloned, so could be changed after constructing the
94+ * instance. This would be inadvisable however.
95+ *
96+ * @param names the names to allow, must not be null
97+ * @param caseSensitivity how to handle case sensitivity, null means case-sensitive
98+ * @throws IllegalArgumentException if the names array is null
99+ */
100+ public NameFileFilter (String [] names , IOCase caseSensitivity ) {
72101 if (names == null ) {
73102 throw new IllegalArgumentException ("The array of names must not be null" );
74103 }
75104 this .names = names ;
105+ this .caseSensitivity = (caseSensitivity == null ? IOCase .SENSITIVE : caseSensitivity );
76106 }
77107
78108 /**
79- * Constructs a new name file filter for a list of names.
109+ * Constructs a new case-sensitive name file filter for a list of names.
80110 *
81111 * @param names the names to allow, must not be null
82112 * @throws IllegalArgumentException if the name list is null
83113 * @throws ClassCastException if the list does not contain Strings
84114 */
85115 public NameFileFilter (List names ) {
116+ this (names , null );
117+ }
118+
119+ /**
120+ * Constructs a new name file filter for a list of names specifying case-sensitivity.
121+ *
122+ * @param names the names to allow, must not be null
123+ * @param caseSensitivity how to handle case sensitivity, null means case-sensitive
124+ * @throws IllegalArgumentException if the name list is null
125+ * @throws ClassCastException if the list does not contain Strings
126+ */
127+ public NameFileFilter (List names , IOCase caseSensitivity ) {
86128 if (names == null ) {
87129 throw new IllegalArgumentException ("The list of names must not be null" );
88130 }
89131 this .names = (String []) names .toArray (new String [names .size ()]);
132+ this .caseSensitivity = (caseSensitivity == null ? IOCase .SENSITIVE : caseSensitivity );
90133 }
91134
135+ //-----------------------------------------------------------------------
92136 /**
93137 * Checks to see if the filename matches.
94138 *
@@ -98,13 +142,13 @@ public NameFileFilter(List names) {
98142 public boolean accept (File file ) {
99143 String name = file .getName ();
100144 for (int i = 0 ; i < this .names .length ; i ++) {
101- if (name . equals ( this . names [i ])) {
145+ if (caseSensitivity . checkEquals ( name , names [i ])) {
102146 return true ;
103147 }
104148 }
105149 return false ;
106150 }
107-
151+
108152 /**
109153 * Checks to see if the filename matches.
110154 *
@@ -113,12 +157,12 @@ public boolean accept(File file) {
113157 * @return true if the filename matches
114158 */
115159 public boolean accept (File file , String name ) {
116- for (int i = 0 ; i < this . names .length ; i ++) {
117- if (name . equals ( this . names [i ])) {
160+ for (int i = 0 ; i < names .length ; i ++) {
161+ if (caseSensitivity . checkEquals ( name , names [i ])) {
118162 return true ;
119163 }
120164 }
121165 return false ;
122166 }
123-
167+
124168}
0 commit comments