KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cowsultants > itracker > ejb > client > models > VersionModel


1 /*
2  * This software was designed and created by Jason Carroll.
3  * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4  * The author can be reached at jcarroll@cowsultants.com
5  * ITracker website: http://www.cowsultants.com
6  * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it only under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  */

18
19 package cowsultants.itracker.ejb.client.models;
20
21 import java.util.Comparator JavaDoc;
22
23 public class VersionModel extends GenericModel implements Comparator JavaDoc {
24     private String JavaDoc number;
25     private int major;
26     private int minor;
27     private String JavaDoc description;
28     private Integer JavaDoc projectId;
29
30     public VersionModel() {
31         major = 0;
32         minor = 0;
33         number = "";
34         description = "";
35         projectId = new Integer JavaDoc(-1);
36     }
37
38     public VersionModel(Integer JavaDoc id) {
39         super(id);
40     }
41
42     public String JavaDoc getNumber() {
43         return (number == null ? "" : number);
44     }
45
46     public void setNumber(String JavaDoc value) {
47          number = value;
48     }
49
50     public int getMajor() {
51         return major;
52     }
53
54     public void setMajor(int value) {
55         major = value;
56     }
57
58     public int getMinor() {
59         return minor;
60     }
61
62     public void setMinor(int value) {
63         minor = value;
64     }
65
66     public String JavaDoc getDescription() {
67         return (description == null ? "" : description);
68     }
69
70     public void setDescription(String JavaDoc value) {
71          description = value;
72     }
73
74     public Integer JavaDoc getProjectId() {
75         return projectId;
76     }
77
78     public void setProjectId(Integer JavaDoc value) {
79         projectId = value;
80     }
81
82     /**
83       * This convience method will set the number, major and minor
84       * fields with a single call. It will first set the number to
85       * the provided data, and then attempt to parse the info if in
86       * the form major.minor into parts to set the other information.
87       * @parameter versionInfo the version number string to use
88       */

89     public void setVersionInfo(String JavaDoc versionInfo) {
90         setNumber(versionInfo);
91
92         String JavaDoc versionNumber = getNumber().trim();
93         int firstDot = versionNumber.indexOf('.');
94         String JavaDoc major = "0";
95         major = (firstDot > 0 ? versionNumber.substring(0, firstDot).trim() : versionNumber.trim());
96
97         try {
98             setMajor(Integer.parseInt(major));
99         } catch(NumberFormatException JavaDoc nfe) {
100             setMajor(0);
101         }
102
103         int secondDot = (firstDot > -1 ? versionNumber.indexOf('.', firstDot + 1) : -1);
104         String JavaDoc minor = (secondDot > -1 ? versionNumber.substring(firstDot + 1, secondDot).trim() : versionNumber.substring(firstDot + 1).trim());
105         try {
106             setMinor(Integer.parseInt(minor));
107         } catch(NumberFormatException JavaDoc nfe) {
108             setMinor(0);
109         }
110     }
111
112     public String JavaDoc toString() {
113         return "Version [" + this.getId() + "] Project: " + this.getProjectId() + " Number: " + this.getNumber();
114     }
115
116     public int compare(Object JavaDoc a, Object JavaDoc b) {
117         return this.new CompareByMajorMinor(false).compare(a, b);
118     }
119
120     public boolean equals(Object JavaDoc obj) {
121         return this.new CompareByMajorMinor(false).equals(obj);
122     }
123
124     public int hashCode() {
125         return this.new CompareByMajorMinor().hashCode();
126     }
127
128     public class CompareByMajorMinor implements Comparator JavaDoc {
129         private boolean isAscending = true;
130
131         public CompareByMajorMinor() {
132         }
133
134         public CompareByMajorMinor(boolean isAscending) {
135             setAscending(isAscending);
136         }
137
138         public void setAscending(boolean value) {
139             this.isAscending = value;
140         }
141
142         public int compare(Object JavaDoc a, Object JavaDoc b) {
143             int result = 0;
144
145             if(! (a instanceof VersionModel) || ! (b instanceof VersionModel)) {
146                 throw new ClassCastException JavaDoc();
147             }
148
149             VersionModel ma = (VersionModel) a;
150             VersionModel mb = (VersionModel) b;
151
152             if(ma.getMajor() == mb.getMajor()) {
153                 if(ma.getMinor() == mb.getMinor()) {
154                     if(ma.getNumber() == null) {
155                         result = -1;
156                     } else if(mb.getNumber() == null) {
157                         result = 1;
158                     } else {
159                         result = ma.getNumber().compareTo(mb.getNumber());
160                     }
161                 } else if(ma.getMinor() > mb.getMinor()) {
162                     result = 1;
163                 } else {
164                     result = -1;
165                 }
166             } else if(ma.getMajor() > mb.getMajor()) {
167                 result = 1;
168             } else {
169                 result = -1;
170             }
171
172             return (isAscending ? result : result * -1);
173         }
174
175         public boolean equals(Object JavaDoc obj) {
176             if(! (obj instanceof VersionModel)) {
177                 return false;
178             }
179
180             try {
181                 VersionModel mo = (VersionModel) obj;
182                 if(VersionModel.this.getMajor() == mo.getMajor() && VersionModel.this.getMinor() == mo.getMinor()) {
183                     return true;
184                 }
185             } catch(ClassCastException JavaDoc cce) {
186             }
187
188             return false;
189         }
190
191         public int hashCode() {
192             return (VersionModel.this.getMajor() ^ VersionModel.this.getMinor());
193         }
194     }
195 }
196
Popular Tags