-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilterFirst.java
More file actions
34 lines (26 loc) · 1 KB
/
FilterFirst.java
File metadata and controls
34 lines (26 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.io.File;
import java.util.ArrayList;
import java.util.Collections; // Import the Collections utility
public class FilterFirst extends Filter {
/**
* A rule that selects the "first" file from a list of duplicates.
* It sorts the list alphabetically by path to ensure it always finds
* the true first file, regardless of input order.
*/
public FilterFirst() {
super(null, null);
}
@Override
public ArrayList<File> eval(String ignoredArgument, ArrayList<File> duplicateSet) {
ArrayList<File> fileToKeep = new ArrayList<>();
if (duplicateSet == null || duplicateSet.isEmpty()) {
return fileToKeep;
}
// The crucial correction: Sort the list first to ensure determinism.
// File's natural order is alphabetical by path.
Collections.sort(duplicateSet);
// Now, we can be certain we are selecting the true "first" file.
fileToKeep.add(duplicateSet.get(0));
return fileToKeep;
}
}