~ubuntu-branches/ubuntu/precise/weka/precise

« back to all changes in this revision

Viewing changes to weka/core/Version.java

  • Committer: Bazaar Package Importer
  • Author(s): Soeren Sonnenburg
  • Date: 2008-02-24 09:18:45 UTC
  • Revision ID: james.westby@ubuntu.com-20080224091845-1l8zy6fm6xipbzsr
Tags: upstream-3.5.7+tut1
ImportĀ upstreamĀ versionĀ 3.5.7+tut1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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: 1.6 $ 
 
39
 */
 
40
public class Version
 
41
  implements Comparable {
 
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 = ClassLoader.getSystemResourceAsStream(VERSION_FILE);
 
58
      LineNumberReader lnr = new LineNumberReader(new InputStreamReader(inR));
 
59
      
 
60
      String line = lnr.readLine();
 
61
      int[] maj = new int[1];
 
62
      int[] min = new int[1];
 
63
      int[] rev = new int[1];
 
64
      parseVersion(line, maj, min, rev);
 
65
      MAJOR    = maj[0];
 
66
      MINOR    = min[0];
 
67
      REVISION = rev[0];
 
68
      lnr.close();
 
69
    }
 
70
    catch (Exception e) {
 
71
      System.err.println(
 
72
          Version.class.getName() + ": Unable to load version information!");
 
73
    }
 
74
  }
 
75
 
 
76
  /** the complete version */
 
77
  public static String VERSION = MAJOR + "." + MINOR + "." + REVISION;
 
78
 
 
79
  /**
 
80
   * parses the version and stores the result in the arrays
 
81
   * 
 
82
   * @param version     the version string to parse (contains "-" instead of "."!)
 
83
   * @param maj         the major version
 
84
   * @param min         the minor version
 
85
   * @param rev         the revision version
 
86
   */
 
87
  private static void parseVersion(String version, int[] maj, int[] min, int[] rev) {
 
88
    int major = 0;
 
89
    int minor = 0;
 
90
    int revision = 0;
 
91
 
 
92
    try {
 
93
      String tmpStr = version;
 
94
      tmpStr = tmpStr.replace('-', '.');
 
95
      if (tmpStr.indexOf(".") > -1) {
 
96
        major  = Integer.parseInt(tmpStr.substring(0, tmpStr.indexOf(".")));
 
97
        tmpStr = tmpStr.substring(tmpStr.indexOf(".") + 1);
 
98
        if (tmpStr.indexOf(".") > -1) {
 
99
          minor  = Integer.parseInt(tmpStr.substring(0, tmpStr.indexOf(".")));
 
100
          tmpStr = tmpStr.substring(tmpStr.indexOf(".") + 1);
 
101
          if (!tmpStr.equals(""))
 
102
            revision = Integer.parseInt(tmpStr);
 
103
          else
 
104
            revision = 0;
 
105
        }
 
106
        else {
 
107
          if (!tmpStr.equals(""))
 
108
            minor = Integer.parseInt(tmpStr);
 
109
          else
 
110
            minor = 0;
 
111
        }
 
112
      }
 
113
      else {
 
114
        if (!tmpStr.equals(""))
 
115
          major = Integer.parseInt(tmpStr);
 
116
        else
 
117
          major = 0;
 
118
      }
 
119
    } catch (Exception e) {
 
120
      e.printStackTrace();
 
121
      major    = -1;
 
122
      minor    = -1;
 
123
      revision = -1;
 
124
    } finally {
 
125
      maj[0] = major;
 
126
      min[0] = minor;
 
127
      rev[0] = revision;
 
128
    }
 
129
  }
 
130
 
 
131
  /**
 
132
   * checks the version of this class against the given version-string
 
133
   * 
 
134
   * @param o     the version-string to compare with
 
135
   * @return      -1 if this version is less, 0 if equal and +1 if greater
 
136
   *              than the provided version 
 
137
   */
 
138
  public int compareTo(Object o) {
 
139
    int       result;
 
140
    int       major;
 
141
    int       minor;
 
142
    int       revision;
 
143
    int       [] maj = new int [1];
 
144
    int       [] min = new int [1];
 
145
    int       [] rev = new int [1];
 
146
   
 
147
    
 
148
    // do we have a string?
 
149
    if (o instanceof String) {
 
150
      parseVersion((String)o, maj, min, rev);
 
151
      major = maj[0];
 
152
      minor = min[0];
 
153
      revision = rev[0];
 
154
    }
 
155
    else {
 
156
      System.out.println(this.getClass().getName() + ": no version-string for comparTo povided!");
 
157
      major    = -1;
 
158
      minor    = -1;
 
159
      revision = -1;
 
160
    }
 
161
 
 
162
    if (MAJOR < major) {
 
163
      result = -1;
 
164
    }
 
165
    else if (MAJOR == major) {
 
166
      if (MINOR < minor) {
 
167
        result = -1;
 
168
      }
 
169
      else if (MINOR == minor) {
 
170
        if (REVISION < revision) {
 
171
          result = -1;
 
172
        }
 
173
        else if (REVISION == revision) {
 
174
          result = 0;
 
175
        }
 
176
        else {
 
177
          result = 1;
 
178
        }
 
179
      }
 
180
      else {
 
181
        result = 1;
 
182
      }
 
183
    }
 
184
    else {
 
185
      result = 1;
 
186
    }
 
187
    
 
188
    return result;
 
189
  }
 
190
  
 
191
  /**
 
192
   * whether the given version string is equal to this version
 
193
   * 
 
194
   * @param o       the version-string to compare to
 
195
   * @return        TRUE if the version-string is equals to its own
 
196
   */
 
197
  public boolean equals(Object o) {
 
198
    return (compareTo(o) == 0);
 
199
  }
 
200
  
 
201
  /**
 
202
   * checks whether this version is older than the one from the given 
 
203
   * version string
 
204
   * 
 
205
   * @param o       the version-string to compare with
 
206
   * @return        TRUE if this version is older than the given one
 
207
   */
 
208
  public boolean isOlder(Object o) {
 
209
    return (compareTo(o) == -1);
 
210
  }
 
211
  
 
212
  /**
 
213
   * checks whether this version is newer than the one from the given 
 
214
   * version string
 
215
   * 
 
216
   * @param o       the version-string to compare with
 
217
   * @return        TRUE if this version is newer than the given one
 
218
   */
 
219
  public boolean isNewer(Object o) {
 
220
    return (compareTo(o) == 1);
 
221
  }
 
222
  
 
223
  /**
 
224
   * returns the current version as string
 
225
   * 
 
226
   * @return        the current version
 
227
   */
 
228
  public String toString() {
 
229
    return VERSION;
 
230
  }
 
231
  
 
232
  /**
 
233
   * only for testing
 
234
   * 
 
235
   * @param args        the commandline arguments - ignored
 
236
   */
 
237
  public static void main(String[] args) {
 
238
    Version       v;
 
239
    String        tmpStr;
 
240
 
 
241
    // print version
 
242
    System.out.println(VERSION + "\n");
 
243
    
 
244
    // test on different versions
 
245
    v = new Version();
 
246
    System.out.println("-1? " + v.compareTo("5.0.1"));
 
247
    System.out.println(" 0? " + v.compareTo(VERSION));
 
248
    System.out.println("+1? " + v.compareTo("3.4.0"));
 
249
    
 
250
    tmpStr = "5.0.1";
 
251
    System.out.println("\ncomparing with " + tmpStr);
 
252
    System.out.println("isOlder? " + v.isOlder(tmpStr));
 
253
    System.out.println("equals ? " + v.equals(tmpStr));
 
254
    System.out.println("isNewer? " + v.isNewer(tmpStr));
 
255
    
 
256
    tmpStr = VERSION;
 
257
    System.out.println("\ncomparing with " + tmpStr);
 
258
    System.out.println("isOlder? " + v.isOlder(tmpStr));
 
259
    System.out.println("equals ? " + v.equals(tmpStr));
 
260
    System.out.println("isNewer? " + v.isNewer(tmpStr));
 
261
    
 
262
    tmpStr = "3.4.0";
 
263
    System.out.println("\ncomparing with " + tmpStr);
 
264
    System.out.println("isOlder? " + v.isOlder(tmpStr));
 
265
    System.out.println("equals ? " + v.equals(tmpStr));
 
266
    System.out.println("isNewer? " + v.isNewer(tmpStr));
 
267
    
 
268
    tmpStr = "3.4";
 
269
    System.out.println("\ncomparing with " + tmpStr);
 
270
    System.out.println("isOlder? " + v.isOlder(tmpStr));
 
271
    System.out.println("equals ? " + v.equals(tmpStr));
 
272
    System.out.println("isNewer? " + v.isNewer(tmpStr));
 
273
    
 
274
    tmpStr = "5";
 
275
    System.out.println("\ncomparing with " + tmpStr);
 
276
    System.out.println("isOlder? " + v.isOlder(tmpStr));
 
277
    System.out.println("equals ? " + v.equals(tmpStr));
 
278
    System.out.println("isNewer? " + v.isNewer(tmpStr));
 
279
  }
 
280
}