Version { public static Version parseVersion( String rawVersion ) { Version result = null; Matcher m = Pattern.compile( "^(\\d+)\\.(\\d+)(\\.\\d+)?([.-].+)?$" ).matcher( rawVersion ); if( m.find()) { result = new Version( Integer.parseInt( m.group( 1 )), Integer.parseInt( m.group( 2 )), m.group( 3 ) == null ? 0 : Integer.parseInt( m.group( 3 ).substring( 1 )), m.group( 4 ) == null ? null : m.group( 4 ).substring( 1 )); } return result; } private  Version( int major, int minor, int patch, String qualifier ); int getMajor(); int getMinor(); int getPatch(); String getQualifier(); static Version parseVersion( String rawVersion );  }
@Test public void testParseVersion() { Assert.assertNull( Version.parseVersion( "v1" )); Assert.assertNull( Version.parseVersion( "3.1." )); Assert.assertNull( Version.parseVersion( "3.t.4" )); Assert.assertNull( Version.parseVersion( "3-1-4" )); Version v = Version.parseVersion( "5.10.9" ); Assert.assertEquals( 5, v.getMajor()); Assert.assertEquals( 10, v.getMinor()); Assert.assertEquals( 9, v.getPatch()); Assert.assertNull( v.getQualifier()); v = Version.parseVersion( "71.1.0.snapshot" ); Assert.assertEquals( 71, v.getMajor()); Assert.assertEquals( 1, v.getMinor()); Assert.assertEquals( 0, v.getPatch()); Assert.assertEquals( "snapshot", v.getQualifier()); v = Version.parseVersion( "4.7.84-SNAPSHOT" ); Assert.assertEquals( 4, v.getMajor()); Assert.assertEquals( 7, v.getMinor()); Assert.assertEquals( 84, v.getPatch()); Assert.assertEquals( "SNAPSHOT", v.getQualifier()); v = Version.parseVersion( "3.11" ); Assert.assertEquals( 3, v.getMajor()); Assert.assertEquals( 11, v.getMinor()); Assert.assertEquals( 0, v.getPatch()); Assert.assertNull( v.getQualifier()); v = Version.parseVersion( "8.4-SNAPSHOT" ); Assert.assertEquals( 8, v.getMajor()); Assert.assertEquals( 4, v.getMinor()); Assert.assertEquals( 0, v.getPatch()); Assert.assertEquals( "SNAPSHOT", v.getQualifier()); v = Version.parseVersion( "4.7.84.SNAP-SHOT" ); Assert.assertEquals( 4, v.getMajor()); Assert.assertEquals( 7, v.getMinor()); Assert.assertEquals( 84, v.getPatch()); Assert.assertEquals( "SNAP-SHOT", v.getQualifier()); v = Version.parseVersion( "4.7.84.SNAP.SHOT" ); Assert.assertEquals( 4, v.getMajor()); Assert.assertEquals( 7, v.getMinor()); Assert.assertEquals( 84, v.getPatch()); Assert.assertEquals( "SNAP.SHOT", v.getQualifier()); v = Version.parseVersion( "4.7.SNAP.SHOT" ); Assert.assertEquals( 4, v.getMajor()); Assert.assertEquals( 7, v.getMinor()); Assert.assertEquals( 0, v.getPatch()); Assert.assertEquals( "SNAP.SHOT", v.getQualifier()); }