]> git.argeo.org Git - lgpl/argeo-commons.git/blob - RankingKey.java
c475c6733312096e5488629f8befde8afc394e00
[lgpl/argeo-commons.git] / RankingKey.java
1 package org.argeo.api;
2
3 import java.util.Map;
4
5 /**
6 * Key used to classify and filter available components (typically provided by
7 * OSGi services).
8 */
9 public class RankingKey implements Comparable<RankingKey> {
10 public final static String SERVICE_PID = "service.pid";
11 public final static String SERVICE_ID = "service.id";
12 public final static String SERVICE_RANKING = "service.ranking";
13
14 private String pid;
15 private Integer ranking = 0;
16 private Long id = 0l;
17 private String dataType;
18 private String dataPath;
19
20 public RankingKey(String pid, Integer ranking, Long id, String dataType, String dataPath) {
21 super();
22 this.pid = pid;
23 this.ranking = ranking;
24 this.id = id;
25 this.dataType = dataType;
26 this.dataPath = dataPath;
27 }
28
29 public RankingKey(Map<String, Object> properties) {
30 this.pid = properties.containsKey(SERVICE_PID) ? properties.get(SERVICE_PID).toString() : null;
31 this.ranking = properties.containsKey(SERVICE_RANKING)
32 ? Integer.parseInt(properties.get(SERVICE_RANKING).toString())
33 : 0;
34 this.id = properties.containsKey(SERVICE_ID) ? (Long) properties.get(SERVICE_ID) : null;
35
36 // Argeo specific
37 this.dataType = properties.containsKey(NodeConstants.DATA_TYPE)
38 ? properties.get(NodeConstants.DATA_TYPE).toString()
39 : null;
40 }
41
42 @Override
43 public int hashCode() {
44 Integer result = 0;
45 if (pid != null)
46 result = +pid.hashCode();
47 if (ranking != null)
48 result = +ranking;
49 if (dataType != null)
50 result = +dataType.hashCode();
51 return result;
52 }
53
54 @Override
55 protected Object clone() throws CloneNotSupportedException {
56 return new RankingKey(pid, ranking, id, dataType, dataPath);
57 }
58
59 @Override
60 public String toString() {
61 StringBuilder sb = new StringBuilder("");
62 if (pid != null)
63 sb.append(pid);
64 if (ranking != null && ranking != 0)
65 sb.append(' ').append(ranking);
66 if (dataType != null)
67 sb.append(' ').append(dataType);
68 return sb.toString();
69 }
70
71 @Override
72 public boolean equals(Object obj) {
73 if (!(obj instanceof RankingKey))
74 return false;
75 RankingKey other = (RankingKey) obj;
76 return equalsOrBothNull(pid, other.pid) && equalsOrBothNull(ranking, other.ranking)
77 && equalsOrBothNull(id, other.id) && equalsOrBothNull(dataType, other.dataType)
78 && equalsOrBothNull(dataPath, other.dataPath);
79 }
80
81 @Override
82 public int compareTo(RankingKey o) {
83 if (pid != null && o.pid != null) {
84 if (pid.equals(o.pid)) {
85 if (ranking.equals(o.ranking))
86 if (id != null && o.id != null)
87 return id.compareTo(o.id);
88 else
89 return 0;
90 else
91 return ranking.compareTo(o.ranking);
92 } else {
93 return pid.compareTo(o.pid);
94 }
95
96 } else {
97 if (dataType != null && o.dataType != null) {
98 if (dataType.equals(o.dataType)) {
99 // TODO factorise
100 if (ranking.equals(o.ranking))
101 if (id != null && o.id != null)
102 return id.compareTo(o.id);
103 else
104 return 0;
105 else
106 return ranking.compareTo(o.ranking);
107 } else {
108 return dataPath.compareTo(o.dataType);
109 }
110 }
111 }
112 return -1;
113 }
114
115 public String getPid() {
116 return pid;
117 }
118
119 public Integer getRanking() {
120 return ranking;
121 }
122
123 public Long getId() {
124 return id;
125 }
126
127 public String getDataType() {
128 return dataType;
129 }
130
131 public String getDataPath() {
132 return dataPath;
133 }
134
135 public static RankingKey minPid(String pid) {
136 return new RankingKey(pid, Integer.MIN_VALUE, null, null, null);
137 }
138
139 public static RankingKey maxPid(String pid) {
140 return new RankingKey(pid, Integer.MAX_VALUE, null, null, null);
141 }
142
143 public static RankingKey minDataType(String dataType) {
144 return new RankingKey(null, Integer.MIN_VALUE, null, dataType, null);
145 }
146
147 public static RankingKey maxDataType(String dataType) {
148 return new RankingKey(null, Integer.MAX_VALUE, null, dataType, null);
149 }
150
151 private static boolean equalsOrBothNull(Object o1, Object o2) {
152 if (o1 == null && o2 == null)
153 return true;
154 if (o1 == null && o2 != null)
155 return false;
156 if (o1 != null && o2 == null)
157 return false;
158 return o2.equals(o1);
159 }
160 }