View Javadoc
1   package org.argeo.connect;
2   
3   import java.util.Map;
4   
5   public class ServiceRanking implements Comparable<ServiceRanking> {
6   	public final static String SERVICE_ID = "service.id";
7   	public final static String SERVICE_RANKING = "service.ranking";
8   
9   	private final Long id;
10  	private final Integer ranking;
11  
12  	public ServiceRanking(Map<String, Object> properties) {
13  		this((Long) properties.get(SERVICE_ID),
14  				properties.containsKey(SERVICE_RANKING) ? (Integer) properties.get(SERVICE_RANKING) : 0);
15  	}
16  
17  	public ServiceRanking(Long id, Integer ranking) {
18  		if (id == null)
19  			throw new IllegalArgumentException("Service id cannot be null");
20  		if (ranking == null)
21  			throw new IllegalArgumentException("Service ranking cannot be null");
22  		this.id = id;
23  		this.ranking = ranking;
24  	}
25  
26  	@Override
27  	public int hashCode() {
28  		return id.intValue();
29  	}
30  
31  	@Override
32  	public boolean equals(Object obj) {
33  		if (!(obj instanceof ServiceRanking))
34  			return false;
35  		ServiceRanking o = (ServiceRanking) obj;
36  		return id == o.id;
37  	}
38  
39  	/**
40  	 * Inverted with the canonical order, so that the highest ranking services come
41  	 * first.
42  	 */
43  	@Override
44  	public int compareTo(ServiceRanking o) {
45  		if (ranking == o.ranking)
46  			return id.compareTo(o.id);
47  		else
48  			return o.ranking.compareTo(ranking);
49  	}
50  
51  	@Override
52  	protected Object clone() throws CloneNotSupportedException {
53  		return new ServiceRanking(id, ranking);
54  	}
55  
56  	@Override
57  	public String toString() {
58  		return ranking + "-" + id;
59  	}
60  
61  }