25
25
26
26
public final class SemanticVersion {
27
27
28
- private static final String BUILD_SEPERATOR = "+" ;
28
+ private static final String BUILD_SEPERATOR = "\\ +" ;
29
29
private static final String PRE_RELEASE_SEPERATOR = "-" ;
30
30
31
31
private final String version ;
@@ -54,8 +54,10 @@ public int compare(SemanticVersion targetedVersion) throws Exception {
54
54
if (userVersionPartInt == null ) {
55
55
// Compare strings
56
56
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 ;
59
61
}
60
62
} else if (targetVersionPartInt != null ) {
61
63
if (!userVersionPartInt .equals (targetVersionPartInt )) {
@@ -75,11 +77,25 @@ public int compare(SemanticVersion targetedVersion) throws Exception {
75
77
}
76
78
77
79
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 ;
79
88
}
80
89
81
90
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 ;
83
99
}
84
100
85
101
private int dotCount (String prefixVersion ) {
@@ -93,6 +109,17 @@ private int dotCount(String prefixVersion) {
93
109
return count ;
94
110
}
95
111
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
+
96
123
public String [] splitSemanticVersion () throws Exception {
97
124
List <String > versionParts = new ArrayList <>();
98
125
String versionPrefix = "" ;
@@ -102,13 +129,13 @@ public String[] splitSemanticVersion() throws Exception {
102
129
String [] preVersionParts ;
103
130
104
131
// 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." );
107
134
}
108
135
109
136
if (isBuild () || isPreRelease ()) {
110
137
String [] partialVersionParts = version .split (isPreRelease () ?
111
- PRE_RELEASE_SEPERATOR : BUILD_SEPERATOR );
138
+ PRE_RELEASE_SEPERATOR : BUILD_SEPERATOR , 2 );
112
139
113
140
if (partialVersionParts .length <= 1 ) {
114
141
// throw error
0 commit comments