33import org .utplsql .api .exception .InvalidVersionException ;
44
55import javax .annotation .Nullable ;
6+ import java .util .Map ;
7+ import java .util .Objects ;
8+ import java .util .function .Function ;
69import java .util .regex .Matcher ;
710import java .util .regex .Pattern ;
11+ import java .util .stream .Stream ;
12+
13+ import static java .util .stream .Collectors .toMap ;
814
915/** Simple class to parse utPLSQL Version-information and provide the separate version-numbers
1016 *
1117 * @author pesse
1218 */
1319public 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+
1433 private final String origString ;
1534 private final Integer major ;
1635 private final Integer minor ;
1736 private final Integer bugfix ;
1837 private final Integer build ;
1938 private final boolean valid ;
2039
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 ) {
2255 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+ }
2465
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+ }
2671
27- Matcher m = p .matcher (origString );
72+ private static Version parseVersionString (String origString )
73+ {
2874
2975 Integer major = null ;
3076 Integer minor = null ;
3177 Integer bugfix = null ;
3278 Integer build = null ;
3379 boolean valid = false ;
80+ Pattern p = Pattern .compile ("([0-9]+)\\ .?([0-9]+)?\\ .?([0-9]+)?\\ .?([0-9]+)?" );
81+
82+ Matcher m = p .matcher (origString );
3483
3584 try {
3685 if (m .find ()) {
@@ -52,30 +101,30 @@ public Version( String versionString ) {
52101 valid = false ;
53102 }
54103
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 );
60105 }
61106
62107 @ Override
63108 public String toString () {
64109 return origString ;
65110 }
66111
112+ @ Nullable
67113 public Integer getMajor () {
68114 return major ;
69115 }
70116
117+ @ Nullable
71118 public Integer getMinor () {
72119 return minor ;
73120 }
74121
122+ @ Nullable
75123 public Integer getBugfix () {
76124 return bugfix ;
77125 }
78126
127+ @ Nullable
79128 public Integer getBuild () {
80129 return build ;
81130 }
@@ -92,13 +141,13 @@ public String getNormalizedString()
92141 {
93142 if ( isValid () ) {
94143 StringBuilder sb = new StringBuilder ();
95- sb .append (String . valueOf ( major ) );
144+ sb .append (major );
96145 if ( minor != null )
97- sb .append ("." ).append (String . valueOf ( minor ) );
146+ sb .append ("." ).append (minor );
98147 if ( bugfix != null )
99- sb .append ("." ).append (String . valueOf ( bugfix ) );
148+ sb .append ("." ).append (bugfix );
100149 if ( build != null )
101- sb .append ("." ).append (String . valueOf ( build ) );
150+ sb .append ("." ).append (build );
102151
103152 return sb .toString ();
104153 }
0 commit comments