Clover Coverage Report
Coverage timestamp: Fri May 24 2013 13:47:27 UTC
../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
42   200   27   3
24   86   0.64   14
14     1.93  
1    
 
  Version       Line # 25 42 27 100% 1.0
 
  (98)
 
1    /* This file is part of the project "Hilbert II" - http://www.qedeq.org
2    *
3    * Copyright 2000-2013, Michael Meyling <mime@qedeq.org>.
4    *
5    * "Hilbert II" is free software; you can redistribute
6    * it and/or modify it under the terms of the GNU General Public
7    * License as published by the Free Software Foundation; either
8    * version 2 of the License, or (at your option) any later version.
9    *
10    * This program is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13    * GNU General Public License for more details.
14    */
15   
16    package org.qedeq.base.io;
17   
18   
19    /**
20    * A version number implementation oriented at the standard:
21    * <a href="http://semver.org/">http://semver.org</a>.
22    *
23    * @author Michael Meyling
24    */
 
25    public final class Version implements Comparable {
26   
27    /** Major version number. */
28    private final int major;
29   
30    /** Minor version number. */
31    private final int minor;
32   
33    /** Patch version number. */
34    private final int patch;
35   
36    /**
37    * Constructs version object. You must give a version string in a form like
38    * <em>a</em>.<em>b</em>.<em>c</em> where a, b and c are non negative integers.
39    * These numbers are called <em>Major</em> <em>Minor</em> and <em>Patch</em>.
40    *
41    * @param version Version string.
42    * @throws IllegalArgumentException Version string has wrong format.
43    * @throws NullPointerException No null pointer as argument accepted.
44    */
 
45  4054 toggle public Version(final String version) {
46  4054 final TextInput text = new TextInput(version);
47  4051 major = text.readNonNegativeInt();
48  4041 if (!".".equals(text.readString(1))) {
49  2 throw new IllegalArgumentException("version number must have two digits");
50    }
51  4039 minor = text.readNonNegativeInt();
52  4037 if (!".".equals(text.readString(1))) {
53  2 throw new IllegalArgumentException("version number must have two digits");
54    }
55  4035 patch = text.readNonNegativeInt();
56  4033 text.skipWhiteSpace();
57  4033 if (!text.isEmpty()) {
58  5 throw new IllegalArgumentException("version number to long: " + text.readString(100));
59    }
60    }
61   
62    /**
63    * Get major version number.
64    *
65    * @return Major version number.
66    */
 
67  6 toggle public int getMajor() {
68  6 return major;
69    }
70   
71    /**
72    * Get minor version number.
73    *
74    * @return Minor version number.
75    */
 
76  6 toggle public int getMinor() {
77  6 return minor;
78    }
79   
80    /**
81    * Get patch number.
82    *
83    * @return Patch version number.
84    */
 
85  6 toggle public int getPatch() {
86  6 return patch;
87    }
88   
 
89  1814 toggle public int compareTo(final Object o) {
90  1814 if (!(o instanceof Version)) {
91  1 return -1;
92    }
93  1813 final Version other = (Version) o;
94  1813 if (major < other.major) {
95  9 return -1;
96  1804 } else if (major > other.major) {
97  27 return 1;
98    }
99  1777 if (minor < other.minor) {
100  87 return -1;
101  1690 } else if (minor > other.minor) {
102  589 return 1;
103    }
104  1101 if (patch < other.patch) {
105  5 return -1;
106  1096 } else if (patch > other.patch) {
107  7 return 1;
108    }
109  1089 return 0;
110    }
111   
 
112  16 toggle public int hashCode() {
113  16 return major ^ minor ^ patch;
114    }
115   
 
116  734 toggle public boolean equals(final Object o) {
117  734 return 0 == compareTo(o);
118    }
119   
120    /**
121    * Notes the given string the same version?
122    *
123    * @param version String version number.
124    * @return Are both versions equal?
125    */
 
126  573 toggle public boolean equals(final String version) {
127  573 Version compare = null;
128  573 try {
129  573 compare = new Version(version);
130    } catch (RuntimeException e) {
131    // ignore
132    }
133  573 return equals(compare);
134    }
135   
 
136  37 toggle public String toString() {
137  37 return major + "." + (minor < 10 ? "0" : "") + minor
138  37 + "." + (patch < 10 ? "0" : "") + patch;
139    }
140   
141    /**
142    * Is this version number less than the given other?
143    *
144    * @param other Compare with this number.
145    * @return Less?
146    */
 
147  86 toggle public boolean isLess(final Version other) {
148  86 return 0 > compareTo(other);
149    }
150   
151    /**
152    * Is this version number bigger than the given other?
153    *
154    * @param other Compare with this number.
155    * @return Bigger?
156    */
 
157  21 toggle public boolean isBigger(final Version other) {
158  21 return 0 < compareTo(other);
159    }
160   
161    /**
162    * Is <code>version1</code> &lt; <code>version2</code>?
163    *
164    * @param version1 First operand. Must be valid version pattern.
165    * @param version2 Second operand. Must be valid version pattern.
166    * @return Less?
167    * @throws IllegalArgumentException No valid version pattern.
168    * @throws NullPointerException No null pointer as argument accepted.
169    */
 
170  70 toggle public static boolean less(final String version1, final String version2) {
171  70 return (new Version(version1)).isLess(new Version(version2));
172    }
173   
174    /**
175    * Is <code>version1</code> &gt; <code>version2</code>?
176    *
177    * @param version1 First operand. Must be valid version pattern.
178    * @param version2 Second operand. Must be valid version pattern.
179    * @return Less?
180    * @throws IllegalArgumentException No valid version pattern.
181    * @throws NullPointerException No null pointer as argument accepted.
182    */
 
183  5 toggle public static boolean bigger(final String version1, final String version2) {
184  5 return (new Version(version1)).isBigger(new Version(version2));
185    }
186   
187    /**
188    * Is <code>version1</code> == <code>version2</code>?
189    *
190    * @param version1 First operand. Must be valid version pattern.
191    * @param version2 Second operand. Must be valid version pattern.
192    * @return Less?
193    * @throws IllegalArgumentException No valid version pattern.
194    * @throws NullPointerException No null pointer as argument accepted.
195    */
 
196  139 toggle public static boolean equals(final String version1, final String version2) {
197  139 return (new Version(version1)).equals(new Version(version2));
198    }
199   
200    }