Project

General

Profile

1
package eu.dnetlib.data.transform;
2

    
3
import com.google.common.collect.ImmutableList;
4
import com.google.common.collect.Ordering;
5

    
6
import eu.dnetlib.data.proto.OafProtos.Oaf;
7
import eu.dnetlib.data.proto.SpecialTrustProtos.SpecialTrust;
8
import org.apache.commons.lang3.StringUtils;
9

    
10
public class TrustOrdering extends Ordering<Oaf> {
11

    
12
	@Override
13
	public int compare(Oaf left, Oaf right) {
14
		String lTrust = left.getDataInfo().getTrust();
15
		String rTrust = right.getDataInfo().getTrust();
16

    
17
		if (lTrust.equals(rTrust)) return 0;
18

    
19
		if (lTrust.equals(SpecialTrust.INFINITE.toString())) return 1;
20
		if (rTrust.equals(SpecialTrust.INFINITE.toString())) return -1;
21

    
22
		if (lTrust.equals(SpecialTrust.NEUTRAL.toString())) return 1;
23
		if (rTrust.equals(SpecialTrust.NEUTRAL.toString())) return -1;
24

    
25
		return Float.compare(
26
				Float.parseFloat(StringUtils.isBlank(lTrust) ? "0.9" : lTrust),
27
				Float.parseFloat(StringUtils.isBlank(rTrust) ? "0.9" : rTrust));
28
	}
29

    
30
	public static ImmutableList<Oaf> sort(Iterable<Oaf> entities) {
31
		return new TrustOrdering().immutableSortedCopy(entities);
32
	}
33

    
34
}
(8-8/8)