]> git.argeo.org Git - gpl/argeo-suite.git/blob - core/org.argeo.suite.core/src/org/argeo/suite/RankingKey.java
Fix remove empty paragraphs.
[gpl/argeo-suite.git] / core / org.argeo.suite.core / src / org / argeo / suite / RankingKey.java
1 package org.argeo.suite;
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 @Deprecated
10 public class RankingKey implements Comparable<RankingKey> {
11 public final static String SERVICE_PID = "service.pid";
12 public final static String SERVICE_ID = "service.id";
13 public final static String SERVICE_RANKING = "service.ranking";
14 public final static String DATA_TYPE = "data.type";
15
16 private String pid;
17 private Integer ranking = 0;
18 private Long id = 0l;
19 private String dataType;
20 private String dataPath;
21
22 public RankingKey(String pid, Integer ranking, Long id, String dataType, String dataPath) {
23 super();
24 this.pid = pid;
25 this.ranking = ranking;
26 this.id = id;
27 this.dataType = dataType;
28 this.dataPath = dataPath;
29 }
30
31 public RankingKey(Map<String, Object> properties) {
32 this.pid = properties.containsKey(SERVICE_PID) ? properties.get(SERVICE_PID).toString() : null;
33 this.ranking = properties.containsKey(SERVICE_RANKING)
34 ? Integer.parseInt(properties.get(SERVICE_RANKING).toString())
35 : 0;
36 this.id = properties.containsKey(SERVICE_ID) ? (Long) properties.get(SERVICE_ID) : null;
37
38 // Argeo specific
39 this.dataType = properties.containsKey(DATA_TYPE) ? properties.get(DATA_TYPE).toString() : 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 }