File Source: Version.java
/*
P/P * Method: com.dmdirc.updater.Version__static_init
*/
1 /*
2 * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 package com.dmdirc.updater;
24
25 /**
26 * Describes a version of a component, either as an integer or as a String which
27 * corresponds to the output of `git-describe --tags`.
28 *
29 * @since 0.6.3m1
30 * @author chris
31 */
/*
P/P * Method: int compareTo(Object)
*
* Preconditions:
* x0 != null
* (soft) this.intVersion - x0.intVersion in {-6_442_450_943, -231..232-1, 6_442_450_943}
*
* Postconditions:
* init'ed(return_value)
*/
32 public class Version implements Comparable<Version> {
33
34 protected final int intVersion;
35 protected final String strVersion;
36
/*
P/P * Method: void com.dmdirc.updater.Version(int)
*
* Postconditions:
* this.intVersion == version
* init'ed(this.intVersion)
* this.strVersion == null
*/
37 public Version(final int version) {
38 this.intVersion = version;
39 this.strVersion = null;
40 }
41
/*
P/P * Method: void com.dmdirc.updater.Version(String)
*
* Preconditions:
* version != null
*
* Postconditions:
* init'ed(this.intVersion)
* this.strVersion == One-of{null, version}
* init'ed(this.strVersion)
*
* Test Vectors:
* java.lang.String:matches(...)@43: {0}, {1}
* java.lang.String:matches(...)@46: {0}, {1}
*/
42 public Version(final String version) {
43 if (version.matches("^[0-9]+$")) {
44 this.intVersion = Integer.parseInt(version);
45 this.strVersion = null;
46 } else if (version.matches("^[0-9]+(\\.[0-9]+)*((a|b|rc|m)[0-9]+)*(\\-[0-9]+\\-g[a-z0-9]{7})?$")) {
47 this.intVersion = Integer.MIN_VALUE;
48 this.strVersion = version;
49 } else {
50 this.intVersion = Integer.MIN_VALUE;
51 this.strVersion = null;
52 }
53 }
54
55 /** {@inheritDoc} */
56 @Override
57 public int compareTo(final Version o) {
/*
P/P * Method: int compareTo(Version)
*
* Preconditions:
* o != null
* (soft) this.intVersion - o.intVersion in {-6_442_450_943, -231..232-1, 6_442_450_943}
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* o.intVersion: {-231}, {-231+1..0, 232-1}
* o.strVersion: Inverse{null}, Addr_Set{null}
* this.intVersion: {-231}, {-231+1..232-1}
* this.intVersion - o.intVersion: {0..232-1}, {-6_442_450_943, -231..-1}
* this.strVersion: Inverse{null}, Addr_Set{null}
*/
58 if (o.intVersion > Integer.MIN_VALUE && this.intVersion > Integer.MIN_VALUE) {
59 return this.intVersion - o.intVersion;
60 } else if (o.strVersion == null && this.strVersion == null) {
61 return 0;
62 } else if (o.strVersion == null && this.strVersion != null) {
63 return 1;
64 } else if (o.strVersion != null && this.strVersion == null) {
65 return -1;
66 } else {
67 final String myParts[] = this.strVersion.split("-");
68 final String thParts[] = o.strVersion.split("-");
69
70 final String myFirstParts[] = myParts[0].split("\\.|(?=a|b|rc|m)");
71 final String thFirstParts[] = thParts[0].split("\\.|(?=a|b|rc|m)");
72
73 for (int i = 0; i < Math.max(myFirstParts.length, thFirstParts.length); i++) {
74 final boolean myExists = myFirstParts.length > i;
75 final boolean thExists = thFirstParts.length > i;
76
77 final boolean myIsInt = myExists && myFirstParts[i].matches("[0-9]+");
78 final boolean thIsInt = thExists && thFirstParts[i].matches("[0-9]+");
79
80 final int myInt = myIsInt ? Integer.parseInt(myFirstParts[i]) : 0;
81 final int thInt = thIsInt ? Integer.parseInt(thFirstParts[i]) : 0;
82
83 // Please consult handwritten truth table in the care of
84 // Chris for an explanation of what the hell is going on here.
85 // If there's a bug in this code it should probably be
86 // rewritten.
87 if ((!myExists && !thExists)
88 || (myIsInt && thIsInt && myInt == thInt)
89 || (myExists && thExists && !myIsInt && !thIsInt
90 && myFirstParts[i].equals(thFirstParts[i]))) {
91 continue;
92 } else if ((!thExists && myIsInt)
93 || (thExists && !thIsInt && (!myExists || myIsInt))) {
94 return +1;
95 } else if ((thIsInt && !myExists)
96 || (myExists && !myIsInt && (!thExists || thIsInt))) {
97 return -1;
98 } else if (thIsInt && myIsInt) {
99 return myInt - thInt;
100 } else {
101 final String myLetterParts[] = myFirstParts[i].split("(?=[0-9])", 2);
102 final String thLetterParts[] = thFirstParts[i].split("(?=[0-9])", 2);
103
104 if (myLetterParts[0].equals(thLetterParts[0])) {
105 return Integer.parseInt(myLetterParts[1])
106 - Integer.parseInt(thLetterParts[1]);
107 } else if (myLetterParts[0].equals("m")
108 || thLetterParts[0].equals("rc")
109 || (myLetterParts[0].equals("a") && thLetterParts[0].equals("b"))) {
110 return -1;
111 } else {
112 return +1;
113 }
114 }
115 }
116
117 final int myInt = myParts.length > 1 ? Integer.parseInt(myParts[1]) : 0;
118 final int thInt = thParts.length > 1 ? Integer.parseInt(thParts[1]) : 0;
119
120 return myInt - thInt;
121 }
122 }
123
124 /**
125 * Determines whether or not this represents a valid version.
126 *
127 * @return True if the version is valid, false otherwise
128 */
129 public boolean isValid() {
/*
P/P * Method: bool isValid()
*
* Postconditions:
* init'ed(return_value)
*/
130 return intVersion > Integer.MIN_VALUE || strVersion != null;
131 }
132
133 /** {@inheritDoc} */
134 @Override
135 public String toString() {
/*
P/P * Method: String toString()
*
* Postconditions:
* java.lang.String:valueOf(...)._tainted == 0
* return_value == One-of{&java.lang.String:valueOf(...), this.strVersion}
* return_value != null
*/
136 return strVersion == null ? String.valueOf(intVersion) : strVersion;
137 }
138
139 }
SofCheck Inspector Build Version : 2.17854
| Version.java |
2009-Jun-25 01:54:24 |
| Version.class |
2009-Sep-02 17:04:13 |