Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a680b01311736fe27014970026e94e8ddf5d7b16 (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
/*******************************************************************************
 * Copyright (c) 2003, 2008 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.osgi.service.resolver;

import org.osgi.framework.Version;

/**
 * This class represents a version range.
 * @since 3.1
 * @noextend This class is not intended to be subclassed by clients.
 */
public class VersionRange {
	private static final Version versionMax = new Version(Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE);
	/**
	 * An empty version
	 */
	public static final VersionRange emptyRange = new VersionRange(null);

	private Version minVersion;
	private boolean includeMin;
	private Version maxVersion;
	private boolean includeMax;

	/**
	 * Constructs a VersionRange with the specified minVersion and maxVersion.
	 * @param minVersion the minimum version of the range
	 * @param maxVersion the maximum version of the range
	 */
	public VersionRange(Version minVersion, boolean includeMin, Version maxVersion, boolean includeMax) {
		this.minVersion = minVersion;
		this.includeMin = includeMin;
		this.maxVersion = maxVersion;
		this.includeMax = includeMax;
	}

	/**
	 * Constructs a VersionRange from the given versionRange String.
	 * @param versionRange a version range String that specifies a range of
	 * versions.
	 */
	public VersionRange(String versionRange) {
		if (versionRange == null || versionRange.length() == 0) {
			minVersion = Version.emptyVersion;
			includeMin = true;
			maxVersion = VersionRange.versionMax;
			includeMax = true;
			return;
		}
		versionRange = versionRange.trim();
		if (versionRange.charAt(0) == '[' || versionRange.charAt(0) == '(') {
			int comma = versionRange.indexOf(',');
			if (comma < 0)
				throw new IllegalArgumentException();
			char last = versionRange.charAt(versionRange.length() - 1);
			if (last != ']' && last != ')')
				throw new IllegalArgumentException();

			minVersion = Version.parseVersion(versionRange.substring(1, comma).trim());
			includeMin = versionRange.charAt(0) == '[';
			maxVersion = Version.parseVersion(versionRange.substring(comma + 1, versionRange.length() - 1).trim());
			includeMax = last == ']';
		} else {
			minVersion = Version.parseVersion(versionRange.trim());
			includeMin = true;
			maxVersion = VersionRange.versionMax;
			includeMax = true;
		}
	}

	/**
	 * Returns the minimum Version of this VersionRange
	 * @return the minimum Version of this VersionRange
	 */
	public Version getMinimum() {
		return minVersion;
	}

	/**
	 * Indicates if the minimum version is included in the version range.
	 * @return true if the minimum version is included in the version range;
	 * otherwise false is returned
	 */
	public boolean getIncludeMinimum() {
		return includeMin;
	}

	/**
	 * Returns the maximum Version of this VersionRange
	 * @return the maximum Version of this VersionRange
	 */
	public Version getMaximum() {
		return maxVersion;
	}

	/**
	 * Indicates if the maximum version is included in the version range.
	 * @return true if the maximum version is included in the version range;
	 * otherwise false is returned
	 */
	public boolean getIncludeMaximum() {
		return includeMax;
	}

	/**
	 * Returns whether the given version is included in this VersionRange.
	 * This will depend on the minimum and maximum versions of this VersionRange
	 * and the given version.
	 * 
	 * @param version a version to be tested for inclusion in this VersionRange. 
	 * (may be <code>null</code>)
	 * @return <code>true</code> if the version is include, 
	 * <code>false</code> otherwise 
	 */
	public boolean isIncluded(Version version) {
		Version minRequired = getMinimum();
		if (minRequired == null)
			return true;
		if (version == null)
			return false;
		Version maxRequired = getMaximum() == null ? VersionRange.versionMax : getMaximum();
		int minCheck = includeMin ? 0 : 1;
		int maxCheck = includeMax ? 0 : -1;
		return version.compareTo(minRequired) >= minCheck && version.compareTo(maxRequired) <= maxCheck;

	}

	public boolean equals(Object object) {
		if (!(object instanceof VersionRange))
			return false;
		VersionRange vr = (VersionRange) object;
		if (minVersion != null && vr.getMinimum() != null) {
			if (minVersion.equals(vr.getMinimum()) && includeMin == vr.includeMin)
				if (maxVersion != null && vr.getMaximum() != null) {
					if (maxVersion.equals(vr.getMaximum()) && includeMax == vr.includeMax)
						return true;
				} else
					return maxVersion == vr.getMaximum();
		} else {
			return minVersion == vr.getMinimum();
		}
		return false;
	}

	public String toString() {
		if (minVersion == null)
			return Version.emptyVersion.toString();
		if (VersionRange.versionMax.equals(maxVersion))
			return minVersion.toString();
		StringBuffer result = new StringBuffer();
		result.append(includeMin ? '[' : '(');
		result.append(minVersion);
		result.append(',');
		result.append(maxVersion);
		result.append(includeMax ? ']' : ')');
		return result.toString();
	}
}

Back to the top