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 | public Version(final String version) { |
46 | final TextInput text = new TextInput(version); |
47 | major = text.readNonNegativeInt(); |
48 | if (!".".equals(text.readString(1))) { |
49 | throw new IllegalArgumentException("version number must have two digits"); |
50 | } |
51 | minor = text.readNonNegativeInt(); |
52 | if (!".".equals(text.readString(1))) { |
53 | throw new IllegalArgumentException("version number must have two digits"); |
54 | } |
55 | patch = text.readNonNegativeInt(); |
56 | text.skipWhiteSpace(); |
57 | if (!text.isEmpty()) { |
58 | 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 | public int getMajor() { |
68 | return major; |
69 | } |
70 | |
71 | /** |
72 | * Get minor version number. |
73 | * |
74 | * @return Minor version number. |
75 | */ |
76 | public int getMinor() { |
77 | return minor; |
78 | } |
79 | |
80 | /** |
81 | * Get patch number. |
82 | * |
83 | * @return Patch version number. |
84 | */ |
85 | public int getPatch() { |
86 | return patch; |
87 | } |
88 | |
89 | public int compareTo(final Object o) { |
90 | if (!(o instanceof Version)) { |
91 | return -1; |
92 | } |
93 | final Version other = (Version) o; |
94 | if (major < other.major) { |
95 | return -1; |
96 | } else if (major > other.major) { |
97 | return 1; |
98 | } |
99 | if (minor < other.minor) { |
100 | return -1; |
101 | } else if (minor > other.minor) { |
102 | return 1; |
103 | } |
104 | if (patch < other.patch) { |
105 | return -1; |
106 | } else if (patch > other.patch) { |
107 | return 1; |
108 | } |
109 | return 0; |
110 | } |
111 | |
112 | public int hashCode() { |
113 | return major ^ minor ^ patch; |
114 | } |
115 | |
116 | public boolean equals(final Object o) { |
117 | 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 | public boolean equals(final String version) { |
127 | Version compare = null; |
128 | try { |
129 | compare = new Version(version); |
130 | } catch (RuntimeException e) { |
131 | // ignore |
132 | } |
133 | return equals(compare); |
134 | } |
135 | |
136 | public String toString() { |
137 | return major + "." + (minor < 10 ? "0" : "") + minor |
138 | + "." + (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 | public boolean isLess(final Version other) { |
148 | 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 | public boolean isBigger(final Version other) { |
158 | 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 | public static boolean less(final String version1, final String version2) { |
171 | 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 | public static boolean bigger(final String version1, final String version2) { |
184 | 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 | public static boolean equals(final String version1, final String version2) { |
197 | return (new Version(version1)).equals(new Version(version2)); |
198 | } |
199 | |
200 | } |