1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  package org.itracker.model;
18  
19  import org.apache.commons.lang.builder.CompareToBuilder;
20  import org.apache.commons.lang.builder.ToStringBuilder;
21  
22  import java.io.Serializable;
23  import java.util.Comparator;
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      public Version(Project project, String number) {
97          setProject(project);
98          setVersionInfo(number);
99  
100         
101         this.status = Status.ACTIVE;
102     }
103 
104     public int getMajor() {
105         return major;
106     }
107 
108     public void setMajor(int getMajor) {
109         this.major = getMajor;
110     }
111 
112     public int getMinor() {
113         return minor;
114     }
115 
116     public void setMinor(int getMinor) {
117         this.minor = getMinor;
118     }
119 
120     public String getNumber() {
121         return number;
122     }
123 
124     public void setNumber(String getNumber) {
125         this.number = getNumber;
126     }
127 
128     public String getDescription() {
129         return description;
130     }
131 
132     public void setDescription(String getDescription) {
133         this.description = getDescription;
134     }
135 
136     public Project getProject() {
137         return project;
138     }
139 
140     public void setProject(Project project) {
141         if (project == null) {
142             throw new IllegalArgumentException("null project");
143         }
144         this.project = project;
145     }
146 
147     
148 
149 
150 
151 
152     public Status getStatus() {
153         return status;
154     }
155 
156     
157 
158 
159 
160 
161 
162     public void setStatus(Status status) {
163         if (status == null) {
164             throw new IllegalArgumentException("null status");
165         }
166         this.status = status;
167     }
168 
169     
170 
171 
172 
173 
174 
175 
176 
177     public void setVersionInfo(String versionInfo) {
178         setNumber(versionInfo);
179 
180         if (null == versionInfo) {
181             throw new IllegalArgumentException("version info must not be null.");
182         }
183         String versionNumber = this.number.trim();
184         int firstDot = versionNumber.indexOf('.');
185         String major = "0";
186         major = (firstDot > 0 ? versionNumber.substring(0, firstDot).trim()
187                 : versionNumber.trim());
188 
189         try {
190             setMajor(Integer.parseInt(major));
191         } catch (NumberFormatException ex) {
192             setMajor(0);
193         }
194 
195         int secondDot = (firstDot > -1 ? versionNumber.indexOf('.',
196                 firstDot + 1) : -1);
197         String minor = (secondDot > -1 ? versionNumber.substring(firstDot + 1,
198                 secondDot).trim() : versionNumber.substring(firstDot + 1)
199                 .trim());
200         try {
201             setMinor(Integer.parseInt(minor));
202         } catch (NumberFormatException ex) {
203             setMinor(0);
204         }
205     }
206 
207     
208 
209 
210     @Override
211     public String toString() {
212         return new ToStringBuilder(this).append("id", getId()).append("number",
213                 getNumber()).append("project", getProject()).append(getMajor()).append(getMinor())
214                 .append("status", getStatus()).toString();
215     }
216 
217     
218 
219 
220     public static final class VersionComparator implements Comparator<Version>, Serializable {
221         
222 
223 
224         private static final long serialVersionUID = 1L;
225 
226         private boolean ascending = true;
227 
228         public VersionComparator() {
229         }
230 
231         public VersionComparator(boolean ascending) {
232             setAscending(ascending);
233         }
234 
235         private boolean isAscending() {
236             return ascending;
237         }
238 
239         private void setAscending(boolean ascending) {
240             this.ascending = ascending;
241         }
242 
243         public int compare(Versionhref="../../../org/itracker/model/Version.html#Version">Version a, Version b) {
244             int result = new CompareToBuilder().append(a.getNumber(), b.getNumber())
245                     .append(a.getMajor(), b.getMajor()).append(a.getMinor(), b.getMinor()).toComparison();
246 
247             return (isAscending() ? result : -result);
248         }
249 
250     }
251 
252 }