-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathVersion.java
More file actions
192 lines (155 loc) · 4.88 KB
/
Version.java
File metadata and controls
192 lines (155 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package org.utplsql.api;
import org.utplsql.api.exception.InvalidVersionException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** Simple class to parse utPLSQL Version-information and provide the separate version-numbers
*
* @author pesse
*/
public class Version implements Comparable<Version> {
private String origString;
private Integer major;
private Integer minor;
private Integer bugfix;
private Integer build;
private boolean valid = false;
public Version( String versionString ) {
assert versionString != null;
this.origString = versionString;
parseVersionString();
}
private void parseVersionString()
{
Pattern p = Pattern.compile("([0-9]+)\\.?([0-9]+)?\\.?([0-9]+)?\\.?([0-9]+)?");
Matcher m = p.matcher(origString);
try {
if (m.find()) {
if ( m.group(1) != null )
major = Integer.valueOf(m.group(1));
if ( m.group(2) != null )
minor = Integer.valueOf(m.group(2));
if ( m.group(3) != null )
bugfix = Integer.valueOf(m.group(3));
if ( m.group(4) != null )
build = Integer.valueOf(m.group(4));
if ( major != null ) // We need a valid major version as minimum requirement for a Version object to be valid
valid = true;
}
}
catch ( NumberFormatException e )
{
valid = false;
}
}
@Override
public String toString() {
return origString;
}
public Integer getMajor() {
return major;
}
public Integer getMinor() {
return minor;
}
public Integer getBugfix() {
return bugfix;
}
public Integer getBuild() {
return build;
}
public boolean isValid() {
return valid;
}
/** Returns a normalized form of the parsed version information
*
* @return
*/
public String getNormalizedString()
{
if ( isValid() ) {
StringBuilder sb = new StringBuilder();
sb.append(String.valueOf(major));
if ( minor != null )
sb.append("." + String.valueOf(minor));
if ( bugfix != null )
sb.append("." + String.valueOf(bugfix));
if ( build != null )
sb.append("." + String.valueOf(build));
return sb.toString();
}
else
return "invalid";
}
private int compareToWithNulls( Integer i1, Integer i2 ) {
if ( i1 == null && i2 == null )
return 0;
else if ( i1 == null )
return -1;
else if ( i2 == null )
return 1;
else return i1.compareTo(i2);
}
@Override
public int compareTo(Version o) {
int curResult;
if ( isValid() && o.isValid() ) {
curResult = compareToWithNulls(getMajor(), o.getMajor());
if ( curResult != 0 )
return curResult;
curResult = compareToWithNulls(getMinor(), o.getMinor());
if ( curResult != 0 )
return curResult;
curResult = compareToWithNulls(getBugfix(), o.getBugfix());
if ( curResult != 0 )
return curResult;
curResult = compareToWithNulls(getBuild(), o.getBuild());
if ( curResult != 0 )
return curResult;
}
return 0;
}
private void versionsAreValid( Version v ) throws InvalidVersionException {
if ( !isValid() )
throw new InvalidVersionException(this);
if ( !v.isValid() )
throw new InvalidVersionException(v);
}
/** Compares this version to a given version and returns true if this version is greater or equal than the given one
* Throws an InvalidVersionException if either this or the given version are invalid
*
* @param v Version to compare with
* @return
* @throws InvalidVersionException
*/
public boolean isGreaterOrEqualThan( Version v ) throws InvalidVersionException {
versionsAreValid(v);
if ( compareTo(v) >= 0 )
return true;
else
return false;
}
public boolean isGreaterThan( Version v) throws InvalidVersionException
{
versionsAreValid(v);
if ( compareTo(v) > 0 )
return true;
else
return false;
}
public boolean isLessOrEqualThan( Version v ) throws InvalidVersionException
{
versionsAreValid(v);
if ( compareTo(v) <= 0 )
return true;
else
return false;
}
public boolean isLessThan( Version v) throws InvalidVersionException
{
versionsAreValid(v);
if ( compareTo(v) < 0 )
return true;
else
return false;
}
}