2525
2626public final class SemanticVersion {
2727
28- private static final String BUILD_SEPERATOR = "+" ;
28+ private static final String BUILD_SEPERATOR = "\\ +" ;
2929 private static final String PRE_RELEASE_SEPERATOR = "-" ;
3030
3131 private final String version ;
@@ -54,8 +54,10 @@ public int compare(SemanticVersion targetedVersion) throws Exception {
5454 if (userVersionPartInt == null ) {
5555 // Compare strings
5656 int result = userVersionParts [index ].compareTo (targetedVersionParts [index ]);
57- if (result != 0 ) {
58- return result ;
57+ if (result < 0 ) {
58+ return targetedVersion .isPreRelease () && !isPreRelease () ? 1 : -1 ;
59+ } else if (result > 0 ) {
60+ return !targetedVersion .isPreRelease () && isPreRelease () ? -1 : 1 ;
5961 }
6062 } else if (targetVersionPartInt != null ) {
6163 if (!userVersionPartInt .equals (targetVersionPartInt )) {
@@ -75,11 +77,25 @@ public int compare(SemanticVersion targetedVersion) throws Exception {
7577 }
7678
7779 public boolean isPreRelease () {
78- return version .contains (PRE_RELEASE_SEPERATOR );
80+ int buildIndex = version .indexOf ("+" );
81+ int preReleaseIndex = version .indexOf ("-" );
82+ if (buildIndex < 0 ) {
83+ return preReleaseIndex > 0 ;
84+ } else if (preReleaseIndex < 0 ) {
85+ return false ;
86+ }
87+ return preReleaseIndex < buildIndex ;
7988 }
8089
8190 public boolean isBuild () {
82- return version .contains (BUILD_SEPERATOR );
91+ int buildIndex = version .indexOf ("+" );
92+ int preReleaseIndex = version .indexOf ("-" );
93+ if (preReleaseIndex < 0 ) {
94+ return buildIndex > 0 ;
95+ } else if (buildIndex < 0 ) {
96+ return false ;
97+ }
98+ return buildIndex < preReleaseIndex ;
8399 }
84100
85101 private int dotCount (String prefixVersion ) {
@@ -93,6 +109,17 @@ private int dotCount(String prefixVersion) {
93109 return count ;
94110 }
95111
112+ private boolean isValidBuildMetadata () {
113+ char [] vCharArray = version .toCharArray ();
114+ int count = 0 ;
115+ for (char c : vCharArray ) {
116+ if (c == '+' ) {
117+ count ++;
118+ }
119+ }
120+ return count > 1 ;
121+ }
122+
96123 public String [] splitSemanticVersion () throws Exception {
97124 List <String > versionParts = new ArrayList <>();
98125 String versionPrefix = "" ;
@@ -102,13 +129,13 @@ public String[] splitSemanticVersion() throws Exception {
102129 String [] preVersionParts ;
103130
104131 // Contains white spaces
105- if (version .contains (" " )) { // log and throw error
106- throw new Exception ("Semantic version contains white spaces. Invalid Semantic Version." );
132+ if (version .contains (" " ) || isValidBuildMetadata () ) { // log and throw error
133+ throw new Exception ("Invalid Semantic Version." );
107134 }
108135
109136 if (isBuild () || isPreRelease ()) {
110137 String [] partialVersionParts = version .split (isPreRelease () ?
111- PRE_RELEASE_SEPERATOR : BUILD_SEPERATOR );
138+ PRE_RELEASE_SEPERATOR : BUILD_SEPERATOR , 2 );
112139
113140 if (partialVersionParts .length <= 1 ) {
114141 // throw error
0 commit comments