3
3
import org .utplsql .api .exception .InvalidVersionException ;
4
4
5
5
import javax .annotation .Nullable ;
6
+ import java .util .Map ;
7
+ import java .util .Objects ;
8
+ import java .util .function .Function ;
6
9
import java .util .regex .Matcher ;
7
10
import java .util .regex .Pattern ;
11
+ import java .util .stream .Stream ;
12
+
13
+ import static java .util .stream .Collectors .toMap ;
8
14
9
15
/** Simple class to parse utPLSQL Version-information and provide the separate version-numbers
10
16
*
11
17
* @author pesse
12
18
*/
13
19
public class Version implements Comparable <Version > {
20
+
21
+ public final static Version V3_0_0 = new Version ("3.0.0" , 3 ,0 ,0 ,null , true );
22
+ public final static Version V3_0_1 = new Version ("3.0.1" , 3 ,0 ,1 ,null , true );
23
+ public final static Version V3_0_2 = new Version ("3.0.2" , 3 ,0 ,2 ,null , true );
24
+ public final static Version V3_0_3 = new Version ("3.0.3" , 3 ,0 ,3 ,null , true );
25
+ public final static Version V3_0_4 = new Version ("3.0.4" , 3 ,0 ,4 ,null , true );
26
+ public final static Version V3_1_0 = new Version ("3.1.0" , 3 ,1 ,0 ,null , true );
27
+ public final static Version V3_1_1 = new Version ("3.1.1" , 3 ,1 ,1 ,null , true );
28
+ public final static Version V3_1_2 = new Version ("3.1.2" , 3 ,1 ,2 ,null , true );
29
+ private final static Map <String , Version > knownVersions =
30
+ Stream .of (V3_0_0 , V3_0_1 , V3_0_2 , V3_0_3 , V3_0_4 , V3_1_0 , V3_1_1 , V3_1_2 )
31
+ .collect (toMap (Version ::toString , Function .identity ()));
32
+
14
33
private final String origString ;
15
34
private final Integer major ;
16
35
private final Integer minor ;
17
36
private final Integer bugfix ;
18
37
private final Integer build ;
19
38
private final boolean valid ;
20
39
21
- public Version ( String versionString ) {
40
+ private Version (String origString , @ Nullable Integer major , @ Nullable Integer minor , @ Nullable Integer bugfix , @ Nullable Integer build , boolean valid ) {
41
+ this .origString = origString ;
42
+ this .major = major ;
43
+ this .minor = minor ;
44
+ this .bugfix = bugfix ;
45
+ this .build = build ;
46
+ this .valid = valid ;
47
+ }
48
+
49
+ /**
50
+ * Use {@link Version#create} factory method instead
51
+ * For removal
52
+ */
53
+ @ Deprecated ()
54
+ public Version (String versionString ) {
22
55
assert versionString != null ;
23
- this .origString = versionString .trim ();
56
+ Version dummy = parseVersionString (versionString );
57
+
58
+ this .origString = dummy .origString ;
59
+ this .major = dummy .major ;
60
+ this .minor =dummy .minor ;
61
+ this .bugfix = dummy .bugfix ;
62
+ this .build = dummy .build ;
63
+ this .valid = dummy .valid ;
64
+ }
24
65
25
- Pattern p = Pattern .compile ("([0-9]+)\\ .?([0-9]+)?\\ .?([0-9]+)?\\ .?([0-9]+)?" );
66
+ public static Version create (final String versionString ) {
67
+ String origString = Objects .requireNonNull (versionString ).trim ();
68
+ Version version = knownVersions .get (origString );
69
+ return version != null ? version : parseVersionString (origString );
70
+ }
26
71
27
- Matcher m = p .matcher (origString );
72
+ private static Version parseVersionString (String origString )
73
+ {
28
74
29
75
Integer major = null ;
30
76
Integer minor = null ;
31
77
Integer bugfix = null ;
32
78
Integer build = null ;
33
79
boolean valid = false ;
80
+ Pattern p = Pattern .compile ("([0-9]+)\\ .?([0-9]+)?\\ .?([0-9]+)?\\ .?([0-9]+)?" );
81
+
82
+ Matcher m = p .matcher (origString );
34
83
35
84
try {
36
85
if (m .find ()) {
@@ -52,30 +101,30 @@ public Version( String versionString ) {
52
101
valid = false ;
53
102
}
54
103
55
- this .major = major ;
56
- this .minor = minor ;
57
- this .bugfix = bugfix ;
58
- this .build = build ;
59
- this .valid = valid ;
104
+ return new Version (origString , major , minor , bugfix , build , valid );
60
105
}
61
106
62
107
@ Override
63
108
public String toString () {
64
109
return origString ;
65
110
}
66
111
112
+ @ Nullable
67
113
public Integer getMajor () {
68
114
return major ;
69
115
}
70
116
117
+ @ Nullable
71
118
public Integer getMinor () {
72
119
return minor ;
73
120
}
74
121
122
+ @ Nullable
75
123
public Integer getBugfix () {
76
124
return bugfix ;
77
125
}
78
126
127
+ @ Nullable
79
128
public Integer getBuild () {
80
129
return build ;
81
130
}
@@ -92,13 +141,13 @@ public String getNormalizedString()
92
141
{
93
142
if ( isValid () ) {
94
143
StringBuilder sb = new StringBuilder ();
95
- sb .append (String . valueOf ( major ) );
144
+ sb .append (major );
96
145
if ( minor != null )
97
- sb .append ("." ).append (String . valueOf ( minor ) );
146
+ sb .append ("." ).append (minor );
98
147
if ( bugfix != null )
99
- sb .append ("." ).append (String . valueOf ( bugfix ) );
148
+ sb .append ("." ).append (bugfix );
100
149
if ( build != null )
101
- sb .append ("." ).append (String . valueOf ( build ) );
150
+ sb .append ("." ).append (build );
102
151
103
152
return sb .toString ();
104
153
}
0 commit comments