Skip to content

Commit daa2688

Browse files
author
Gary Gregory
committed
Internal refactoring
1 parent aa73cb8 commit daa2688

1 file changed

Lines changed: 17 additions & 31 deletions

File tree

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

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,20 @@
1919
import java.io.File;
2020
import java.io.Serializable;
2121
import java.util.Comparator;
22+
import java.util.stream.Stream;
2223
import java.util.stream.StreamSupport;
2324

2425
/**
2526
* Compare two files using a set of delegate file {@link Comparator}.
2627
* <p>
27-
* This comparator can be used to sort lists or arrays of files
28-
* by combining a number of other comparators.
28+
* This comparator can be used to sort lists or arrays of files by combining a number of other comparators.
2929
* <p>
30-
* Example of sorting a list of files by type (i.e. directory or file)
31-
* and then by name:
30+
* Example of sorting a list of files by type (i.e. directory or file) and then by name:
31+
*
3232
* <pre>
33-
* CompositeFileComparator comparator =
34-
* new CompositeFileComparator(
35-
* (AbstractFileComparator) DirectoryFileComparator.DIRECTORY_COMPARATOR,
36-
* (AbstractFileComparator) NameFileComparator.NAME_COMPARATOR);
33+
* CompositeFileComparator comparator = new CompositeFileComparator(
34+
* DirectoryFileComparator.DIRECTORY_COMPARATOR,
35+
* NameFileComparator.NAME_COMPARATOR);
3736
* List&lt;File&gt; list = ...
3837
* comparator.sort(list);
3938
* </pre>
@@ -52,47 +51,34 @@ public class CompositeFileComparator extends AbstractFileComparator implements S
5251
*
5352
* @param delegates The delegate file comparators
5453
*/
55-
@SuppressWarnings("unchecked") // casts 1 & 2 must be OK because types are already correct
56-
public CompositeFileComparator(final Comparator<File>... delegates) {
57-
if (delegates == null) {
58-
this.delegates = (Comparator<File>[]) EMPTY_COMPARATOR_ARRAY; //1
59-
} else {
60-
this.delegates = delegates.clone(); //2
61-
}
54+
public CompositeFileComparator(@SuppressWarnings("unchecked") final Comparator<File>... delegates) {
55+
this.delegates = delegates == null ? emptyArray() : delegates.clone();
6256
}
6357

6458
/**
6559
* Constructs a composite comparator for the set of delegate comparators.
6660
*
6761
* @param delegates The delegate file comparators
6862
*/
69-
@SuppressWarnings("unchecked") // casts 1 & 2 must be OK because types are already correct
7063
public CompositeFileComparator(final Iterable<Comparator<File>> delegates) {
71-
if (delegates == null) {
72-
this.delegates = (Comparator<File>[]) EMPTY_COMPARATOR_ARRAY; //1
73-
} else {
74-
this.delegates = StreamSupport.stream(delegates.spliterator(), false).toArray(Comparator[]::new);
75-
}
64+
this.delegates = delegates == null ? emptyArray() : StreamSupport.stream(delegates.spliterator(), false).toArray(Comparator[]::new);
7665
}
7766

7867
/**
7968
* Compares the two files using delegate comparators.
8069
*
8170
* @param file1 The first file to compare
8271
* @param file2 The second file to compare
83-
* @return the first non-zero result returned from
84-
* the delegate comparators or zero.
72+
* @return the first non-zero result returned from the delegate comparators or zero.
8573
*/
8674
@Override
8775
public int compare(final File file1, final File file2) {
88-
int result = 0;
89-
for (final Comparator<File> delegate : delegates) {
90-
result = delegate.compare(file1, file2);
91-
if (result != 0) {
92-
break;
93-
}
94-
}
95-
return result;
76+
return Stream.of(delegates).map(delegate -> delegate.compare(file1, file2)).filter(r -> r != 0).findFirst().orElse(0);
77+
}
78+
79+
@SuppressWarnings("unchecked") // types are already correct
80+
private Comparator<File>[] emptyArray() {
81+
return (Comparator<File>[]) EMPTY_COMPARATOR_ARRAY;
9682
}
9783

9884
/**

0 commit comments

Comments
 (0)