3434 */
3535public class VersionNumber implements Comparable <VersionNumber > {
3636
37- private static final String VERSION_REGEXP = "(\\ d+)\\ .(\\ d+)(\\ .\\ d+)?([~\\ -]\\ w[.\\ w]*(?:\\ -\\ w[.\\ w]*)*)?(\\ +[.\\ w]+)?" ;
37+ private static final String VERSION_REGEXP = "(\\ d+)\\ .(\\ d+)(\\ .\\ d+)?(\\ . \\ d+)?( [~\\ -]\\ w[.\\ w]*(?:\\ -\\ w[.\\ w]*)*)?(\\ +[.\\ w]+)?" ;
3838 private static final Pattern pattern = Pattern .compile (VERSION_REGEXP );
3939
4040 private final int major ;
4141 private final int minor ;
4242 private final int patch ;
43+ private final int dsePatch ;
4344
4445 private final String [] preReleases ;
4546 private final String build ;
4647
47- private VersionNumber (int major , int minor , int patch , String [] preReleases , String build ) {
48+ private VersionNumber (int major , int minor , int patch , int dsePatch , String [] preReleases , String build ) {
4849 this .major = major ;
4950 this .minor = minor ;
5051 this .patch = patch ;
52+ this .dsePatch = dsePatch ;
5153 this .preReleases = preReleases ;
5254 this .build = build ;
5355 }
@@ -79,13 +81,16 @@ public static VersionNumber parse(String version) {
7981 String pa = matcher .group (3 );
8082 int patch = pa == null || pa .isEmpty () ? 0 : Integer .parseInt (pa .substring (1 )); // dropping the initial '.' since it's included this time
8183
82- String pr = matcher .group (4 );
84+ String dse = matcher .group (4 );
85+ int dsePatch = dse == null || dse .isEmpty () ? -1 : Integer .parseInt (dse .substring (1 )); // dropping the initial '.' since it's included this time
86+
87+ String pr = matcher .group (5 );
8388 String [] preReleases = pr == null || pr .isEmpty () ? null : pr .substring (1 ).split ("\\ -" ); // drop initial '-' or '~' then split on the remaining ones
8489
85- String bl = matcher .group (5 );
90+ String bl = matcher .group (6 );
8691 String build = bl == null || bl .isEmpty () ? null : bl .substring (1 ); // drop the initial '+'
8792
88- return new VersionNumber (major , minor , patch , preReleases , build );
93+ return new VersionNumber (major , minor , patch , dsePatch , preReleases , build );
8994 } catch (NumberFormatException e ) {
9095 throw new IllegalArgumentException ("Invalid version number: " + version );
9196 }
@@ -118,6 +123,20 @@ public int getPatch() {
118123 return patch ;
119124 }
120125
126+ /**
127+ * The DSE patch version number (will only be present for version of Cassandra in DSE).
128+ * <p>
129+ * DataStax Entreprise (DSE) adds a fourth number to the version number to track potential
130+ * hot fixes and/or DSE specific patches that may have been applied to the Cassandra version.
131+ * In that case, this method return that fourth number.
132+ *
133+ * @return the DSE patch version number, i.e. D in X.Y.Z.D, or -1 if the version number is
134+ * not from DSE.
135+ */
136+ public int getDSEPatch () {
137+ return dsePatch ;
138+ }
139+
121140 /**
122141 * The pre-release labels if relevants, i.e. label1 and label2 in X.Y.Z-label1-lable2.
123142 *
@@ -154,6 +173,20 @@ public int compareTo(VersionNumber other) {
154173 if (patch > other .patch )
155174 return 1 ;
156175
176+ if (dsePatch < 0 ) {
177+ if (other .dsePatch >= 0 )
178+ return -1 ;
179+ } else {
180+ if (other .dsePatch < 0 )
181+ return 1 ;
182+
183+ // Both are >= 0
184+ if (dsePatch < other .dsePatch )
185+ return -1 ;
186+ if (dsePatch > other .dsePatch )
187+ return 1 ;
188+ }
189+
157190 if (preReleases == null )
158191 return other .preReleases == null ? 0 : 1 ;
159192 if (other .preReleases == null )
@@ -179,19 +212,22 @@ public boolean equals(Object o) {
179212 return major == that .major
180213 && minor == that .minor
181214 && patch == that .patch
215+ && dsePatch == that .dsePatch
182216 && (preReleases == null ? that .preReleases == null : Arrays .equals (preReleases , that .preReleases ))
183217 && Objects .equal (build , that .build );
184218 }
185219
186220 @ Override
187221 public int hashCode () {
188- return Objects .hashCode (major , minor , patch , preReleases , build );
222+ return Objects .hashCode (major , minor , patch , dsePatch , preReleases , build );
189223 }
190224
191225 @ Override
192226 public String toString () {
193227 StringBuilder sb = new StringBuilder ();
194228 sb .append (major ).append ('.' ).append (minor ).append ('.' ).append (patch );
229+ if (dsePatch >= 0 )
230+ sb .append ('.' ).append (dsePatch );
195231 if (preReleases != null ) {
196232 for (String preRelease : preReleases )
197233 sb .append ('-' ).append (preRelease );
0 commit comments