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
42
43 public class VersionRange {
44 private static final char separator = '/';
45
46 private final Version from;
47 private final Version to;
48
49 public VersionRange(String input) {
50 final String rangeDef = input == null ? "*" : input;
51 final int sepIdx = rangeDef.indexOf(separator);
52 if (sepIdx < 0) {
53 this.from = newVersion(rangeDef, Version.UNDEFINED_FROM);
54 this.to = newVersion(rangeDef, Version.UNDEFINED_TO);
55 } else {
56 this.from = newVersion(rangeDef.substring(0, sepIdx), Version.UNDEFINED_FROM);
57 this.to = newVersion(rangeDef.substring(sepIdx + 1), Version.UNDEFINED_TO);
58 }
59 validate();
60 }
61
62 public VersionRange(Version from, Version to) {
63 this.from = from;
64 this.to = to;
65 validate();
66 }
67
68 private Version newVersion(String rangeDef, Version ifUndefined) {
69 if ("*".equals(rangeDef.trim())) {
70 return ifUndefined;
71 }
72 return Version.parseVersion(rangeDef);
73 }
74
75 private void validate() {
76 if (from.isStrictlyAfter(to)) {
77 throw new IllegalArgumentException("Invalid range: " + from + "/" + to);
78 }
79 }
80
81 public Version getFrom() {
82 return from;
83 }
84
85 public Version getTo() {
86 return to;
87 }
88
89 public boolean contains(Version other) {
90 return other.isEquivalent(from) || (other.isStrictlyAfter(from) && other.isBeforeOrEquivalent(to));
91 }
92
93 @Override
94 public String toString() {
95 return from + "/" + to;
96 }
97 }