001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002 *
003 * Copyright 2000-2013, Michael Meyling <mime@qedeq.org>.
004 *
005 * "Hilbert II" is free software; you can redistribute
006 * it and/or modify it under the terms of the GNU General Public
007 * License as published by the Free Software Foundation; either
008 * version 2 of the License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU General Public License for more details.
014 */
015
016 package org.qedeq.base.io;
017
018
019 /**
020 * A version number implementation oriented at the standard:
021 * <a href="http://semver.org/">http://semver.org</a>.
022 *
023 * @author Michael Meyling
024 */
025 public final class Version implements Comparable {
026
027 /** Major version number. */
028 private final int major;
029
030 /** Minor version number. */
031 private final int minor;
032
033 /** Patch version number. */
034 private final int patch;
035
036 /**
037 * Constructs version object. You must give a version string in a form like
038 * <em>a</em>.<em>b</em>.<em>c</em> where a, b and c are non negative integers.
039 * These numbers are called <em>Major</em> <em>Minor</em> and <em>Patch</em>.
040 *
041 * @param version Version string.
042 * @throws IllegalArgumentException Version string has wrong format.
043 * @throws NullPointerException No null pointer as argument accepted.
044 */
045 public Version(final String version) {
046 final TextInput text = new TextInput(version);
047 major = text.readNonNegativeInt();
048 if (!".".equals(text.readString(1))) {
049 throw new IllegalArgumentException("version number must have two digits");
050 }
051 minor = text.readNonNegativeInt();
052 if (!".".equals(text.readString(1))) {
053 throw new IllegalArgumentException("version number must have two digits");
054 }
055 patch = text.readNonNegativeInt();
056 text.skipWhiteSpace();
057 if (!text.isEmpty()) {
058 throw new IllegalArgumentException("version number to long: " + text.readString(100));
059 }
060 }
061
062 /**
063 * Get major version number.
064 *
065 * @return Major version number.
066 */
067 public int getMajor() {
068 return major;
069 }
070
071 /**
072 * Get minor version number.
073 *
074 * @return Minor version number.
075 */
076 public int getMinor() {
077 return minor;
078 }
079
080 /**
081 * Get patch number.
082 *
083 * @return Patch version number.
084 */
085 public int getPatch() {
086 return patch;
087 }
088
089 public int compareTo(final Object o) {
090 if (!(o instanceof Version)) {
091 return -1;
092 }
093 final Version other = (Version) o;
094 if (major < other.major) {
095 return -1;
096 } else if (major > other.major) {
097 return 1;
098 }
099 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 -1 == 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 1 == 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 }
|