1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.module.model;
35
36
37
38
39
40
41 public class VersionRange {
42 private static final char separator = '/';
43
44 private final Version from;
45 private final Version to;
46
47 public VersionRange(String rangeDef) {
48 final int sepIdx = rangeDef.indexOf(separator);
49 if (sepIdx < 0) {
50 this.from = newVersion(rangeDef, Version.UNDEFINED_FROM);
51 this.to = newVersion(rangeDef, Version.UNDEFINED_TO);
52 } else {
53 this.from = newVersion(rangeDef.substring(0, sepIdx), Version.UNDEFINED_FROM);
54 this.to = newVersion(rangeDef.substring(sepIdx + 1), Version.UNDEFINED_TO);
55 }
56 validate();
57 }
58
59 public VersionRange(Version from, Version to) {
60 this.from = from;
61 this.to = to;
62 validate();
63 }
64
65 private Version newVersion(String rangeDef, Version ifUndefined) {
66 if ("*".equals(rangeDef.trim())) {
67 return ifUndefined;
68 }
69 return Version.parseVersion(rangeDef);
70 }
71
72 private void validate() {
73 if (from.isStrictlyAfter(to)) {
74 throw new IllegalArgumentException("Invalid range: " + from + "/" + to);
75 }
76 }
77
78 public Version getFrom() {
79 return from;
80 }
81
82 public Version getTo() {
83 return to;
84 }
85
86 public boolean contains(Version other) {
87 return other.isEquivalent(from) || (other.isStrictlyAfter(from) && other.isBeforeOrEquivalent(to));
88 }
89
90 public String toString() {
91 return from + "/" + to;
92 }
93 }