|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Version | Line # 25 | 42 | 27 | 100% |
1.0
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
(18) | |||
Result | |||
0.575
|
org.qedeq.base.io.VersionTest.testEquals
![]() |
1 PASS | |
0.5375
|
org.qedeq.base.io.VersionTest.testConstructors
![]() |
1 PASS | |
0.5125
|
org.qedeq.base.io.VersionSetTest.testAddAndContainsClearIsEmpty
![]() |
1 PASS | |
0.5125
|
org.qedeq.base.io.VersionSetTest.testAddAndEquals
![]() |
1 PASS | |
0.5
|
org.qedeq.base.io.VersionTest.testEquals2
![]() |
1 PASS | |
0.4875
|
org.qedeq.base.io.VersionTest.testEquals3
![]() |
1 PASS | |
0.4875
|
org.qedeq.base.io.VersionSetTest.testConstructors
![]() |
1 PASS | |
0.4875
|
org.qedeq.base.io.VersionTest.testBigger
![]() |
1 PASS | |
0.4625
|
org.qedeq.base.io.VersionTest.testLess
![]() |
1 PASS | |
0.4375
|
org.qedeq.base.io.VersionSetTest.testToString
![]() |
1 PASS | |
0.4125
|
org.qedeq.base.io.VersionSetTest.testIterator
![]() |
1 PASS | |
0.4125
|
org.qedeq.base.io.VersionSetTest.testHashCode
![]() |
1 PASS | |
0.3375
|
org.qedeq.base.io.VersionTest.testIsBigger
![]() |
1 PASS | |
0.3375
|
org.qedeq.base.io.VersionTest.testIsLess
![]() |
1 PASS | |
0.3125
|
org.qedeq.base.io.VersionTest.testCompareTo
![]() |
1 PASS | |
0.225
|
org.qedeq.base.io.VersionTest.testToString
![]() |
1 PASS | |
0.225
|
org.qedeq.base.io.VersionTest.testGetters
![]() |
1 PASS | |
0.175
|
org.qedeq.base.io.VersionTest.testHashCode
![]() |
1 PASS | |
1 | /* This file is part of the project "Hilbert II" - http://www.qedeq.org | |
2 | * | |
3 | * Copyright 2000-2014, 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 | 190 |
![]() |
46 | 190 | final TextInput text = new TextInput(version); |
47 | 187 | major = text.readNonNegativeInt(); |
48 | 177 | if (!".".equals(text.readString(1))) { |
49 | 2 | throw new IllegalArgumentException("version number must have two digits"); |
50 | } | |
51 | 175 | minor = text.readNonNegativeInt(); |
52 | 173 | if (!".".equals(text.readString(1))) { |
53 | 2 | throw new IllegalArgumentException("version number must have two digits"); |
54 | } | |
55 | 171 | patch = text.readNonNegativeInt(); |
56 | 169 | text.skipWhiteSpace(); |
57 | 169 | 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 |
![]() |
68 | 6 | return major; |
69 | } | |
70 | ||
71 | /** | |
72 | * Get minor version number. | |
73 | * | |
74 | * @return Minor version number. | |
75 | */ | |
76 | 6 |
![]() |
77 | 6 | return minor; |
78 | } | |
79 | ||
80 | /** | |
81 | * Get patch number. | |
82 | * | |
83 | * @return Patch version number. | |
84 | */ | |
85 | 6 |
![]() |
86 | 6 | return patch; |
87 | } | |
88 | ||
89 | 157 |
![]() |
90 | 157 | if (!(o instanceof Version)) { |
91 | 1 | return -1; |
92 | } | |
93 | 156 | final Version other = (Version) o; |
94 | 156 | if (major < other.major) { |
95 | 9 | return -1; |
96 | 147 | } else if (major > other.major) { |
97 | 24 | return 1; |
98 | } | |
99 | 123 | if (minor < other.minor) { |
100 | 23 | return -1; |
101 | 100 | } else if (minor > other.minor) { |
102 | 33 | return 1; |
103 | } | |
104 | 67 | if (patch < other.patch) { |
105 | 5 | return -1; |
106 | 62 | } else if (patch > other.patch) { |
107 | 7 | return 1; |
108 | } | |
109 | 55 | return 0; |
110 | } | |
111 | ||
112 | 16 |
![]() |
113 | 16 | return major ^ minor ^ patch; |
114 | } | |
115 | ||
116 | 37 |
![]() |
117 | 37 | 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 | 10 |
![]() |
127 | 10 | Version compare = null; |
128 | 10 | try { |
129 | 10 | compare = new Version(version); |
130 | } catch (RuntimeException e) { | |
131 | // ignore | |
132 | } | |
133 | 10 | return equals(compare); |
134 | } | |
135 | ||
136 | 34 |
![]() |
137 | 34 | return major + "." + (minor < 10 ? "0" : "") + minor |
138 | 34 | + "." + (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 | 22 |
![]() |
148 | 22 | 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 |
![]() |
158 | 21 | return 0 < compareTo(other); |
159 | } | |
160 | ||
161 | /** | |
162 | * Is <code>version1</code> < <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 | 6 |
![]() |
171 | 6 | return (new Version(version1)).isLess(new Version(version2)); |
172 | } | |
173 | ||
174 | /** | |
175 | * Is <code>version1</code> > <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 |
![]() |
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 | 5 |
![]() |
197 | 5 | return (new Version(version1)).equals(new Version(version2)); |
198 | } | |
199 | ||
200 | } |
|