Skip to content

Commit c22eabc

Browse files
author
Gary Gregory
committed
Add and use IOCase.value(IOCase, IOCase).
1 parent c38fd92 commit c22eabc

13 files changed

Lines changed: 127 additions & 120 deletions

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,9 @@ The <action> type attribute can be add,update,fix,remove.
202202
Add and use PathUtils.sizeOfDirectory(Path).
203203
Add and use PathUtils.sizeOfDirectoryAsBigInteger(Path).
204204
</action>
205+
<action dev="ggregory" type="add" due-to="Gary Gregory">
206+
Add and use IOCase.value(IOCase, IOCase).
207+
</action>
205208
<action dev="jonfreedman" type="add" due-to="Jon Freedman, Gary Gregory">
206209
Add Tailer.Tailable interface to allow tailing of remote files for example using jCIFS.
207210
</action>

src/main/java/org/apache/commons/io/FilenameUtils.java

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -438,11 +438,11 @@ public static boolean equals(final String fileName1, final String fileName2) {
438438
* @param fileName1 the first fileName to query, may be null
439439
* @param fileName2 the second fileName to query, may be null
440440
* @param normalized whether to normalize the fileNames
441-
* @param caseSensitivity what case sensitivity rule to use, null means case-sensitive
441+
* @param ioCase what case sensitivity rule to use, null means case-sensitive
442442
* @return true if the fileNames are equal, null equals null
443443
* @since 1.3
444444
*/
445-
public static boolean equals(String fileName1, String fileName2, final boolean normalized, IOCase caseSensitivity) {
445+
public static boolean equals(String fileName1, String fileName2, final boolean normalized, final IOCase ioCase) {
446446

447447
if (fileName1 == null || fileName2 == null) {
448448
return fileName1 == null && fileName2 == null;
@@ -457,10 +457,7 @@ public static boolean equals(String fileName1, String fileName2, final boolean n
457457
return false;
458458
}
459459
}
460-
if (caseSensitivity == null) {
461-
caseSensitivity = IOCase.SENSITIVE;
462-
}
463-
return caseSensitivity.checkEquals(fileName1, fileName2);
460+
return IOCase.value(ioCase, IOCase.SENSITIVE).checkEquals(fileName1, fileName2);
464461
}
465462

466463
/**
@@ -1567,20 +1564,18 @@ public static boolean wildcardMatch(final String fileName, final String wildcard
15671564
*
15681565
* @param fileName the fileName to match on
15691566
* @param wildcardMatcher the wildcard string to match against
1570-
* @param caseSensitivity what case sensitivity rule to use, null means case-sensitive
1567+
* @param ioCase what case sensitivity rule to use, null means case-sensitive
15711568
* @return true if the fileName matches the wildcard string
15721569
* @since 1.3
15731570
*/
1574-
public static boolean wildcardMatch(final String fileName, final String wildcardMatcher, IOCase caseSensitivity) {
1571+
public static boolean wildcardMatch(final String fileName, final String wildcardMatcher, IOCase ioCase) {
15751572
if (fileName == null && wildcardMatcher == null) {
15761573
return true;
15771574
}
15781575
if (fileName == null || wildcardMatcher == null) {
15791576
return false;
15801577
}
1581-
if (caseSensitivity == null) {
1582-
caseSensitivity = IOCase.SENSITIVE;
1583-
}
1578+
ioCase = IOCase.value(ioCase, IOCase.SENSITIVE);
15841579
final String[] wcs = splitOnTokens(wildcardMatcher);
15851580
boolean anyChars = false;
15861581
int textIdx = 0;
@@ -1618,16 +1613,16 @@ public static boolean wildcardMatch(final String fileName, final String wildcard
16181613
// matching text token
16191614
if (anyChars) {
16201615
// any chars then try to locate text token
1621-
textIdx = caseSensitivity.checkIndexOf(fileName, textIdx, wcs[wcsIdx]);
1616+
textIdx = ioCase.checkIndexOf(fileName, textIdx, wcs[wcsIdx]);
16221617
if (textIdx == NOT_FOUND) {
16231618
// token not found
16241619
break;
16251620
}
1626-
final int repeat = caseSensitivity.checkIndexOf(fileName, textIdx + 1, wcs[wcsIdx]);
1621+
final int repeat = ioCase.checkIndexOf(fileName, textIdx + 1, wcs[wcsIdx]);
16271622
if (repeat >= 0) {
16281623
backtrack.push(new int[] {wcsIdx, repeat});
16291624
}
1630-
} else if (!caseSensitivity.checkRegionMatches(fileName, textIdx, wcs[wcsIdx])) {
1625+
} else if (!ioCase.checkRegionMatches(fileName, textIdx, wcs[wcsIdx])) {
16311626
// matching from current position
16321627
// couldn't match token
16331628
break;

src/main/java/org/apache/commons/io/IOCase.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,24 @@ public enum IOCase {
6767
/**
6868
* Tests for cases sensitivity in a null-safe manner.
6969
*
70-
* @param caseSensitivity an IOCase.
70+
* @param ioCase an IOCase.
7171
* @return true if the input is non-null and {@link #isCaseSensitive()}.
7272
* @since 2.10.0
7373
*/
74-
public static boolean isCaseSensitive(final IOCase caseSensitivity) {
75-
return caseSensitivity != null && !caseSensitivity.isCaseSensitive();
74+
public static boolean isCaseSensitive(final IOCase ioCase) {
75+
return ioCase != null && !ioCase.isCaseSensitive();
76+
}
77+
78+
/**
79+
* Returns the given value if not-null, the defaultValue if null.
80+
*
81+
* @param value the value to test.
82+
* @param defaultValue the default value.
83+
* @return the given value if not-null, the defaultValue if null.
84+
* @since 2.12.0
85+
*/
86+
public static IOCase value(final IOCase value, final IOCase defaultValue) {
87+
return value != null ? value : defaultValue;
7688
}
7789

7890
/** Serialization version. */

src/main/java/org/apache/commons/io/comparator/ExtensionFileComparator.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,22 +77,22 @@ public class ExtensionFileComparator extends AbstractFileComparator implements S
7777
public static final Comparator<File> EXTENSION_SYSTEM_REVERSE = new ReverseFileComparator(EXTENSION_SYSTEM_COMPARATOR);
7878

7979
/** Whether the comparison is case sensitive. */
80-
private final IOCase caseSensitivity;
80+
private final IOCase ioCase;
8181

8282
/**
8383
* Constructs a case sensitive file extension comparator instance.
8484
*/
8585
public ExtensionFileComparator() {
86-
this.caseSensitivity = IOCase.SENSITIVE;
86+
this.ioCase = IOCase.SENSITIVE;
8787
}
8888

8989
/**
9090
* Constructs a file extension comparator instance with the specified case-sensitivity.
9191
*
92-
* @param caseSensitivity how to handle case sensitivity, null means case-sensitive
92+
* @param ioCase how to handle case sensitivity, null means case-sensitive
9393
*/
94-
public ExtensionFileComparator(final IOCase caseSensitivity) {
95-
this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
94+
public ExtensionFileComparator(final IOCase ioCase) {
95+
this.ioCase = IOCase.value(ioCase, IOCase.SENSITIVE);
9696
}
9797

9898
/**
@@ -110,7 +110,7 @@ public ExtensionFileComparator(final IOCase caseSensitivity) {
110110
public int compare(final File file1, final File file2) {
111111
final String suffix1 = FilenameUtils.getExtension(file1.getName());
112112
final String suffix2 = FilenameUtils.getExtension(file2.getName());
113-
return caseSensitivity.checkCompareTo(suffix1, suffix2);
113+
return ioCase.checkCompareTo(suffix1, suffix2);
114114
}
115115

116116
/**
@@ -120,6 +120,6 @@ public int compare(final File file1, final File file2) {
120120
*/
121121
@Override
122122
public String toString() {
123-
return super.toString() + "[caseSensitivity=" + caseSensitivity + "]";
123+
return super.toString() + "[ioCase=" + ioCase + "]";
124124
}
125125
}

src/main/java/org/apache/commons/io/comparator/NameFileComparator.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,22 +73,22 @@ public class NameFileComparator extends AbstractFileComparator implements Serial
7373
public static final Comparator<File> NAME_SYSTEM_REVERSE = new ReverseFileComparator(NAME_SYSTEM_COMPARATOR);
7474

7575
/** Whether the comparison is case sensitive. */
76-
private final IOCase caseSensitivity;
76+
private final IOCase ioCase;
7777

7878
/**
7979
* Constructs a case sensitive file name comparator instance.
8080
*/
8181
public NameFileComparator() {
82-
this.caseSensitivity = IOCase.SENSITIVE;
82+
this.ioCase = IOCase.SENSITIVE;
8383
}
8484

8585
/**
8686
* Constructs a file name comparator instance with the specified case-sensitivity.
8787
*
88-
* @param caseSensitivity how to handle case sensitivity, null means case-sensitive
88+
* @param ioCase how to handle case sensitivity, null means case-sensitive
8989
*/
90-
public NameFileComparator(final IOCase caseSensitivity) {
91-
this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
90+
public NameFileComparator(final IOCase ioCase) {
91+
this.ioCase = IOCase.value(ioCase, IOCase.SENSITIVE);
9292
}
9393

9494
/**
@@ -103,7 +103,7 @@ public NameFileComparator(final IOCase caseSensitivity) {
103103
*/
104104
@Override
105105
public int compare(final File file1, final File file2) {
106-
return caseSensitivity.checkCompareTo(file1.getName(), file2.getName());
106+
return ioCase.checkCompareTo(file1.getName(), file2.getName());
107107
}
108108

109109
/**
@@ -113,6 +113,6 @@ public int compare(final File file1, final File file2) {
113113
*/
114114
@Override
115115
public String toString() {
116-
return super.toString() + "[caseSensitivity=" + caseSensitivity + "]";
116+
return super.toString() + "[ioCase=" + ioCase + "]";
117117
}
118118
}

src/main/java/org/apache/commons/io/comparator/PathFileComparator.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,22 +73,22 @@ public class PathFileComparator extends AbstractFileComparator implements Serial
7373
public static final Comparator<File> PATH_SYSTEM_REVERSE = new ReverseFileComparator(PATH_SYSTEM_COMPARATOR);
7474

7575
/** Whether the comparison is case sensitive. */
76-
private final IOCase caseSensitivity;
76+
private final IOCase ioCase;
7777

7878
/**
7979
* Constructs a case sensitive file path comparator instance.
8080
*/
8181
public PathFileComparator() {
82-
this.caseSensitivity = IOCase.SENSITIVE;
82+
this.ioCase = IOCase.SENSITIVE;
8383
}
8484

8585
/**
8686
* Constructs a file path comparator instance with the specified case-sensitivity.
8787
*
88-
* @param caseSensitivity how to handle case sensitivity, null means case-sensitive
88+
* @param ioCase how to handle case sensitivity, null means case-sensitive
8989
*/
90-
public PathFileComparator(final IOCase caseSensitivity) {
91-
this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
90+
public PathFileComparator(final IOCase ioCase) {
91+
this.ioCase = IOCase.value(ioCase, IOCase.SENSITIVE);
9292
}
9393

9494
/**
@@ -104,7 +104,7 @@ public PathFileComparator(final IOCase caseSensitivity) {
104104
*/
105105
@Override
106106
public int compare(final File file1, final File file2) {
107-
return caseSensitivity.checkCompareTo(file1.getPath(), file2.getPath());
107+
return ioCase.checkCompareTo(file1.getPath(), file2.getPath());
108108
}
109109

110110
/**
@@ -114,6 +114,6 @@ public int compare(final File file1, final File file2) {
114114
*/
115115
@Override
116116
public String toString() {
117-
return super.toString() + "[caseSensitivity=" + caseSensitivity + "]";
117+
return super.toString() + "[ioCase=" + ioCase + "]";
118118
}
119119
}

src/main/java/org/apache/commons/io/filefilter/FileFilterUtils.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -579,13 +579,13 @@ public static IOFileFilter nameFileFilter(final String name) {
579579
* Returns a filter that returns true if the file name matches the specified text.
580580
*
581581
* @param name the file name
582-
* @param caseSensitivity how to handle case sensitivity, null means case-sensitive
582+
* @param ioCase how to handle case sensitivity, null means case-sensitive
583583
* @return a name checking filter
584584
* @see NameFileFilter
585585
* @since 2.0
586586
*/
587-
public static IOFileFilter nameFileFilter(final String name, final IOCase caseSensitivity) {
588-
return new NameFileFilter(name, caseSensitivity);
587+
public static IOFileFilter nameFileFilter(final String name, final IOCase ioCase) {
588+
return new NameFileFilter(name, ioCase);
589589
}
590590

591591
/**
@@ -644,13 +644,13 @@ public static IOFileFilter prefixFileFilter(final String prefix) {
644644
* Returns a filter that returns true if the file name starts with the specified text.
645645
*
646646
* @param prefix the file name prefix
647-
* @param caseSensitivity how to handle case sensitivity, null means case-sensitive
647+
* @param ioCase how to handle case sensitivity, null means case-sensitive
648648
* @return a prefix checking filter
649649
* @see PrefixFileFilter
650650
* @since 2.0
651651
*/
652-
public static IOFileFilter prefixFileFilter(final String prefix, final IOCase caseSensitivity) {
653-
return new PrefixFileFilter(prefix, caseSensitivity);
652+
public static IOFileFilter prefixFileFilter(final String prefix, final IOCase ioCase) {
653+
return new PrefixFileFilter(prefix, ioCase);
654654
}
655655

656656
/**
@@ -709,13 +709,13 @@ public static IOFileFilter suffixFileFilter(final String suffix) {
709709
* Returns a filter that returns true if the file name ends with the specified text.
710710
*
711711
* @param suffix the file name suffix
712-
* @param caseSensitivity how to handle case sensitivity, null means case-sensitive
712+
* @param ioCase how to handle case sensitivity, null means case-sensitive
713713
* @return a suffix checking filter
714714
* @see SuffixFileFilter
715715
* @since 2.0
716716
*/
717-
public static IOFileFilter suffixFileFilter(final String suffix, final IOCase caseSensitivity) {
718-
return new SuffixFileFilter(suffix, caseSensitivity);
717+
public static IOFileFilter suffixFileFilter(final String suffix, final IOCase ioCase) {
718+
return new SuffixFileFilter(suffix, ioCase);
719719
}
720720

721721
/**

src/main/java/org/apache/commons/io/filefilter/NameFileFilter.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public class NameFileFilter extends AbstractFileFilter implements Serializable {
7272
private final String[] names;
7373

7474
/** Whether the comparison is case sensitive. */
75-
private final IOCase caseSensitivity;
75+
private final IOCase ioCase;
7676

7777
/**
7878
* Constructs a new case-sensitive name file filter for a list of names.
@@ -89,16 +89,16 @@ public NameFileFilter(final List<String> names) {
8989
* Constructs a new name file filter for a list of names specifying case-sensitivity.
9090
*
9191
* @param names the names to allow, must not be null
92-
* @param caseSensitivity how to handle case sensitivity, null means case-sensitive
92+
* @param ioCase how to handle case sensitivity, null means case-sensitive
9393
* @throws IllegalArgumentException if the name list is null
9494
* @throws ClassCastException if the list does not contain Strings
9595
*/
96-
public NameFileFilter(final List<String> names, final IOCase caseSensitivity) {
96+
public NameFileFilter(final List<String> names, final IOCase ioCase) {
9797
if (names == null) {
9898
throw new IllegalArgumentException("The list of names must not be null");
9999
}
100100
this.names = names.toArray(EMPTY_STRING_ARRAY);
101-
this.caseSensitivity = toIOCase(caseSensitivity);
101+
this.ioCase = toIOCase(ioCase);
102102
}
103103

104104
/**
@@ -129,31 +129,31 @@ public NameFileFilter(final String... names) {
129129
* Construct a new name file filter specifying case-sensitivity.
130130
*
131131
* @param name the name to allow, must not be null
132-
* @param caseSensitivity how to handle case sensitivity, null means case-sensitive
132+
* @param ioCase how to handle case sensitivity, null means case-sensitive
133133
* @throws IllegalArgumentException if the name is null
134134
*/
135-
public NameFileFilter(final String name, final IOCase caseSensitivity) {
135+
public NameFileFilter(final String name, final IOCase ioCase) {
136136
if (name == null) {
137137
throw new IllegalArgumentException("The wildcard must not be null");
138138
}
139139
this.names = new String[] {name};
140-
this.caseSensitivity = toIOCase(caseSensitivity);
140+
this.ioCase = toIOCase(ioCase);
141141
}
142142

143143
/**
144144
* Constructs a new name file filter for an array of names specifying case-sensitivity.
145145
*
146146
* @param names the names to allow, must not be null
147-
* @param caseSensitivity how to handle case sensitivity, null means case-sensitive
147+
* @param ioCase how to handle case sensitivity, null means case-sensitive
148148
* @throws IllegalArgumentException if the names array is null
149149
*/
150-
public NameFileFilter(final String[] names, final IOCase caseSensitivity) {
150+
public NameFileFilter(final String[] names, final IOCase ioCase) {
151151
if (names == null) {
152152
throw new IllegalArgumentException("The array of names must not be null");
153153
}
154154
this.names = new String[names.length];
155155
System.arraycopy(names, 0, this.names, 0, names.length);
156-
this.caseSensitivity = toIOCase(caseSensitivity);
156+
this.ioCase = toIOCase(ioCase);
157157
}
158158

159159
/**
@@ -193,15 +193,15 @@ public FileVisitResult accept(final Path file, final BasicFileAttributes attribu
193193

194194
private boolean acceptBaseName(final String baseName) {
195195
for (final String testName : names) {
196-
if (caseSensitivity.checkEquals(baseName, testName)) {
196+
if (ioCase.checkEquals(baseName, testName)) {
197197
return true;
198198
}
199199
}
200200
return false;
201201
}
202202

203-
private IOCase toIOCase(final IOCase caseSensitivity) {
204-
return caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
203+
private IOCase toIOCase(final IOCase ioCase) {
204+
return IOCase.value(ioCase, IOCase.SENSITIVE);
205205
}
206206

207207
/**

0 commit comments

Comments
 (0)