| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
package nl.toolforge.karma.core; |
| 20 |
|
|
| 21 |
|
import java.util.StringTokenizer; |
| 22 |
|
import java.util.regex.PatternSyntaxException; |
| 23 |
|
|
| 24 |
|
|
| 25 |
|
|
| 26 |
|
|
| 27 |
|
|
| 28 |
|
|
| 29 |
|
|
| 30 |
|
|
| 31 |
|
public class Version implements Cloneable, Comparable { |
| 32 |
|
|
| 33 |
260 |
public static String VERSION_PATTERN_STRING = "\\d{1,2}-\\d{1,2}"; |
| 34 |
|
|
| 35 |
|
|
| 36 |
|
public static final String VERSION_SEPARATOR_CHAR = "-"; |
| 37 |
|
|
| 38 |
|
|
| 39 |
260 |
public static Version INITIAL_VERSION = new Version("0"+VERSION_SEPARATOR_CHAR+"0"); |
| 40 |
|
|
| 41 |
|
public static final int FIRST_DIGIT = 0; |
| 42 |
|
public static final int SECOND_DIGIT = 1; |
| 43 |
|
public static final int THIRD_DIGIT = 2; |
| 44 |
|
|
| 45 |
1981 |
private String versionNumber = null; |
| 46 |
|
|
| 47 |
1981 |
private int[] versionDigits = null; |
| 48 |
|
|
| 49 |
|
|
| 50 |
|
|
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
| 54 |
1955 |
public Version(String versionIdentifier) { |
| 55 |
|
|
| 56 |
1955 |
if (!versionIdentclass="keyword">ifier.matches(getPatternString())) { |
| 57 |
26 |
throw new PatternSyntaxException( |
| 58 |
|
"Pattern mismatch for version. Should match " + getPatternString(), versionIdentifier, -1); |
| 59 |
|
} |
| 60 |
|
|
| 61 |
|
|
| 62 |
1929 |
StringTokenizer tokenizer = new StringTokenizer(versionIdentifier, VERSION_SEPARATOR_CHAR); |
| 63 |
|
|
| 64 |
1929 |
versionDigits = new int[tokenizer.countTokens()]; |
| 65 |
1929 |
int i = 0; |
| 66 |
5991 |
while (tokenizer.hasMoreTokens()) { |
| 67 |
4062 |
versionDigits[i] = new Integer(tokenizer.nextToken()).intValue(); |
| 68 |
4062 |
i++; |
| 69 |
|
} |
| 70 |
|
|
| 71 |
1929 |
createVersionNumber(); |
| 72 |
1929 |
} |
| 73 |
|
|
| 74 |
|
public String getPatternString() { |
| 75 |
1777 |
return VERSION_PATTERN_STRING; |
| 76 |
|
} |
| 77 |
|
|
| 78 |
|
|
| 79 |
|
|
| 80 |
|
|
| 81 |
|
|
| 82 |
|
|
| 83 |
|
protected static Version getInitialVersion() { |
| 84 |
0 |
return new Version("0"+VERSION_SEPARATOR_CHAR+"0"); |
| 85 |
|
} |
| 86 |
|
|
| 87 |
|
|
| 88 |
|
|
| 89 |
|
|
| 90 |
|
|
| 91 |
|
|
| 92 |
|
|
| 93 |
|
public Patch createPatch(int patchNumber) { |
| 94 |
78 |
return new Patch(getVersionNumber() + VERSION_SEPARATOR_CHAR + patchNumber); |
| 95 |
|
} |
| 96 |
|
|
| 97 |
|
|
| 98 |
|
|
| 99 |
|
|
| 100 |
|
|
| 101 |
|
|
| 102 |
|
|
| 103 |
26 |
public Version(int[] versionDigits) { |
| 104 |
|
|
| 105 |
26 |
if (versionDigits.length < 2) { |
| 106 |
0 |
throw new IllegalArgumentException( |
| 107 |
|
"Pattern mismatch for version. Should match " + getPatternString() + |
| 108 |
|
"; provide 'int'-array"); |
| 109 |
|
} |
| 110 |
|
|
| 111 |
26 |
StringBuffer versionStringBuffer = new StringBuffer(); |
| 112 |
104 |
for (int i = 0; i < versionDigits.length; i++) { |
| 113 |
78 |
versionStringBuffer.append(versionDigits[i]); |
| 114 |
78 |
if (i < versionDigits.length - 1); |
| 115 |
|
} |
| 116 |
|
|
| 117 |
26 |
this.versionDigits = versionDigits; |
| 118 |
|
|
| 119 |
26 |
createVersionNumber(); |
| 120 |
26 |
} |
| 121 |
|
|
| 122 |
|
private void createVersionNumber() { |
| 123 |
1512 |
this.versionNumber = "" + versionDigits[0]; |
| 124 |
3216 |
for (int i = 1; i < versionDigits.length; i++) { |
| 125 |
1704 |
this.versionNumber += VERSION_SEPARATOR_CHAR + versionDigits[i]; |
| 126 |
571 |
} |
| 127 |
2724 |
} |
| 128 |
641 |
|
| 129 |
|
public String getVersionNumber() { |
| 130 |
924 |
return versionNumber; |
| 131 |
|
} |
| 132 |
|
|
| 133 |
220 |
private int getLastDigit() { |
| 134 |
64 |
return versionDigits[versionDigits.length - 1]; |
| 135 |
|
} |
| 136 |
|
|
| 137 |
|
private int getLastDigitIndex() { |
| 138 |
64 |
return versionDigits.length - 1; |
| 139 |
|
} |
| 140 |
|
|
| 141 |
|
|
| 142 |
|
|
| 143 |
|
|
| 144 |
|
|
| 145 |
|
|
| 146 |
|
public String toString() { |
| 147 |
616 |
return versionNumber; |
| 148 |
|
} |
| 149 |
|
|
| 150 |
210 |
public final int hashCode() { |
| 151 |
0 |
return versionNumber.hashCode(); |
| 152 |
|
} |
| 153 |
|
|
| 154 |
|
public final boolean equals(Object o) { |
| 155 |
128 |
if (!(o instanceof Version)) { |
| 156 |
0 |
return false; |
| 157 |
|
} |
| 158 |
71 |
|
| 159 |
128 |
return ((Version) o).versionNumber.equals(this.versionNumber); |
| 160 |
|
} |
| 161 |
|
|
| 162 |
71 |
|
| 163 |
|
|
| 164 |
|
|
| 165 |
|
|
| 166 |
|
|
| 167 |
|
|
| 168 |
|
|
| 169 |
|
public final int compareTo(Object o) { |
| 170 |
|
|
| 171 |
176 |
int[] zis = versionDigits; |
| 172 |
176 |
int[] zat = ((Version) o).versionDigits; |
| 173 |
|
|
| 174 |
286 |
if (zis[0] < zat[0]) { |
| 175 |
158 |
return -1; |
| 176 |
128 |
} else if (zis[0] == zat[0]) { |
| 177 |
190 |
if (zis[1] < zat[1]) { |
| 178 |
78 |
return -1; |
| 179 |
112 |
} else if (zis[1] == zat[1]) { |
| 180 |
50 |
if (o instanceof Patch) { |
| 181 |
30 |
|
| 182 |
20 |
|
| 183 |
0 |
if (zis[2] < zat[2]) { |
| 184 |
0 |
return -1; |
| 185 |
0 |
} else if (zis[2] == zat[2]) { |
| 186 |
0 |
return 0; |
| 187 |
|
} else { |
| 188 |
0 |
return 1; |
| 189 |
|
} |
| 190 |
|
} |
| 191 |
0 |
return 0; |
| 192 |
|
} else { |
| 193 |
32 |
return 1; |
| 194 |
|
} |
| 195 |
|
} else { |
| 196 |
68 |
return 1; |
| 197 |
|
} |
| 198 |
|
} |
| 199 |
30 |
|
| 200 |
|
public void setDigit(int index, class="keyword">int nextDigit) { |
| 201 |
128 |
versionDigits[index] = nextDigit; |
| 202 |
128 |
createVersionNumber(); |
| 203 |
128 |
} |
| 204 |
|
|
| 205 |
|
public final boolean isLowerThan(Version version) { |
| 206 |
64 |
return this.compareTo(version) == -1; |
| 207 |
|
} |
| 208 |
|
|
| 209 |
40 |
public final boolean isHigherThan(Version version) { |
| 210 |
64 |
return this.compareTo(version) == 1; |
| 211 |
|
} |
| 212 |
|
|
| 213 |
40 |
|
| 214 |
|
|
| 215 |
|
|
| 216 |
|
public final void increase() { |
| 217 |
64 |
setDigit(getLastDigitIndex(), getLastDigit() + 1); |
| 218 |
64 |
} |
| 219 |
|
|
| 220 |
|
|
| 221 |
|
|
| 222 |
|
|
| 223 |
|
|
| 224 |
|
public void increaseMajor() { |
| 225 |
32 |
setDigit(FIRST_DIGIT, versionDigits[0] + 1); |
| 226 |
32 |
setDigit(SECOND_DIGIT, 0); |
| 227 |
32 |
} |
| 228 |
|
|
| 229 |
|
public Object clone() throws CloneNotSupportedException { |
| 230 |
0 |
return super.clone(); |
| 231 |
|
} |
| 232 |
|
} |