-
Notifications
You must be signed in to change notification settings - Fork 32
feature: add semantic version types and test #386
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 17 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
65c1641
semantic versioning
mnoman09 e87e914
Merge branch 'master' into mnoman/semVersion
mnoman09 856aa7e
Fix
mnoman09 23f26cf
added comments and refact
mnoman09 dcb982f
comments fix
mnoman09 3d4c5c3
Merge branch 'master' into mnoman/semVersion
mnoman09 db372cb
Added tests and refact logic
mnoman09 6a469e6
refact
mnoman09 460c908
Refactored entire logic of Semantic version comparision and added new…
mnoman09 741dc37
Nit fix
mnoman09 856bc65
Merge branch 'master' into mnoman/semVersion
mnoman09 61f42fc
Added Additional tests for Semantic Versioning
mnoman09 6af5312
Allowed numbers to parse
mnoman09 a985821
Merge branch 'master' into mnoman/semVersion
mnoman09 37ccebf
Revert "Allowed numbers to parse"
mnoman09 861b0df
Refactored Logic and made it similar to other sdks
mnoman09 42e349c
SpotBug bug fix
mnoman09 0a2b631
Removed static functions
mnoman09 755bfed
remove string field and use proprety version
thomaszurkan-optimizely File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
core-api/src/main/java/com/optimizely/ab/config/audience/match/GEMatch.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| /** | ||
| * | ||
| * Copyright 2020, Optimizely and contributors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.optimizely.ab.config.audience.match; | ||
|
|
||
| import javax.annotation.Nullable; | ||
|
|
||
| import static com.optimizely.ab.internal.AttributesUtil.isValidNumber; | ||
|
|
||
| class GEMatch extends AttributeMatch<Number> { | ||
| Number value; | ||
|
|
||
| protected GEMatch(Number value) { | ||
| this.value = value; | ||
| } | ||
|
|
||
| @Nullable | ||
| public Boolean eval(Object attributeValue) { | ||
| try { | ||
| if(isValidNumber(attributeValue)) { | ||
| return castToValueType(attributeValue, value).doubleValue() >= value.doubleValue(); | ||
| } | ||
| } catch (Exception e) { | ||
| return null; | ||
| } | ||
| return null; | ||
| } | ||
| } |
42 changes: 42 additions & 0 deletions
42
core-api/src/main/java/com/optimizely/ab/config/audience/match/LEMatch.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /** | ||
| * | ||
| * Copyright 2020, Optimizely and contributors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.optimizely.ab.config.audience.match; | ||
|
|
||
| import javax.annotation.Nullable; | ||
|
|
||
| import static com.optimizely.ab.internal.AttributesUtil.isValidNumber; | ||
|
|
||
| class LEMatch extends AttributeMatch<Number> { | ||
| Number value; | ||
|
|
||
| protected LEMatch(Number value) { | ||
| this.value = value; | ||
| } | ||
|
|
||
| @Nullable | ||
| public Boolean eval(Object attributeValue) { | ||
| try { | ||
| if(isValidNumber(attributeValue)) { | ||
| return castToValueType(attributeValue, value).doubleValue() <= value.doubleValue(); | ||
| } | ||
| } catch (Exception e) { | ||
| return null; | ||
| } | ||
| return null; | ||
| } | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
core-api/src/main/java/com/optimizely/ab/config/audience/match/SemanticVersion.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| /** | ||
| * | ||
| * Copyright 2020, Optimizely and contributors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.optimizely.ab.config.audience.match; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| import static com.optimizely.ab.internal.AttributesUtil.parseNumeric; | ||
| import static com.optimizely.ab.internal.AttributesUtil.stringIsNullOrEmpty; | ||
|
|
||
| public final class SemanticVersion { | ||
|
|
||
| private final String version; | ||
|
|
||
| public SemanticVersion(String version) { | ||
| this.version = version; | ||
| } | ||
|
|
||
| public int compare(SemanticVersion targetedVersion) throws Exception { | ||
|
|
||
| if (targetedVersion == null || stringIsNullOrEmpty(targetedVersion.version)) { | ||
| return 0; | ||
| } | ||
|
|
||
| String[] targetedVersionParts = SemanticVersionExtension.splitSemanticVersion(targetedVersion.version); | ||
| String[] userVersionParts = SemanticVersionExtension.splitSemanticVersion(version); | ||
|
|
||
| for (int index = 0; index < targetedVersionParts.length; index++) { | ||
|
|
||
| if (userVersionParts.length <= index) { | ||
| return SemanticVersionExtension.isPreRelease(targetedVersion.version) ? 1 : -1; | ||
| } | ||
| Integer targetVersionPartInt = parseNumeric(targetedVersionParts[index]); | ||
| Integer userVersionPartInt = parseNumeric(userVersionParts[index]); | ||
|
|
||
| if (userVersionPartInt == null) { | ||
| // Compare strings | ||
| int result = userVersionParts[index].compareTo(targetedVersionParts[index]); | ||
| if (result != 0) { | ||
| return result; | ||
| } | ||
| } else if (targetVersionPartInt != null) { | ||
| if (!userVersionPartInt.equals(targetVersionPartInt)) { | ||
| return userVersionPartInt < targetVersionPartInt ? -1 : 1; | ||
| } | ||
| } else { | ||
| return -1; | ||
| } | ||
| } | ||
|
|
||
| if (!SemanticVersionExtension.isPreRelease(targetedVersion.version) && | ||
| SemanticVersionExtension.isPreRelease(version)) { | ||
| return -1; | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| public static class SemanticVersionExtension { | ||
| public static final String BUILD_SEPERATOR = "+"; | ||
| public static final String PRE_RELEASE_SEPERATOR = "-"; | ||
|
|
||
| public static boolean isPreRelease(String semanticVersion) { | ||
| return semanticVersion.contains(PRE_RELEASE_SEPERATOR); | ||
| } | ||
|
|
||
| public static boolean isBuild(String semanticVersion) { | ||
| return semanticVersion.contains(BUILD_SEPERATOR); | ||
| } | ||
|
|
||
| public static String[] splitSemanticVersion(String version) throws Exception { | ||
| List<String> versionParts = new ArrayList<>(); | ||
| // pre-release or build. | ||
| String versionSuffix = ""; | ||
| // for example: beta.2.1 | ||
| String[] preVersionParts; | ||
|
|
||
| // Contains white spaces | ||
| if (version.contains(" ")) { // log and throw error | ||
| throw new Exception("Semantic version contains white spaces. Invalid Semantic Version."); | ||
| } | ||
|
|
||
| if (isBuild(version) || isPreRelease(version)) { | ||
| String[] partialVersionParts = version.split(isPreRelease(version) ? | ||
| PRE_RELEASE_SEPERATOR : BUILD_SEPERATOR); | ||
|
|
||
| if (partialVersionParts.length <= 1) { | ||
| // throw error | ||
| throw new Exception("Invalid Semantic Version."); | ||
| } | ||
| // major.minor.patch | ||
| String versionPrefix = partialVersionParts[0]; | ||
|
|
||
| versionSuffix = partialVersionParts[1]; | ||
|
|
||
| preVersionParts = versionPrefix.split("\\."); | ||
| } else { | ||
| preVersionParts = version.split("\\."); | ||
| } | ||
|
|
||
| if (preVersionParts.length > 3) { | ||
| // Throw error as pre version should only contain major.minor.patch version | ||
| throw new Exception("Invalid Semantic Version."); | ||
| } | ||
|
|
||
| for (String preVersionPart : preVersionParts) { | ||
| if (parseNumeric(preVersionPart) == null) { | ||
| throw new Exception("Invalid Semantic Version."); | ||
| } | ||
| } | ||
|
|
||
| Collections.addAll(versionParts, preVersionParts); | ||
| if (!stringIsNullOrEmpty(versionSuffix)) { | ||
| versionParts.add(versionSuffix); | ||
| } | ||
|
|
||
| return versionParts.toArray(new String[0]); | ||
| } | ||
| } | ||
| } | ||
41 changes: 41 additions & 0 deletions
41
...api/src/main/java/com/optimizely/ab/config/audience/match/SemanticVersionEqualsMatch.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| /** | ||
| * | ||
| * Copyright 2020, Optimizely and contributors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.optimizely.ab.config.audience.match; | ||
|
|
||
| import javax.annotation.Nullable; | ||
|
|
||
| class SemanticVersionEqualsMatch implements Match { | ||
| String value; | ||
|
|
||
| protected SemanticVersionEqualsMatch(String value) { | ||
| this.value = value; | ||
| } | ||
|
|
||
| @Nullable | ||
| public Boolean eval(Object attributeValue) { | ||
| try { | ||
| if (this.value != null && attributeValue instanceof String) { | ||
| SemanticVersion conditionalVersion = new SemanticVersion(value); | ||
| SemanticVersion userSemanticVersion = new SemanticVersion((String) attributeValue); | ||
| return userSemanticVersion.compare(conditionalVersion) == 0; | ||
| } | ||
| } catch (Exception e) { | ||
| return null; | ||
| } | ||
| return null; | ||
| } | ||
| } |
41 changes: 41 additions & 0 deletions
41
core-api/src/main/java/com/optimizely/ab/config/audience/match/SemanticVersionGEMatch.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| /** | ||
| * | ||
| * Copyright 2020, Optimizely and contributors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.optimizely.ab.config.audience.match; | ||
|
|
||
| import javax.annotation.Nullable; | ||
|
|
||
| class SemanticVersionGEMatch implements Match { | ||
| String value; | ||
|
|
||
| protected SemanticVersionGEMatch(String value) { | ||
| this.value = value; | ||
| } | ||
|
|
||
| @Nullable | ||
| public Boolean eval(Object attributeValue) { | ||
| try { | ||
| if (this.value != null && attributeValue instanceof String) { | ||
| SemanticVersion conditionalVersion = new SemanticVersion(value); | ||
| SemanticVersion userSemanticVersion = new SemanticVersion((String) attributeValue); | ||
| return userSemanticVersion.compare(conditionalVersion) >= 0; | ||
| } | ||
| } catch (Exception e) { | ||
| return null; | ||
| } | ||
| return null; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Look good. One nit. Why the inner static methods? They could be part of the semantic version and collapse some logic. so, isBuild() {self.version.contains... would be against semantic version, isPreRelease, and split would just use self.version. Perhaps there is something I'm missing?