blob: 8c9a8ddbe76868657faaa3b3bed4ae612005bb5f [file] [log] [blame]
package aQute.bnd.version;
import java.util.*;
import java.util.regex.*;
public class VersionRange {
Version high;
Version low;
char start = '[';
char end = ']';
static Pattern RANGE = Pattern.compile("(\\(|\\[)\\s*(" + Version.VERSION_STRING + ")\\s*,\\s*("
+ Version.VERSION_STRING + ")\\s*(\\)|\\])");
public VersionRange(String string) {
string = string.trim();
Matcher m = RANGE.matcher(string);
if (m.matches()) {
start = m.group(1).charAt(0);
String v1 = m.group(2);
String v2 = m.group(10);
low = new Version(v1);
high = new Version(v2);
end = m.group(18).charAt(0);
if (low.compareTo(high) > 0)
throw new IllegalArgumentException("Low Range is higher than High Range: " + low + "-" + high);
} else
high = low = new Version(string);
}
public boolean isRange() {
return high != low;
}
public boolean includeLow() {
return start == '[';
}
public boolean includeHigh() {
return end == ']';
}
@Override
public String toString() {
if (high == low)
return high.toString();
StringBuilder sb = new StringBuilder();
sb.append(start);
sb.append(low);
sb.append(',');
sb.append(high);
sb.append(end);
return sb.toString();
}
public Version getLow() {
return low;
}
public Version getHigh() {
return high;
}
public boolean includes(Version v) {
if (!isRange()) {
return low.compareTo(v) <= 0;
}
if (includeLow()) {
if (v.compareTo(low) < 0)
return false;
} else if (v.compareTo(low) <= 0)
return false;
if (includeHigh()) {
if (v.compareTo(high) > 0)
return false;
} else if (v.compareTo(high) >= 0)
return false;
return true;
}
public Iterable<Version> filter(final Iterable<Version> versions) {
List<Version> list = new ArrayList<Version>();
for (Version v : versions) {
if (includes(v))
list.add(v);
}
return list;
}
}