1 | /* |
---|
2 | * This program is free software; you can redistribute it and/or modify |
---|
3 | * it under the terms of the GNU General Public License as published by |
---|
4 | * the Free Software Foundation; either version 2 of the License, or |
---|
5 | * (at your option) any later version. |
---|
6 | * |
---|
7 | * This program is distributed in the hope that it will be useful, |
---|
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
10 | * GNU General Public License for more details. |
---|
11 | * |
---|
12 | * You should have received a copy of the GNU General Public License |
---|
13 | * along with this program; if not, write to the Free Software |
---|
14 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
15 | */ |
---|
16 | |
---|
17 | /* |
---|
18 | * Version.java |
---|
19 | * Copyright (C) 2005 University of Waikato, Hamilton, New Zealand |
---|
20 | * |
---|
21 | */ |
---|
22 | |
---|
23 | package weka.core; |
---|
24 | |
---|
25 | import java.io.InputStream; |
---|
26 | import java.io.InputStreamReader; |
---|
27 | import java.io.LineNumberReader; |
---|
28 | |
---|
29 | /** |
---|
30 | * This class contains the version number of the current WEKA release and some |
---|
31 | * methods for comparing another version string. The normal layout of a |
---|
32 | * version string is "MAJOR.MINOR.REVISION", but it can also handle partial |
---|
33 | * version strings, e.g. "3.4". <br/> |
---|
34 | * Should be used e.g. in exports to XML for keeping track, with which version |
---|
35 | * of WEKA the file was produced. |
---|
36 | * |
---|
37 | * @author FracPete (fracpete at waikato dot ac dot nz) |
---|
38 | * @version $Revision: 5953 $ |
---|
39 | */ |
---|
40 | public class Version |
---|
41 | implements Comparable, RevisionHandler { |
---|
42 | |
---|
43 | /** the version file */ |
---|
44 | public final static String VERSION_FILE = "weka/core/version.txt"; |
---|
45 | |
---|
46 | /** the major version */ |
---|
47 | public static int MAJOR = 3; |
---|
48 | |
---|
49 | /** the minor version */ |
---|
50 | public static int MINOR = 4; |
---|
51 | |
---|
52 | /** the revision */ |
---|
53 | public static int REVISION = 3; |
---|
54 | |
---|
55 | static { |
---|
56 | try { |
---|
57 | InputStream inR = (new Version()).getClass().getClassLoader().getResourceAsStream(VERSION_FILE); |
---|
58 | // InputStream inR = ClassLoader.getSystemResourceAsStream(VERSION_FILE); |
---|
59 | LineNumberReader lnr = new LineNumberReader(new InputStreamReader(inR)); |
---|
60 | |
---|
61 | String line = lnr.readLine(); |
---|
62 | int[] maj = new int[1]; |
---|
63 | int[] min = new int[1]; |
---|
64 | int[] rev = new int[1]; |
---|
65 | parseVersion(line, maj, min, rev); |
---|
66 | MAJOR = maj[0]; |
---|
67 | MINOR = min[0]; |
---|
68 | REVISION = rev[0]; |
---|
69 | lnr.close(); |
---|
70 | } |
---|
71 | catch (Exception e) { |
---|
72 | System.err.println( |
---|
73 | Version.class.getName() + ": Unable to load version information!"); |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | /** the complete version */ |
---|
78 | public static String VERSION = MAJOR + "." + MINOR + "." + REVISION; |
---|
79 | |
---|
80 | /** |
---|
81 | * parses the version and stores the result in the arrays |
---|
82 | * |
---|
83 | * @param version the version string to parse (contains "-" instead of "."!) |
---|
84 | * @param maj the major version |
---|
85 | * @param min the minor version |
---|
86 | * @param rev the revision version |
---|
87 | */ |
---|
88 | private static void parseVersion(String version, int[] maj, int[] min, int[] rev) { |
---|
89 | int major = 0; |
---|
90 | int minor = 0; |
---|
91 | int revision = 0; |
---|
92 | |
---|
93 | try { |
---|
94 | String tmpStr = version; |
---|
95 | tmpStr = tmpStr.replace('-', '.'); |
---|
96 | if (tmpStr.indexOf(".") > -1) { |
---|
97 | major = Integer.parseInt(tmpStr.substring(0, tmpStr.indexOf("."))); |
---|
98 | tmpStr = tmpStr.substring(tmpStr.indexOf(".") + 1); |
---|
99 | if (tmpStr.indexOf(".") > -1) { |
---|
100 | minor = Integer.parseInt(tmpStr.substring(0, tmpStr.indexOf("."))); |
---|
101 | tmpStr = tmpStr.substring(tmpStr.indexOf(".") + 1); |
---|
102 | if (!tmpStr.equals("")) |
---|
103 | revision = Integer.parseInt(tmpStr); |
---|
104 | else |
---|
105 | revision = 0; |
---|
106 | } |
---|
107 | else { |
---|
108 | if (!tmpStr.equals("")) |
---|
109 | minor = Integer.parseInt(tmpStr); |
---|
110 | else |
---|
111 | minor = 0; |
---|
112 | } |
---|
113 | } |
---|
114 | else { |
---|
115 | if (!tmpStr.equals("")) |
---|
116 | major = Integer.parseInt(tmpStr); |
---|
117 | else |
---|
118 | major = 0; |
---|
119 | } |
---|
120 | } catch (Exception e) { |
---|
121 | e.printStackTrace(); |
---|
122 | major = -1; |
---|
123 | minor = -1; |
---|
124 | revision = -1; |
---|
125 | } finally { |
---|
126 | maj[0] = major; |
---|
127 | min[0] = minor; |
---|
128 | rev[0] = revision; |
---|
129 | } |
---|
130 | } |
---|
131 | |
---|
132 | /** |
---|
133 | * checks the version of this class against the given version-string |
---|
134 | * |
---|
135 | * @param o the version-string to compare with |
---|
136 | * @return -1 if this version is less, 0 if equal and +1 if greater |
---|
137 | * than the provided version |
---|
138 | */ |
---|
139 | public int compareTo(Object o) { |
---|
140 | int result; |
---|
141 | int major; |
---|
142 | int minor; |
---|
143 | int revision; |
---|
144 | int [] maj = new int [1]; |
---|
145 | int [] min = new int [1]; |
---|
146 | int [] rev = new int [1]; |
---|
147 | |
---|
148 | |
---|
149 | // do we have a string? |
---|
150 | if (o instanceof String) { |
---|
151 | parseVersion((String)o, maj, min, rev); |
---|
152 | major = maj[0]; |
---|
153 | minor = min[0]; |
---|
154 | revision = rev[0]; |
---|
155 | } |
---|
156 | else { |
---|
157 | System.out.println(this.getClass().getName() + ": no version-string for comparTo povided!"); |
---|
158 | major = -1; |
---|
159 | minor = -1; |
---|
160 | revision = -1; |
---|
161 | } |
---|
162 | |
---|
163 | if (MAJOR < major) { |
---|
164 | result = -1; |
---|
165 | } |
---|
166 | else if (MAJOR == major) { |
---|
167 | if (MINOR < minor) { |
---|
168 | result = -1; |
---|
169 | } |
---|
170 | else if (MINOR == minor) { |
---|
171 | if (REVISION < revision) { |
---|
172 | result = -1; |
---|
173 | } |
---|
174 | else if (REVISION == revision) { |
---|
175 | result = 0; |
---|
176 | } |
---|
177 | else { |
---|
178 | result = 1; |
---|
179 | } |
---|
180 | } |
---|
181 | else { |
---|
182 | result = 1; |
---|
183 | } |
---|
184 | } |
---|
185 | else { |
---|
186 | result = 1; |
---|
187 | } |
---|
188 | |
---|
189 | return result; |
---|
190 | } |
---|
191 | |
---|
192 | /** |
---|
193 | * whether the given version string is equal to this version |
---|
194 | * |
---|
195 | * @param o the version-string to compare to |
---|
196 | * @return TRUE if the version-string is equals to its own |
---|
197 | */ |
---|
198 | public boolean equals(Object o) { |
---|
199 | return (compareTo(o) == 0); |
---|
200 | } |
---|
201 | |
---|
202 | /** |
---|
203 | * checks whether this version is older than the one from the given |
---|
204 | * version string |
---|
205 | * |
---|
206 | * @param o the version-string to compare with |
---|
207 | * @return TRUE if this version is older than the given one |
---|
208 | */ |
---|
209 | public boolean isOlder(Object o) { |
---|
210 | return (compareTo(o) == -1); |
---|
211 | } |
---|
212 | |
---|
213 | /** |
---|
214 | * checks whether this version is newer than the one from the given |
---|
215 | * version string |
---|
216 | * |
---|
217 | * @param o the version-string to compare with |
---|
218 | * @return TRUE if this version is newer than the given one |
---|
219 | */ |
---|
220 | public boolean isNewer(Object o) { |
---|
221 | return (compareTo(o) == 1); |
---|
222 | } |
---|
223 | |
---|
224 | /** |
---|
225 | * returns the current version as string |
---|
226 | * |
---|
227 | * @return the current version |
---|
228 | */ |
---|
229 | public String toString() { |
---|
230 | return VERSION; |
---|
231 | } |
---|
232 | |
---|
233 | /** |
---|
234 | * Returns the revision string. |
---|
235 | * |
---|
236 | * @return the revision |
---|
237 | */ |
---|
238 | public String getRevision() { |
---|
239 | return RevisionUtils.extract("$Revision: 5953 $"); |
---|
240 | } |
---|
241 | |
---|
242 | /** |
---|
243 | * only for testing |
---|
244 | * |
---|
245 | * @param args the commandline arguments - ignored |
---|
246 | */ |
---|
247 | public static void main(String[] args) { |
---|
248 | Version v; |
---|
249 | String tmpStr; |
---|
250 | |
---|
251 | // print version |
---|
252 | System.out.println(VERSION + "\n"); |
---|
253 | |
---|
254 | // test on different versions |
---|
255 | v = new Version(); |
---|
256 | System.out.println("-1? " + v.compareTo("5.0.1")); |
---|
257 | System.out.println(" 0? " + v.compareTo(VERSION)); |
---|
258 | System.out.println("+1? " + v.compareTo("3.4.0")); |
---|
259 | |
---|
260 | tmpStr = "5.0.1"; |
---|
261 | System.out.println("\ncomparing with " + tmpStr); |
---|
262 | System.out.println("isOlder? " + v.isOlder(tmpStr)); |
---|
263 | System.out.println("equals ? " + v.equals(tmpStr)); |
---|
264 | System.out.println("isNewer? " + v.isNewer(tmpStr)); |
---|
265 | |
---|
266 | tmpStr = VERSION; |
---|
267 | System.out.println("\ncomparing with " + tmpStr); |
---|
268 | System.out.println("isOlder? " + v.isOlder(tmpStr)); |
---|
269 | System.out.println("equals ? " + v.equals(tmpStr)); |
---|
270 | System.out.println("isNewer? " + v.isNewer(tmpStr)); |
---|
271 | |
---|
272 | tmpStr = "3.4.0"; |
---|
273 | System.out.println("\ncomparing with " + tmpStr); |
---|
274 | System.out.println("isOlder? " + v.isOlder(tmpStr)); |
---|
275 | System.out.println("equals ? " + v.equals(tmpStr)); |
---|
276 | System.out.println("isNewer? " + v.isNewer(tmpStr)); |
---|
277 | |
---|
278 | tmpStr = "3.4"; |
---|
279 | System.out.println("\ncomparing with " + tmpStr); |
---|
280 | System.out.println("isOlder? " + v.isOlder(tmpStr)); |
---|
281 | System.out.println("equals ? " + v.equals(tmpStr)); |
---|
282 | System.out.println("isNewer? " + v.isNewer(tmpStr)); |
---|
283 | |
---|
284 | tmpStr = "5"; |
---|
285 | System.out.println("\ncomparing with " + tmpStr); |
---|
286 | System.out.println("isOlder? " + v.isOlder(tmpStr)); |
---|
287 | System.out.println("equals ? " + v.equals(tmpStr)); |
---|
288 | System.out.println("isNewer? " + v.isNewer(tmpStr)); |
---|
289 | } |
---|
290 | } |
---|