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