2828
2929public class DockerVersion implements Serializable {
3030
31+ public static final DockerVersion DEFAULT = new DockerVersion (0 , 0 , 0 , null ,
32+ "0" );
33+
3134 private int major ;
3235 private int minor ;
3336 private int patch ;
@@ -52,23 +55,28 @@ private DockerVersion(int major,
5255 *
5356 * @return
5457 */
55- public static DockerVersion fromVersionString (String versionString ) {
56- String [] split = versionString .split ("\\ s" );
57- String build = split [split .length - 1 ];
58- String [] version = split [2 ].substring (0 , split [2 ].length () - 1 ).split (
59- "\\ ." );
60- int major = Integer .parseInt (version [0 ]);
61- int minor = Integer .parseInt (version [1 ]);
62- int patch = 0 ;
63- String extra = null ;
64- if (version [2 ].contains ("-" )) {
65- patch = Integer .parseInt (
66- version [2 ].substring (0 , version [2 ].indexOf ('-' )));
67- extra = version [2 ].substring (version [2 ].indexOf ('-' ) + 1 );
68- } else {
69- patch = Integer .parseInt (version [2 ]);
58+ public static DockerVersion fromVersionString (String versionString ) throws VersionParseException {
59+ try {
60+ String [] split = versionString .split ("\\ s" );
61+ String build = split [split .length - 1 ];
62+ String [] version = split [2 ].substring (0 , split [2 ].length () - 1 )
63+ .split (
64+ "\\ ." );
65+ int major = Integer .parseInt (version [0 ]);
66+ int minor = Integer .parseInt (version [1 ]);
67+ int patch = 0 ;
68+ String extra = null ;
69+ if (version [2 ].contains ("-" )) {
70+ patch = Integer .parseInt (
71+ version [2 ].substring (0 , version [2 ].indexOf ('-' )));
72+ extra = version [2 ].substring (version [2 ].indexOf ('-' ) + 1 );
73+ } else {
74+ patch = Integer .parseInt (version [2 ]);
75+ }
76+ return new DockerVersion (major , minor , patch , extra , build );
77+ } catch (Exception e ) {
78+ throw new VersionParseException (versionString , e );
7079 }
71- return new DockerVersion (major , minor , patch , extra , build );
7280 }
7381
7482 public String getVersionString () {
@@ -91,4 +99,11 @@ public String toString() {
9199 build );
92100 }
93101 }
102+
103+ public static class VersionParseException extends Exception {
104+
105+ public VersionParseException (String version , Throwable cause ) {
106+ super (String .format ("Could not parse '%s'" , version ), cause );
107+ }
108+ }
94109}
0 commit comments