Debian Codegraph @

bundles/org.eclipse.equinox.p2.metadata/src/org/eclipse/equinox/internal/p2/metadata/OSGiVersion.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.util.List;
17 import org.eclipse.equinox.p2.metadata.IVersionFormat;
18 import org.eclipse.equinox.p2.metadata.Version;
19 import org.eclipse.osgi.util.NLS;
20
21 /**
22 * @Immutable
23 * @noextend This class is not intended to be subclassed by clients.
24 */
25 public class OSGiVersion extends BasicVersion {
26
27 private static final long serialVersionUID = -4530178927569560877L;
28
29 private static final boolean[] allowedOSGiChars;
30
31 private final int major;
32
33 private final int minor;
34
35 private final int micro;
36
37 private final Comparable<?> qualifier;
38
39 static {
40 allowedOSGiChars = new boolean[128];
41 for (int c = '0'; c <= '9'; ++c)
42 allowedOSGiChars[c] = true;
43 for (int c = 'A'; c <= 'Z'; ++c)
44 allowedOSGiChars[c] = true;
45 for (int c = 'a'; c <= 'z'; ++c)
46 allowedOSGiChars[c] = true;
47 allowedOSGiChars['_'] = true;
48 allowedOSGiChars['-'] = true;
49 }
50
51 public static boolean isValidOSGiQualifier(Comparable<?> e) {
52 if (e == VersionVector.MAXS_VALUE)
53 return true;
54
55 if (!(e instanceof String))
56 return false;
57
58 String s = (String) e;
59 int idx = s.length();
60 boolean[] allowed = allowedOSGiChars;
61 while (--idx >= 0) {
62 int c = s.charAt(idx);
63 if (c < '-' || c > 'z' || !allowed[c])
64 return false;
65 }
66 return true;
67 }
68
69 static BasicVersion fromVector(List<Comparable<?>> vector) {
70 int vtop = vector.size() - 1;
71 Comparable<?> pad = vector.get(vtop);
72 if (vtop != 4) {
73 if (vtop == 0) {
74 if (pad == null)
75 return (BasicVersion) emptyVersion;
76 if (pad == VersionVector.MAX_VALUE)
77 return (BasicVersion) MAX_VERSION;
78 }
79 throw new IllegalArgumentException();
80 }
81 int major = ((Integer) vector.get(0)).intValue();
82 int minor = ((Integer) vector.get(1)).intValue();
83 int micro = ((Integer) vector.get(2)).intValue();
84 Comparable<?> qualifier = vector.get(3);
85 return (major == 0 && minor == 0 && micro == 0 && qualifier == VersionVector.MINS_VALUE) ? (BasicVersion) emptyVersion : new OSGiVersion(major, minor, micro, qualifier);
86 }
87
88 public OSGiVersion(int major, int minor, int micro, Comparable<? extends Object> qualifier) {
89 this.major = major;
90 this.minor = minor;
91 this.micro = micro;
92 if (!isValidOSGiQualifier(qualifier))
93 throw new IllegalArgumentException(NLS.bind(Messages._0_is_not_a_valid_qualifier_in_osgi_1, "qualifier", this)); //$NON-NLS-1$
94 //intern the qualifier string to avoid duplication
95 if (qualifier instanceof String)
96 qualifier = ((String) qualifier).intern();
97 this.qualifier = qualifier;
98 }
99
100 @Override
101 public int compareTo(Version v) {
102 int result;
103 if (!(v instanceof OSGiVersion)) {
104 BasicVersion ov = (BasicVersion) v;
105 result = VersionVector.compare(getVector(), null, ov.getVector(), ov.getPad());
106 } else {
107 OSGiVersion ov = (OSGiVersion) v;
108 result = major - ov.major;
109 if (result == 0) {
110 result = minor - ov.minor;
111 if (result == 0) {
112 result = micro - ov.micro;
113 if (result == 0)
114 result = VersionVector.compareSegments(qualifier, ov.qualifier);
115 }
116 }
117 }
118 return result;
119 }
120
121 @Override
122 public boolean equals(Object object) {
123 if (object == this)
124 return true;
125
126 if (!(object instanceof OSGiVersion)) {
127 if (object instanceof BasicVersion) {
128 BasicVersion ov = (BasicVersion) object;
129 return VersionVector.equals(getVector(), null, ov.getVector(), ov.getPad());
130 }
131 return false;
132 }
133
134 OSGiVersion other = (OSGiVersion) object;
135 return micro == other.micro && minor == other.minor && major == other.major && qualifier.equals(other.qualifier);
136 }
137
138 @Override
139 public IVersionFormat getFormat() {
140 return VersionFormat.OSGI_FORMAT;
141 }
142
143 @Override
144 public int getMajor() {
145 return major;
146 }
147
148 @Override
149 public int getMicro() {
150 return micro;
151 }
152
153 @Override
154 public int getMinor() {
155 return minor;
156 }
157
158 @Override
159 public String getOriginal() {
160 return toString();
161 }
162
163 @Override
164 public String getQualifier() {
165 return qualifier == VersionVector.MAXS_VALUE ? IVersionFormat.DEFAULT_MAX_STRING_TRANSLATION : (String) qualifier;
166 }
167
168 @Override
169 public int hashCode() {
170 return (major << 24) + (minor << 16) + (micro << 8) + qualifier.hashCode();
171 }
172
173 @Override
174 public boolean isOSGiCompatible() {
175 return true;
176 }
177
178 @Override
179 public void originalToString(StringBuffer sb, boolean rangeSafe) {
180 toString(sb);
181 }
182
183 @Override
184 public void rawToString(StringBuffer sb, boolean rangeSafe) {
185 sb.append(major);
186 sb.append('.');
187 sb.append(minor);
188 sb.append('.');
189 sb.append(micro);
190 sb.append('.');
191 sb.append('\'');
192 sb.append(qualifier);
193 sb.append('\'');
194 }
195
196 @Override
197 public void toString(StringBuffer sb) {
198 sb.append(major);
199 sb.append('.');
200 sb.append(minor);
201 sb.append('.');
202 sb.append(micro);
203 if (qualifier != VersionVector.MINS_VALUE) {
204 sb.append('.');
205 sb.append(getQualifier());
206 }
207 }
208
209 @Override
210 public Comparable<?>[] getVector() {
211 return new Comparable[] {VersionParser.valueOf(major), VersionParser.valueOf(minor), VersionParser.valueOf(micro), qualifier};
212 }
213
214 @Override
215 public Comparable<?> getPad() {
216 return null;
217 }
218
219 @Override
220 public Comparable<?> getSegment(int index) {
221 switch (index) {
222 case 0 :
223 return VersionParser.valueOf(major);
224 case 1 :
225 return VersionParser.valueOf(minor);
226 case 2 :
227 return VersionParser.valueOf(micro);
228 case 3 :
229 return qualifier;
230 }
231 throw new ArrayIndexOutOfBoundsException(index); // Not in the imaginary vector array
232 }
233
234 @Override
235 public int getSegmentCount() {
236 return 4;
237 }
238
239 private Object readResolve() {
240 OSGiVersion v = this;
241 // Preserve the empty string singleton.
242 if (qualifier.equals(VersionVector.MINS_VALUE))
243 v = new OSGiVersion(major, minor, micro, VersionVector.MINS_VALUE);
244 return v;
245 }
246 }