Debian Codegraph @

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

raw
1 /*******************************************************************************
2 * Copyright (c) 2009, 2018 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.util.Collections;
17 import java.util.List;
18 import org.eclipse.equinox.p2.metadata.IVersionFormat;
19 import org.eclipse.equinox.p2.metadata.Version;
20
21 /**
22 * <p>The Generic Omni Version is composed of a vector of Comparable objects and a pad value. The pad
23 * might be <code>null</code>. The vector can contain integers, strings, {@link VersionVector}
24 * instances, or one of the special objects {@link VersionVector#MAX_VALUE MAX_VALUE},
25 * {@link VersionVector#MAXS_VALUE MAXS_VALUE}, or {@link VersionVector#MIN_VALUE MIN_VALUE}.</p>
26 *
27 * <p>When two versions are compared, they are always considered padded to infinity by their
28 * pad value or by {@link VersionVector#MIN_VALUE MIN_VALUE} in case the pad value is
29 * <code>null</code>. The comparison is type sensitive so that:</p><pre>
30 * MAX_VALUE &gt; Integer &gt; VersionVector &gt; MAXS_VALUE &gt; String &gt; MIN_VALUE<br/>
31 * </pre>
32 *
33 * @Immutable
34 * @noextend This class is not intended to be subclassed by clients.
35 */
36 public class OmniVersion extends BasicVersion {
37 private static final long serialVersionUID = 1996212688810048879L;
38
39 private static OmniVersion minimumVersion;
40
41 private static OmniVersion maximumVersion;
42
43 private static final Comparable<?>[] emptyVector = new Comparable<?>[0];
44
45 private final Comparable<?>[] vector;
46
47 private final Comparable<?> padValue;
48
49 /**
50 * The optional format
51 */
52 private final IVersionFormat format;
53
54 /**
55 * The optional original string
56 */
57 private final String original;
58
59 static BasicVersion fromVector(List<Comparable<?>> vector, IVersionFormat format, String original) {
60 int vtop = vector.size() - 1;
61 Comparable<?> padValue = vector.get(vtop);
62 if (vtop == 0) {
63 if (padValue == null)
64 return (BasicVersion) emptyVersion;
65 if (padValue == VersionVector.MAX_VALUE)
66 return (BasicVersion) MAX_VERSION;
67 }
68 if (vtop == 3 && padValue == null && vector.get(0) == (Integer) 0 && vector.get(1) == (Integer) 0 && vector.get(2) == (Integer) 0)
69 return (BasicVersion) emptyVersion;
70
71 return new OmniVersion(vector, format, original);
72 }
73
74 public static Version createMinVersion() {
75 if (minimumVersion == null)
76 minimumVersion = new OmniVersion(Collections.singletonList(null), null, null);
77 return minimumVersion;
78 }
79
80 public static Version createMaxVersion() {
81 if (maximumVersion == null)
82 maximumVersion = new OmniVersion(Collections.singletonList(VersionVector.MAX_VALUE), null, null);
83 return maximumVersion;
84 }
85
86 private OmniVersion(List<Comparable<?>> vector, IVersionFormat format, String original) {
87 int vtop = vector.size() - 1;
88 if (vtop > 0) {
89 Comparable<?>[] v = new Comparable<?>[vtop];
90 for (int idx = 0; idx < vtop; ++idx)
91 v[idx] = vector.get(idx);
92 this.vector = v;
93 } else
94 this.vector = emptyVector;
95 this.padValue = vector.get(vtop);
96 this.format = format;
97 this.original = original;
98 }
99
100 @Override
101 public boolean equals(Object o) {
102 if (o == this)
103 return true;
104
105 if (!(o instanceof BasicVersion))
106 return false;
107
108 BasicVersion ov = (BasicVersion) o;
109 return VersionVector.equals(vector, padValue, ov.getVector(), ov.getPad());
110 }
111
112 @Override
113 public IVersionFormat getFormat() {
114 return format;
115 }
116
117 @Override
118 public int getMajor() {
119 return getIntElement(0);
120 }
121
122 @Override
123 public int getMicro() {
124 return getIntElement(2);
125 }
126
127 @Override
128 public int getMinor() {
129 return getIntElement(1);
130 }
131
132 @Override
133 public String getOriginal() {
134 return original;
135 }
136
137 @Override
138 public String getQualifier() {
139 if (vector.length == 3)
140 return VersionVector.MINS_VALUE;
141
142 if (vector.length != 4)
143 throw new UnsupportedOperationException();
144
145 Comparable<?> qualifier = vector[3];
146 if (qualifier == VersionVector.MAXS_VALUE)
147 return IVersionFormat.DEFAULT_MAX_STRING_TRANSLATION;
148 if (!(qualifier instanceof String))
149 throw new UnsupportedOperationException();
150 return (String) qualifier;
151 }
152
153 @Override
154 public int hashCode() {
155 return VersionVector.hashCode(vector, padValue);
156 }
157
158 /**
159 * Checks if this version is in compliance with the OSGi version spec.
160 * @return A flag indicating whether the version is OSGi compatible or not.
161 */
162 @Override
163 public boolean isOSGiCompatible() {
164 if (vector.length < 3 || vector.length > 4)
165 return (this == emptyVersion || this == MAX_VERSION);
166
167 if (getPad() != null)
168 return false;
169
170 for (int i = 0; i < 3; ++i) {
171 Object e = vector[i];
172 if (!(e instanceof Integer && ((Integer) e).intValue() >= 0))
173 return false;
174 }
175
176 if (vector.length == 3)
177 return true; // No qualifier. Still compatible
178 return OSGiVersion.isValidOSGiQualifier(vector[3]);
179 }
180
181 /**
182 * Appends the original for this version onto the <code>sb</code> StringBuffer
183 * if present.
184 * @param sb The buffer that will receive the raw string format
185 * @param rangeSafe Set to <code>true</code> if range delimiters should be escaped
186 */
187 @Override
188 public void originalToString(StringBuffer sb, boolean rangeSafe) {
189 if (original != null) {
190 if (rangeSafe) {
191 // Escape all range delimiters while appending
192 String s = original;
193 int end = s.length();
194 for (int idx = 0; idx < end; ++idx) {
195 char c = s.charAt(idx);
196 if (c == '\\' || c == '[' || c == '(' || c == ']' || c == ')' || c == ',' || c <= ' ')
197 sb.append('\\');
198 sb.append(c);
199 }
200 } else
201 sb.append(original);
202 }
203 }
204
205 /**
206 * Appends the raw format for this version onto the <code>sb</code> StringBuffer.
207 * @param sb The buffer that will receive the raw string format
208 * @param rangeSafe Set to <code>true</code> if range delimiters should be escaped
209 */
210 @Override
211 public void rawToString(StringBuffer sb, boolean rangeSafe) {
212 VersionVector.toString(sb, vector, padValue, rangeSafe);
213 }
214
215 /**
216 * Appends the string representation of this version onto the
217 * <code>sb</code> StringBuffer.
218 * @param sb The buffer that will receive the version string
219 */
220 @Override
221 public void toString(StringBuffer sb) {
222 if (this == emptyVersion)
223 sb.append("0.0.0"); //$NON-NLS-1$
224 else {
225 sb.append(RAW_PREFIX);
226 VersionVector.toString(sb, vector, padValue, false);
227 if (format != null || original != null) {
228 sb.append('/');
229 if (format != null)
230 format.toString(sb);
231 if (original != null) {
232 sb.append(':');
233 originalToString(sb, false);
234 }
235 }
236 }
237 }
238
239 private int getIntElement(int i) {
240 if (!(vector.length > i && vector[i] instanceof Integer))
241 throw new UnsupportedOperationException();
242 return ((Integer) vector[i]).intValue();
243 }
244
245 // Preserve singletons during deserialization
246 private Object readResolve() {
247 Version v = this;
248 if (equals(MAX_VERSION))
249 v = MAX_VERSION;
250 else if (equals(emptyVersion))
251 v = emptyVersion;
252 return v;
253 }
254
255 @Override
256 public Comparable<?> getPad() {
257 return padValue;
258 }
259
260 @Override
261 public Comparable<?> getSegment(int index) {
262 return vector[index];
263 }
264
265 @Override
266 public int getSegmentCount() {
267 return vector.length;
268 }
269
270 @Override
271 Comparable<?>[] getVector() {
272 return vector;
273 }
274
275 @Override
276 public int compareTo(Version v) {
277 BasicVersion ov = (BasicVersion) v;
278 return VersionVector.compare(vector, padValue, ov.getVector(), ov.getPad());
279 }
280 }