1 package org.codehaus.modello.model;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import org.codehaus.modello.ModelloRuntimeException;
26 import org.codehaus.plexus.util.StringUtils;
27
28
29
30
31
32
33
34 public class Version implements Comparable<Version> {
35 public static final Version INFINITE = new Version("32767.32767.32767");
36
37 private short major;
38
39 private short minor;
40
41 private short micro;
42
43 public Version(String version) {
44 if (version == null) {
45 throw new ModelloRuntimeException("Syntax error in the version field: Missing. ");
46 }
47
48 String[] splittedVersion = StringUtils.split(version.trim(), ".");
49
50 if (splittedVersion.length > 3) {
51 throw new ModelloRuntimeException(
52 "Syntax error in the <version> field: The field must be at more 3 parts long (major, minor and micro). Was: '"
53 + version + "'.");
54 }
55
56 String majorString = splittedVersion[0];
57
58 String minorString = "0";
59
60 String microString = "0";
61
62 if (splittedVersion.length > 1) {
63 minorString = splittedVersion[1];
64
65 if (splittedVersion.length > 2) {
66 microString = splittedVersion[2];
67 }
68 }
69
70 try {
71 major = Short.parseShort(majorString);
72
73 minor = Short.parseShort(minorString);
74
75 micro = Short.parseShort(microString);
76 } catch (NumberFormatException e) {
77 throw new ModelloRuntimeException("Invalid version string: '" + version + "'.");
78 }
79 }
80
81 public int getMajor() {
82 return major;
83 }
84
85 public int getMinor() {
86 return minor;
87 }
88
89 public int getMicro() {
90 return micro;
91 }
92
93
94
95
96
97
98
99 public boolean greaterThan(Version other) {
100 if (this.major != other.major) {
101 return major > other.major;
102 }
103
104 if (this.minor != other.minor) {
105 return this.minor > other.minor;
106 }
107
108 if (this.micro != other.micro) {
109 return this.micro > other.micro;
110 }
111
112 return false;
113 }
114
115
116
117
118
119
120
121 public boolean greaterOrEqualsThan(Version other) {
122 if (this.major != other.major) {
123 return major >= other.major;
124 }
125
126 if (this.minor != other.minor) {
127 return this.minor >= other.minor;
128 }
129
130 if (this.micro != other.micro) {
131 return this.micro >= other.micro;
132 }
133
134 return false;
135 }
136
137
138
139
140
141
142
143 public boolean lesserThan(Version other) {
144 if (this.major != other.major) {
145 return major < other.major;
146 }
147
148 if (this.minor != other.minor) {
149 return this.minor < other.minor;
150 }
151
152 if (this.micro != other.micro) {
153 return this.micro < other.micro;
154 }
155
156 return false;
157 }
158
159
160
161
162
163
164
165 public boolean lesserOrEqualsThan(Version other) {
166 if (this.major != other.major) {
167 return major <= other.major;
168 }
169
170 if (this.minor != other.minor) {
171 return this.minor <= other.minor;
172 }
173
174 if (this.micro != other.micro) {
175 return this.micro <= other.micro;
176 }
177
178 return false;
179 }
180
181 public boolean inside(VersionRange range) {
182 if (range.getFromVersion().equals(this)) {
183 return true;
184 } else if ((this.greaterThan(range.getFromVersion())) && (this.lesserThan(range.getToVersion()))) {
185 return true;
186 } else if (this.equals(range.getFromVersion()) || this.equals(range.getToVersion())) {
187 return true;
188 }
189
190 return false;
191 }
192
193
194
195
196
197 public boolean equals(Object object) {
198 if (!(object instanceof Version)) {
199 return false;
200 }
201
202 Version other = (Version) object;
203
204 return this.major == other.major && this.minor == other.minor && this.micro == other.micro;
205 }
206
207 public int hashCode() {
208 return toString("", null).hashCode();
209 }
210
211 public String toString() {
212 return toString("", ".");
213 }
214
215 public String toString(String prefix, String separator) {
216 return prefix + major + separator + minor + separator + micro;
217 }
218
219 public int compareTo(Version otherVersion) {
220 if (greaterThan(otherVersion)) {
221 return +1;
222 } else if (equals(otherVersion)) {
223 return 0;
224 } else {
225 return -1;
226 }
227 }
228 }