KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > service > resolver > VersionRange


1 /*******************************************************************************
2  * Copyright (c) 2003, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.osgi.service.resolver;
12
13 import org.osgi.framework.Version;
14
15 /**
16  * This class represents a version range.
17  * @since 3.1
18  */

19 public class VersionRange {
20     private static final Version versionMax = new Version(Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE);
21     /**
22      * An empty version
23      */

24     public static final VersionRange emptyRange = new VersionRange(null);
25
26     private Version minVersion;
27     private boolean includeMin;
28     private Version maxVersion;
29     private boolean includeMax;
30
31     /**
32      * Constructs a VersionRange with the specified minVersion and maxVersion.
33      * @param minVersion the minimum version of the range
34      * @param maxVersion the maximum version of the range
35      */

36     public VersionRange(Version minVersion, boolean includeMin, Version maxVersion, boolean includeMax) {
37         this.minVersion = minVersion;
38         this.includeMin = includeMin;
39         this.maxVersion = maxVersion;
40         this.includeMax = includeMax;
41     }
42
43     /**
44      * Constructs a VersionRange from the given versionRange String.
45      * @param versionRange a version range String that specifies a range of
46      * versions.
47      */

48     public VersionRange(String JavaDoc versionRange) {
49         if (versionRange == null || versionRange.length() == 0) {
50             minVersion = Version.emptyVersion;
51             includeMin = true;
52             maxVersion = VersionRange.versionMax;
53             includeMax = true;
54             return;
55         }
56         versionRange = versionRange.trim();
57         if (versionRange.charAt(0) == '[' || versionRange.charAt(0) == '(') {
58             int comma = versionRange.indexOf(',');
59             if (comma < 0)
60                 throw new IllegalArgumentException JavaDoc();
61             char last = versionRange.charAt(versionRange.length() - 1);
62             if (last != ']' && last != ')')
63                 throw new IllegalArgumentException JavaDoc();
64
65             minVersion = Version.parseVersion(versionRange.substring(1, comma).trim());
66             includeMin = versionRange.charAt(0) == '[';
67             maxVersion = Version.parseVersion(versionRange.substring(comma + 1, versionRange.length() - 1).trim());
68             includeMax = last == ']';
69         } else {
70             minVersion = Version.parseVersion(versionRange.trim());
71             includeMin = true;
72             maxVersion = VersionRange.versionMax;
73             includeMax = true;
74         }
75     }
76
77     /**
78      * Returns the minimum Version of this VersionRange
79      * @return the minimum Version of this VersionRange
80      */

81     public Version getMinimum() {
82         return minVersion;
83     }
84
85     /**
86      * Indicates if the minimum version is included in the version range.
87      * @return true if the minimum version is included in the version range;
88      * otherwise false is returned
89      */

90     public boolean getIncludeMinimum() {
91         return includeMin;
92     }
93
94     /**
95      * Returns the maximum Version of this VersionRange
96      * @return the maximum Version of this VersionRange
97      */

98     public Version getMaximum() {
99         return maxVersion;
100     }
101
102     /**
103      * Indicates if the maximum version is included in the version range.
104      * @return true if the maximum version is included in the version range;
105      * otherwise false is returned
106      */

107     public boolean getIncludeMaximum() {
108         return includeMax;
109     }
110
111     /**
112      * Returns whether the given version is included in this VersionRange.
113      * This will depend on the minimum and maximum versions of this VersionRange
114      * and the given version.
115      *
116      * @param version a version to be tested for inclusion in this VersionRange.
117      * (may be <code>null</code>)
118      * @return <code>true</code> if the version is include,
119      * <code>false</code> otherwise
120      */

121     public boolean isIncluded(Version version) {
122         Version minRequired = getMinimum();
123         if (minRequired == null)
124             return true;
125         if (version == null)
126             return false;
127         Version maxRequired = getMaximum() == null ? VersionRange.versionMax : getMaximum();
128         int minCheck = includeMin ? 0 : 1;
129         int maxCheck = includeMax ? 0 : -1;
130         return version.compareTo(minRequired) >= minCheck && version.compareTo(maxRequired) <= maxCheck;
131
132     }
133
134     public boolean equals(Object JavaDoc object) {
135         if (!(object instanceof VersionRange))
136             return false;
137         VersionRange vr = (VersionRange) object;
138         if (minVersion != null && vr.getMinimum() != null) {
139             if (minVersion.equals(vr.getMinimum()) && includeMin == vr.includeMin)
140                 if (maxVersion != null && vr.getMaximum() != null) {
141                     if (maxVersion.equals(vr.getMaximum()) && includeMax == vr.includeMax)
142                         return true;
143                 }
144                 else
145                     return maxVersion == vr.getMaximum();
146         }
147         else {
148             return minVersion == vr.getMinimum();
149         }
150         return false;
151     }
152
153     public String JavaDoc toString() {
154         if (minVersion == null)
155             return Version.emptyVersion.toString();
156         if (VersionRange.versionMax.equals(maxVersion))
157             return minVersion.toString();
158         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
159         result.append(includeMin ? '[' : '(');
160         result.append(minVersion);
161         result.append(',');
162         result.append(maxVersion);
163         result.append(includeMax ? ']' : ')');
164         return result.toString();
165     }
166 }
167
Popular Tags