00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 package org.knopflerfish.framework;
00036
00037 import org.osgi.framework.Version;
00038
00044 public class VersionRange implements Comparable
00045 {
00046 final private Version low;
00047 final private Version high;
00048 final private boolean lowIncluded;
00049 final private boolean highIncluded;
00050
00054 public static final VersionRange defaultVersionRange = new VersionRange();
00055
00065 public VersionRange(String vr) throws NumberFormatException {
00066 boolean op = vr.startsWith("(");
00067 boolean ob = vr.startsWith("[");
00068
00069 if (op || ob) {
00070 boolean cp = vr.endsWith(")");
00071 boolean cb = vr.endsWith("]");
00072 int comma = vr.indexOf(',');
00073
00074 if (comma > 0 && (cp || cb)) {
00075 low = new Version(vr.substring(1, comma).trim());
00076 high = new Version(vr.substring(comma + 1, vr.length() - 1).trim());
00077 lowIncluded = ob;
00078 highIncluded = cb;
00079 } else {
00080 throw new NumberFormatException("Illegal version range: " + vr);
00081 }
00082 } else {
00083 low = new Version(vr);
00084 high = null;
00085 lowIncluded = true;
00086 highIncluded = false;
00087 }
00088 }
00089
00090
00095 protected VersionRange() {
00096 low = Version.emptyVersion;
00097 high = null;
00098 lowIncluded = true;
00099 highIncluded = false;
00100 }
00101
00102
00103 public boolean isSpecified() {
00104 return this != defaultVersionRange;
00105 }
00106
00107
00114 public boolean withinRange(Version ver) {
00115 if (this == defaultVersionRange) {
00116 return true;
00117 }
00118 int c = low.compareTo(ver);
00119
00120 if (c < 0 || (c == 0 && lowIncluded)) {
00121 if (high == null) {
00122 return true;
00123 }
00124 c = high.compareTo(ver);
00125 return c > 0 || (c == 0 && highIncluded);
00126 }
00127 return false;
00128 }
00129
00130
00137 public boolean withinRange(VersionRange range) {
00138 if (this == range) {
00139 return true;
00140 }
00141 int c = low.compareTo(range.low);
00142
00143 if (c < 0 || (c == 0 && lowIncluded == range.lowIncluded)) {
00144 if (high == null) {
00145 return true;
00146 }
00147 c = high.compareTo(range.high);
00148 return c > 0 || (c == 0 && highIncluded == range.highIncluded);
00149 }
00150 return false;
00151 }
00152
00153
00163 public int compareTo(Object obj) throws ClassCastException {
00164 VersionRange o = (VersionRange)obj;
00165 return low.compareTo(o.low);
00166 }
00167
00168
00175 public String toString() {
00176 if (high != null) {
00177 StringBuffer res = new StringBuffer();
00178 if (lowIncluded) {
00179 res.append('[');
00180 } else {
00181 res.append('(');
00182 }
00183 res.append(low.toString());
00184 res.append(',');
00185 res.append(high.toString());
00186 if (highIncluded) {
00187 res.append(']');
00188 } else {
00189 res.append(')');
00190 }
00191 return res.toString();
00192 } else {
00193 return low.toString();
00194 }
00195 }
00196
00197
00204 public boolean equals(Object obj) throws ClassCastException {
00205 VersionRange o = (VersionRange)obj;
00206 if (low.equals(o.low)) {
00207 if (high != null) {
00208 return high.equals(o.high) &&
00209 lowIncluded == o.lowIncluded &&
00210 highIncluded == o.highIncluded;
00211 } else {
00212 return true;
00213 }
00214 }
00215 return false;
00216 }
00217
00218
00224 public int hashCode() {
00225 if (high != null) {
00226 return low.hashCode() + high.hashCode();
00227 } else {
00228 return low.hashCode();
00229 }
00230 }
00231 }