Debian Codegraph @

bundles/org.eclipse.equinox.p2.metadata/src/org/eclipse/equinox/internal/p2/metadata/EnumDefinition.java

raw
1 /*******************************************************************************
2 * Copyright (c) 2009, 2017 Cloudsmith Inc. and others.
3 *
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
8 *
9 * SPDX-License-Identifier: EPL-2.0
10 *
11 * Contributors:
12 * Cloudsmith Inc. - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.equinox.internal.p2.metadata;
15
16 import java.io.Serializable;
17 import java.util.*;
18
19 /**
20 * Instances of this class represents the enum version format.
21 */
22 class EnumDefinition implements Comparable<EnumDefinition>, Serializable {
23 static class EnumSegment implements Comparable<EnumSegment>, Serializable {
24 private static final long serialVersionUID = 4737907767214436543L;
25
26 private final int ordinal;
27 private final EnumDefinition definition;
28
29 EnumSegment(int ordinal, EnumDefinition definition) {
30 this.ordinal = ordinal;
31 this.definition = definition;
32 }
33
34 @Override
35 public int compareTo(EnumSegment other) {
36 if (other == this)
37 return 0;
38 if (definition == other.definition)
39 // Same definition. Just compare ordinals
40 return ordinal - other.ordinal;
41
42 String thisId = definition.getIdentifier(ordinal);
43 String otherId = other.definition.getIdentifier(other.ordinal);
44 if (thisId.equals(otherId))
45 return 0;
46
47 int thisOrdinalInOther = other.definition.getOrdinal(thisId);
48 int otherOrdinalInThis = definition.getOrdinal(otherId);
49 if (thisOrdinalInOther >= 0) {
50 if (otherOrdinalInThis >= 0) {
51 // Both identifiers exists in both enums. Let's see if both
52 // enums order them the same way
53 int thisOrder = ordinal - otherOrdinalInThis;
54 int otherOrder = thisOrdinalInOther - other.ordinal;
55 if (thisOrder > 0 && otherOrder > 0)
56 return 1;
57 if (thisOrder < 0 && otherOrder < 0)
58 return -1;
59 // Difference in opinion...
60 } else {
61 // Use the order in other since it has both identifiers and
62 // this enum does not
63 return thisOrdinalInOther - other.ordinal;
64 }
65 } else if (otherOrdinalInThis >= 0) {
66 // Use the order in this since it has both identifiers and the
67 // other does not.
68 return ordinal - otherOrdinalInThis;
69 }
70
71 // We can't compare the enums since neither enum contain both identifiers
72 // or both do, but use different order. Fall back to comparing the definitions
73 return definition.compareTo(other.definition);
74 }
75
76 @Override
77 public boolean equals(Object other) {
78 return other == this || other instanceof EnumSegment && compareTo((EnumSegment) other) == 0;
79 }
80
81 @Override
82 public int hashCode() {
83 return (1 + ordinal) * 31 + definition.getIdentifier(ordinal).hashCode();
84 }
85
86 int getOrdinal() {
87 return ordinal;
88 }
89
90 String getIdentifier() {
91 return definition.getIdentifier(ordinal);
92 }
93
94 void toString(StringBuffer sb) {
95 definition.toString(sb, ordinal);
96 }
97
98 // For ligthweight deserialization
99 private Object readResolve() {
100 return definition.getSegment(ordinal);
101 }
102 }
103
104 private static final long serialVersionUID = 7237775466362654473L;
105 private static final Map<EnumDefinition, EnumSegment[]> enumDefinitionCache = new HashMap<>();
106
107 private static EnumSegment[] getEnumSegments(EnumDefinition ed) {
108 EnumSegment[] values = enumDefinitionCache.get(ed);
109 if (values == null) {
110 int ordinal = ed.identifiers.length;
111 values = new EnumSegment[ordinal];
112 while (--ordinal >= 0)
113 values[ordinal] = new EnumSegment(ordinal, ed);
114 enumDefinitionCache.put(ed, values);
115 }
116 return values;
117 }
118
119 static EnumDefinition getEnumDefinition(List<List<String>> identifiers) {
120 nextEd: for (EnumDefinition ed : enumDefinitionCache.keySet()) {
121 String[][] defs = ed.identifiers;
122 int ordinal = defs.length;
123 if (ordinal != identifiers.size())
124 continue;
125
126 while (--ordinal >= 0) {
127 String[] def = defs[ordinal];
128 List<String> ldef = identifiers.get(ordinal);
129 int idx = def.length;
130 if (ldef.size() != idx)
131 continue nextEd;
132 while (--idx >= 0)
133 if (!def[idx].equals(ldef.get(idx)))
134 continue nextEd;
135 }
136 return ed;
137 }
138 EnumDefinition ed = new EnumDefinition(identifiers);
139 getEnumSegments(ed);
140 return ed;
141 }
142
143 private final String[][] identifiers;
144 private final int longestLength;
145 private final int shortestLength;
146
147 private EnumDefinition(List<List<String>> identifiers) {
148 int ordinal = identifiers.size();
149 String[][] defs = new String[ordinal][];
150 int minLen = Integer.MAX_VALUE;
151 int maxLen = 0;
152 while (--ordinal >= 0) {
153 List<String> idents = identifiers.get(ordinal);
154 int idx = idents.size();
155 String[] def = idents.toArray(new String[idx]);
156 defs[ordinal] = def;
157 while (--idx >= 0) {
158 int idLen = def[idx].length();
159 if (idLen < minLen)
160 minLen = idLen;
161 if (idLen > maxLen)
162 maxLen = idLen;
163 }
164 }
165 this.shortestLength = minLen;
166 this.longestLength = maxLen;
167 this.identifiers = defs;
168 }
169
170 static EnumSegment getSegment(List<List<String>> identifiers, int ordinal) {
171 return new EnumDefinition(identifiers).getSegment(ordinal);
172 }
173
174 EnumSegment getSegment(int ordinal) {
175 return getEnumSegments(this)[ordinal];
176 }
177
178 /**
179 * Returns the ordinal for the given identifier
180 * @param identifier The identifier
181 * @return The ordinal of the identifier or -1 if it didn't exist.
182 */
183 int getOrdinal(String identifier) {
184 if (identifier != null) {
185 int ordinal = identifiers.length;
186 while (--ordinal >= 0) {
187 String[] idents = identifiers[ordinal];
188 int idx = idents.length;
189 while (--idx >= 0)
190 if (idents[idx].equals(identifier))
191 return ordinal;
192 }
193 }
194 return -1;
195 }
196
197 /**
198 * Returns the canonical identifier for the given ordinal.
199 * @param ordinal The ordinal number of the desired identifier
200 * @return The identifier or <code>null</code> if the ordinal is out of bounds
201 */
202 String getIdentifier(int ordinal) {
203 return ordinal >= 0 && ordinal < identifiers.length ? identifiers[ordinal][0] : null;
204 }
205
206 @Override
207 public int hashCode() {
208 int result = 1;
209 int ordinal = identifiers.length;
210 while (--ordinal > 0) {
211 String[] idents = identifiers[ordinal];
212 int idx = idents.length;
213 while (--idx >= 0)
214 result = 31 * result + idents[idx].hashCode();
215 }
216 return result;
217 }
218
219 @Override
220 public boolean equals(Object o) {
221 if (o == this)
222 return true;
223 if (!(o instanceof EnumDefinition))
224 return false;
225 String[][] otherIds = ((EnumDefinition) o).identifiers;
226 int ordinal = identifiers.length;
227 if (ordinal != otherIds.length)
228 return false;
229 while (--ordinal >= 0)
230 if (!Arrays.equals(identifiers[ordinal], otherIds[ordinal]))
231 return false;
232 return true;
233 }
234
235 @Override
236 public int compareTo(EnumDefinition o) {
237 if (o == this)
238 return 0;
239
240 int top = identifiers.length;
241 int cmp = top - o.identifiers.length;
242 if (cmp != 0)
243 return cmp;
244
245 for (int idx = 0; idx < top; ++idx) {
246 cmp = identifiers[idx][0].compareTo(o.identifiers[idx][0]);
247 if (cmp != 0)
248 return cmp;
249 }
250 // this should never happen since we use a lightweight pattern
251 return 0;
252 }
253
254 @Override
255 public String toString() {
256 StringBuffer bld = new StringBuffer();
257 toString(bld);
258 return bld.toString();
259 }
260
261 public void toString(StringBuffer bld) {
262 bld.append('{');
263 int top = identifiers.length;
264 for (int ordinal = 0;;) {
265 String[] idents = identifiers[ordinal];
266 int identsTop = idents.length;
267 bld.append(idents[0]);
268 for (int idx = 1; idx < identsTop; ++idx) {
269 bld.append('=');
270 bld.append(idents[idx]);
271 }
272 if (++ordinal == top)
273 break;
274 bld.append(',');
275 }
276 bld.append('}');
277 }
278
279 void toString(StringBuffer bld, int selectedOrdinal) {
280 bld.append('{');
281 int top = identifiers.length;
282 for (int ordinal = 0;;) {
283 if (ordinal == selectedOrdinal)
284 bld.append('^');
285 bld.append(identifiers[ordinal][0]);
286 if (++ordinal == top)
287 break;
288 bld.append(',');
289 }
290 bld.append('}');
291 }
292
293 /**
294 * Returns the length of the longest identifier. This method is
295 * used by the parser
296 * @return The length of the longest identifier
297 */
298 int getLongestLength() {
299 return longestLength;
300 }
301
302 /**
303 * Returns the length of the shortest identifier. This method is
304 * used by the parser
305 * @return The length of the shortest identifier
306 */
307 int getShortestLength() {
308 return shortestLength;
309 }
310 }