Skip to main content
summaryrefslogtreecommitdiffstats
blob: d58c0b6aec4a5ad41e58e2164268093a4e54e5f2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*******************************************************************************
 * Copyright (c) 2009 Cloudsmith Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Cloudsmith Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.internal.p2.metadata;

import org.eclipse.equinox.internal.provisional.p2.metadata.IVersionFormat;
import org.eclipse.equinox.internal.provisional.p2.metadata.Version;

/**
 * <p>The Generic Omni Version is composed of a vector of Comparable objects and a pad value. The pad
 * might be <code>null</code>. The vector can contain integers, strings, {@link VersionVector}
 * instances, or one of the special objects {@link VersionVector#MAX_VALUE MAX_VALUE},
 * {@link VersionVector#MAXS_VALUE MAXS_VALUE}, or {@link VersionVector#MIN_VALUE MIN_VALUE}.</p>
 *
 * <p>When two versions are compared, they are always considered padded to infinity by their
 * pad value or by {@link VersionVector#MIN_VALUE MIN_VALUE} in case the pad value is
 * <code>null</code>. The comparison is type sensitive so that:</p><pre>
 * MAX_VALUE &gt; Integer &gt; VersionVector &gt; MAXS_VALUE &gt; String &gt; MIN_VALUE<br/>
 * </pre>
 *
 * @Immutable
 * @noextend This class is not intended to be subclassed by clients.
 */
public class OmniVersion extends BasicVersion {
	private static final long serialVersionUID = 1996212688810048879L;

	private static OmniVersion minimumVersion;

	private static OmniVersion maximumVersion;

	private final Comparable<?>[] vector;

	private final Comparable<?> padValue;

	/**
	 * The optional format
	 */
	private final IVersionFormat format;

	/**
	 * The optional original string
	 */
	private final String original;

	static BasicVersion fromVector(Comparable<?>[] vector, Comparable<?> padValue, IVersionFormat format, String original) {
		if (vector.length == 0) {
			if (padValue == null)
				return (BasicVersion) emptyVersion;
			if (padValue == VersionVector.MAX_VALUE)
				return (BasicVersion) MAX_VERSION;
		}
		if (vector.length == 3 && padValue == null && vector[0] == VersionParser.ZERO_INT && vector[1] == VersionParser.ZERO_INT && vector[2] == VersionParser.ZERO_INT)
			return (BasicVersion) emptyVersion;

		return new OmniVersion(vector, padValue, format, original);
	}

	public static Version createMinVersion() {
		if (minimumVersion == null)
			minimumVersion = new OmniVersion(new Comparable[0], null, null, null);
		return minimumVersion;
	}

	public static Version createMaxVersion() {
		if (maximumVersion == null)
			maximumVersion = new OmniVersion(new Comparable[0], VersionVector.MAX_VALUE, null, null);
		return maximumVersion;
	}

	private OmniVersion(Comparable<?>[] array, Comparable<?> padValue, IVersionFormat format, String original) {
		this.vector = array;
		this.padValue = padValue;
		this.format = format;
		this.original = original;
	}

	public boolean equals(Object o) {
		if (o == this)
			return true;

		if (!(o instanceof BasicVersion))
			return false;

		BasicVersion ov = (BasicVersion) o;
		return VersionVector.equals(vector, padValue, ov.getVector(), ov.getPad());
	}

	public IVersionFormat getFormat() {
		return format;
	}

	public int getMajor() {
		return getIntElement(0);
	}

	public int getMicro() {
		return getIntElement(2);
	}

	public int getMinor() {
		return getIntElement(1);
	}

	public String getOriginal() {
		return original;
	}

	public String getQualifier() {
		if (vector.length == 3)
			return VersionVector.MINS_VALUE;

		if (vector.length != 4)
			throw new UnsupportedOperationException();

		Comparable<?> qualifier = vector[3];
		if (qualifier == VersionVector.MAXS_VALUE)
			return IVersionFormat.DEFAULT_MAX_STRING_TRANSLATION;
		if (!(qualifier instanceof String))
			throw new UnsupportedOperationException();
		return (String) qualifier;
	}

	public int hashCode() {
		return VersionVector.hashCode(vector, padValue);
	}

	/**
	 * Checks if this version is in compliance with the OSGi version spec.
	 * @return A flag indicating whether the version is OSGi compatible or not.
	 */
	public boolean isOSGiCompatible() {
		if (vector.length < 3 || vector.length > 4)
			return (this == emptyVersion || this == MAX_VERSION);

		if (getPad() != null)
			return false;

		for (int i = 0; i < 3; ++i) {
			Object e = vector[i];
			if (!(e instanceof Integer && ((Integer) e).intValue() >= 0))
				return false;
		}

		if (vector.length == 3)
			return true; // No qualifier. Still compatible
		return OSGiVersion.isValidOSGiQualifier(vector[3]);
	}

	/**
	 * Appends the original for this version onto the <code>sb</code> StringBuffer
	 * if present.
	 * @param sb The buffer that will receive the raw string format
	 * @param rangeSafe Set to <code>true</code> if range delimiters should be escaped
	 */
	public void originalToString(StringBuffer sb, boolean rangeSafe) {
		if (original != null) {
			if (rangeSafe) {
				// Escape all range delimiters while appending
				String s = original;
				int end = s.length();
				for (int idx = 0; idx < end; ++idx) {
					char c = s.charAt(idx);
					if (c == '\\' || c == '[' || c == '(' || c == ']' || c == ')' || c == ',' || c <= ' ')
						sb.append('\\');
					sb.append(c);
				}
			} else
				sb.append(original);
		}
	}

	/**
	 * Appends the raw format for this version onto the <code>sb</code> StringBuffer.
	 * @param sb The buffer that will receive the raw string format
	 * @param rangeSafe Set to <code>true</code> if range delimiters should be escaped
	 */
	public void rawToString(StringBuffer sb, boolean rangeSafe) {
		VersionVector.toString(sb, vector, padValue, rangeSafe);
	}

	/**
	 * Appends the string representation of this version onto the
	 * <code>sb</code> StringBuffer.
	 * @param sb The buffer that will receive the version string
	 */
	public void toString(StringBuffer sb) {
		if (this == emptyVersion)
			sb.append("0.0.0"); //$NON-NLS-1$
		else {
			sb.append(RAW_PREFIX);
			VersionVector.toString(sb, vector, padValue, false);
			if (format != null || original != null) {
				sb.append('/');
				if (format != null)
					format.toString(sb);
				if (original != null) {
					sb.append(':');
					originalToString(sb, false);
				}
			}
		}
	}

	private int getIntElement(int i) {
		if (!(vector.length > i && vector[i] instanceof Integer))
			throw new UnsupportedOperationException();
		return ((Integer) vector[i]).intValue();
	}

	// Preserve singletons during deserialization
	private Object readResolve() {
		Version v = this;
		if (equals(MAX_VERSION))
			v = MAX_VERSION;
		else if (equals(emptyVersion))
			v = emptyVersion;
		return v;
	}

	public Comparable<?> getPad() {
		return padValue;
	}

	public Comparable<?> getSegment(int index) {
		return vector[index];
	}

	public int getSegmentCount() {
		return vector.length;
	}

	Comparable<?>[] getVector() {
		return vector;
	}

	public int compareTo(Version v) {
		BasicVersion ov = (BasicVersion) v;
		return VersionVector.compare(vector, padValue, ov.getVector(), ov.getPad());
	}
}

Back to the top