blob: 46f9e9494b240c5e0c95c0cfd60adfd8a608c923 [file] [log] [blame]
Stuart McCullochbb014372012-06-07 21:57:32 +00001package aQute.libg.version;
2
3import java.util.*;
4import java.util.regex.*;
5
6public class VersionRange {
7 Version high;
8 Version low;
9 char start = '[';
10 char end = ']';
11
12 static Pattern RANGE = Pattern.compile("(\\(|\\[)\\s*(" +
13 Version.VERSION_STRING + ")\\s*,\\s*(" +
14 Version.VERSION_STRING + ")\\s*(\\)|\\])");
15
16 public VersionRange(String string) {
17 string = string.trim();
18 Matcher m = RANGE.matcher(string);
19 if (m.matches()) {
20 start = m.group(1).charAt(0);
21 String v1 = m.group(2);
22 String v2 = m.group(10);
23 low = new Version(v1);
24 high = new Version(v2);
25 end = m.group(18).charAt(0);
26 if (low.compareTo(high) > 0)
27 throw new IllegalArgumentException(
28 "Low Range is higher than High Range: " + low + "-" +
29 high);
30
31 } else
32 high = low = new Version(string);
33 }
34
35 public boolean isRange() {
36 return high != low;
37 }
38
39 public boolean includeLow() {
40 return start == '[';
41 }
42
43 public boolean includeHigh() {
44 return end == ']';
45 }
46
47 public String toString() {
48 if (high == low)
49 return high.toString();
50
51 StringBuilder sb = new StringBuilder();
52 sb.append(start);
53 sb.append(low);
54 sb.append(',');
55 sb.append(high);
56 sb.append(end);
57 return sb.toString();
58 }
59
60 public Version getLow() {
61 return low;
62 }
63
64 public Version getHigh() {
65 return high;
66 }
67
68 public boolean includes(Version v) {
69 if ( !isRange() ) {
70 return low.compareTo(v) <=0;
71 }
72 if (includeLow()) {
73 if (v.compareTo(low) < 0)
74 return false;
75 } else if (v.compareTo(low) <= 0)
76 return false;
77
78 if (includeHigh()) {
79 if (v.compareTo(high) > 0)
80 return false;
81 } else if (v.compareTo(high) >= 0)
82 return false;
83
84 return true;
85 }
86
87 public Iterable<Version> filter( final Iterable<Version> versions) {
88 List<Version> list = new ArrayList<Version>();
89 for ( Version v : versions) {
90 if ( includes(v))
91 list.add(v);
92 }
93 return list;
94 }
95
96}