1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.itracker.model;
18
19 import java.io.Serializable;
20 import java.util.Comparator;
21
22 import org.apache.commons.lang.builder.CompareToBuilder;
23 import org.apache.commons.lang.builder.ToStringBuilder;
24
25
26
27
28
29
30
31
32
33
34 public class Version extends AbstractEntity implements Comparable<Entity> {
35
36
37
38
39 private static final long serialVersionUID = 1L;
40
41
42
43
44 private Project project;
45
46
47
48
49 private String number;
50
51
52
53
54
55
56
57 private int major;
58 private int minor;
59
60 private String description;
61
62
63
64
65
66
67
68 private Status status;
69
70
71
72
73
74
75
76 public static final Comparator<Version> VERSION_COMPARATOR = new VersionComparator();
77
78
79
80
81
82
83
84
85
86
87 public Version() {
88 }
89
90
91
92
93
94
95
96
97
98 public Version(Project project, String number) {
99 setProject(project);
100 setVersionInfo(number);
101
102
103 this.status = Status.ACTIVE;
104 }
105
106 public int getMajor() {
107 return major;
108 }
109
110 public void setMajor(int getMajor) {
111 this.major = getMajor;
112 }
113
114 public int getMinor() {
115 return minor;
116 }
117
118 public void setMinor(int getMinor) {
119 this.minor = getMinor;
120 }
121
122 public String getNumber() {
123 return number;
124 }
125
126 public void setNumber(String getNumber) {
127 this.number = getNumber;
128 }
129
130 public String getDescription() {
131 return description;
132 }
133
134 public void setDescription(String getDescription) {
135 this.description = getDescription;
136 }
137
138 public Project getProject() {
139 return project;
140 }
141
142 public void setProject(Project project) {
143 if (project == null) {
144 throw new IllegalArgumentException("null project");
145 }
146 this.project = project;
147 }
148
149
150
151
152
153
154 public Status getStatus() {
155 return status;
156 }
157
158
159
160
161
162
163
164
165
166 public void setStatus(Status status) {
167 if (status == null) {
168 throw new IllegalArgumentException("null status");
169 }
170 this.status = status;
171 }
172
173
174
175
176
177
178
179
180
181
182 public void setVersionInfo(String versionInfo) {
183 setNumber(versionInfo);
184
185 if (null == versionInfo) {
186 throw new IllegalArgumentException("version info must not be null.");
187 }
188 String versionNumber = this.number.trim();
189 int firstDot = versionNumber.indexOf('.');
190 String major = "0";
191 major = (firstDot > 0 ? versionNumber.substring(0, firstDot).trim()
192 : versionNumber.trim());
193
194 try {
195 setMajor(Integer.parseInt(major));
196 } catch (NumberFormatException ex) {
197 setMajor(0);
198 }
199
200 int secondDot = (firstDot > -1 ? versionNumber.indexOf('.',
201 firstDot + 1) : -1);
202 String minor = (secondDot > -1 ? versionNumber.substring(firstDot + 1,
203 secondDot).trim() : versionNumber.substring(firstDot + 1)
204 .trim());
205 try {
206 setMinor(Integer.parseInt(minor));
207 } catch (NumberFormatException ex) {
208 setMinor(0);
209 }
210 }
211
212
213
214
215 @Override
216 public String toString() {
217 return new ToStringBuilder(this).append("id", getId()).append("number",
218 getNumber()).append("project", getProject()).append(getMajor()).append(getMinor())
219 .append("status", getStatus()).toString();
220 }
221
222
223
224
225 public static final class VersionComparator implements Comparator<Version>, Serializable {
226
227
228
229 private static final long serialVersionUID = 1L;
230
231 private boolean ascending = true;
232
233 public VersionComparator() {
234 }
235
236 public VersionComparator(boolean ascending) {
237 setAscending(ascending);
238 }
239
240 private boolean isAscending() {
241 return ascending;
242 }
243
244 private void setAscending(boolean ascending) {
245 this.ascending = ascending;
246 }
247
248 public int compare(Version a, Version b) {
249 int result = new CompareToBuilder().append(a.getNumber(), b.getNumber())
250 .append(a.getMajor(), b.getMajor()).append(a.getMinor(), b.getMinor()).toComparison();
251
252 return (isAscending() ? result : -result);
253 }
254
255 }
256
257 }