Thomas Vachuska

ONOS-2124 FIxed Version to allow two-segment versions and to be more robust in general.

Change-Id: I92db41ad0aa7c604375dae5a0ac6804462532ba9
...@@ -26,8 +26,9 @@ import static java.lang.Integer.parseInt; ...@@ -26,8 +26,9 @@ import static java.lang.Integer.parseInt;
26 */ 26 */
27 public final class Version { 27 public final class Version {
28 28
29 - public static final String FORMAT = "%d.%d.%s.%s"; 29 + public static final String FORMAT_MINIMAL = "%d.%d";
30 public static final String FORMAT_SHORT = "%d.%d.%s"; 30 public static final String FORMAT_SHORT = "%d.%d.%s";
31 + public static final String FORMAT_LONG = "%d.%d.%s.%s";
31 32
32 private static final String NEGATIVE = "Version segment cannot be negative"; 33 private static final String NEGATIVE = "Version segment cannot be negative";
33 34
...@@ -44,9 +45,12 @@ public final class Version { ...@@ -44,9 +45,12 @@ public final class Version {
44 this.minor = minor; 45 this.minor = minor;
45 this.patch = patch; 46 this.patch = patch;
46 this.build = build; 47 this.build = build;
47 - this.format = isNullOrEmpty(build) ? 48 + this.format =
49 + isNullOrEmpty(patch) ?
50 + String.format(FORMAT_MINIMAL, major, minor) :
51 + (isNullOrEmpty(build) ?
48 String.format(FORMAT_SHORT, major, minor, patch) : 52 String.format(FORMAT_SHORT, major, minor, patch) :
49 - String.format(FORMAT, major, minor, patch, build); 53 + String.format(FORMAT_LONG, major, minor, patch, build));
50 } 54 }
51 55
52 56
...@@ -74,7 +78,8 @@ public final class Version { ...@@ -74,7 +78,8 @@ public final class Version {
74 public static Version version(String string) { 78 public static Version version(String string) {
75 String[] fields = string.split("[.-]"); 79 String[] fields = string.split("[.-]");
76 return new Version(parseInt(fields[0]), parseInt(fields[1]), 80 return new Version(parseInt(fields[0]), parseInt(fields[1]),
77 - fields[2], fields.length == 4 ? fields[3] : null); 81 + fields.length >= 3 ? fields[2] : null,
82 + fields.length >= 4 ? fields[3] : null);
78 } 83 }
79 84
80 /** 85 /**
......
...@@ -64,6 +64,14 @@ public class VersionTest { ...@@ -64,6 +64,14 @@ public class VersionTest {
64 } 64 }
65 65
66 @Test 66 @Test
67 + public void minimal() {
68 + Version v = version("1.4");
69 + assertEquals("wrong major", 1, v.major());
70 + assertEquals("wrong minor", 4, v.minor());
71 + assertEquals("wrong patch", null, v.patch());
72 + assertEquals("wrong build", null, v.build());
73 + }
74 + @Test
67 public void testEquals() { 75 public void testEquals() {
68 new EqualsTester() 76 new EqualsTester()
69 .addEqualityGroup(version("1.2.3.4321"), version(1, 2, "3", "4321")) 77 .addEqualityGroup(version("1.2.3.4321"), version(1, 2, "3", "4321"))
......