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