blob: cbae2697b63b470064e33b16bd36bc73d0829aee (
plain)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
/*
* Created on Aug 22, 2008
*/
package bug;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public abstract class NaturallyComparablePruner<T extends NaturallyComparable, O> {
public List<? extends T> prune(List<? extends T> list, List<O> order){
Map<Object, List<T>> sep = new HashMap<Object, List<T>>();
for(T obj : list){
Object id = obj.getNaturalId();
List<T> matches = sep.get(id);
if(matches == null){
matches = new ArrayList<T>();
sep.put(id, matches);
}
if(matches.size() == 0)
matches.add(obj);
else{
T match = matches.get(0);
O yours = getOrdering(match);
O mine = getOrdering(obj);
if(EqualityUtils.equal(mine, yours))
matches.add(obj);
else{
int yourIndex = order.indexOf(yours);
int myIndex = order.indexOf(mine);
if(myIndex < yourIndex){
matches.clear();
matches.add(obj);
}
}
}
}
List<T> result = new ArrayList<T>();
for(List<T> values : sep.values())
result.addAll(values);
return result;
}
public abstract O getOrdering(T object);
}
|