1 11 package org.eclipse.osgi.service.resolver; 12 13 import org.osgi.framework.Version; 14 15 19 public class VersionRange { 20 private static final Version versionMax = new Version(Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE); 21 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 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 48 public VersionRange(String 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 (); 61 char last = versionRange.charAt(versionRange.length() - 1); 62 if (last != ']' && last != ')') 63 throw new IllegalArgumentException (); 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 81 public Version getMinimum() { 82 return minVersion; 83 } 84 85 90 public boolean getIncludeMinimum() { 91 return includeMin; 92 } 93 94 98 public Version getMaximum() { 99 return maxVersion; 100 } 101 102 107 public boolean getIncludeMaximum() { 108 return includeMax; 109 } 110 111 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 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 toString() { 154 if (minVersion == null) 155 return Version.emptyVersion.toString(); 156 if (VersionRange.versionMax.equals(maxVersion)) 157 return minVersion.toString(); 158 StringBuffer result = new StringBuffer (); 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 |