-
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 5 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
67 changes: 67 additions & 0 deletions
67
.../src/main/java/com/optimizely/ab/config/audience/match/SemanticVersionAttributeMatch.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,67 @@ | ||
| /** | ||
| * | ||
| * 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; | ||
|
|
||
| abstract class SemanticVersionAttributeMatch implements Match { | ||
| public int compareVersion(String actualVersion, String targetVersion) { | ||
| if (actualVersion.equals(targetVersion)) { | ||
| // Any version. | ||
| return 0; | ||
| } | ||
|
|
||
| // Expect a version string of the form x.y.z-(string) | ||
| String[] actualVersionParts = actualVersion.split("[-\\.]"); | ||
mnoman09 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| String[] targetVersionParts = targetVersion.split("[-\\.]"); | ||
|
|
||
| // Check only till the precision point of actualVersionParts | ||
| for (int i = 0; i < actualVersionParts.length; i++) { | ||
mnoman09 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (i < targetVersionParts.length) { | ||
| Double actual = parseNumeric(actualVersionParts[i]); | ||
| Double target = parseNumeric(targetVersionParts[i]); | ||
| // Check if the both actual and target are number then compare else if compare the string and if it's not equal than return -1 | ||
| if (actual != null && target != null) { | ||
| if (actual < target) { | ||
| return -1; | ||
| } else if (actual > target) { | ||
| return 1; | ||
| } | ||
| } else if (!actualVersionParts[i].equals(targetVersionParts[i])) { | ||
| return -1; | ||
| } | ||
| } else { | ||
| // If actualVersionParts is greater than targetVersionParts and is not zero than return 1 else if actual is string then return -1 | ||
| // So if actualVersionParts[i] is beta/alpha then this means targetVersion is greater than actualVersion | ||
| Double actual = parseNumeric(actualVersionParts[i]); | ||
| if (actual != null && actual != 0) { | ||
| return 1; | ||
| } else if (actual == null) { | ||
| return -1; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| private static Double parseNumeric(String str) { | ||
mnoman09 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| try { | ||
| return Double.parseDouble(str); | ||
mnoman09 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } catch (NumberFormatException e) { | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
39 changes: 39 additions & 0 deletions
39
...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,39 @@ | ||
| /** | ||
| * | ||
| * 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 extends SemanticVersionAttributeMatch { | ||
| String value; | ||
|
|
||
| protected SemanticVersionEqualsMatch(String value) { | ||
| this.value = value; | ||
| } | ||
|
|
||
| @Nullable | ||
| public Boolean eval(Object attributeValue) { | ||
| try { | ||
| if (attributeValue instanceof String) { | ||
| return compareVersion(value, (String) attributeValue) == 0; | ||
| } | ||
| } catch (Exception e) { | ||
| return null; | ||
| } | ||
| return null; | ||
| } | ||
| } |
39 changes: 39 additions & 0 deletions
39
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,39 @@ | ||
| /** | ||
| * | ||
| * 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 extends SemanticVersionAttributeMatch { | ||
| String value; | ||
|
|
||
| protected SemanticVersionGEMatch(String value) { | ||
| this.value = value; | ||
| } | ||
|
|
||
| @Nullable | ||
| public Boolean eval(Object attributeValue) { | ||
| try { | ||
| if (attributeValue instanceof String) { | ||
| return compareVersion(value, (String) attributeValue) <= 0; | ||
| } | ||
| } catch (Exception e) { | ||
| return null; | ||
| } | ||
| return null; | ||
| } | ||
| } |
39 changes: 39 additions & 0 deletions
39
core-api/src/main/java/com/optimizely/ab/config/audience/match/SemanticVersionGTMatch.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,39 @@ | ||
| /** | ||
| * | ||
| * 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 SemanticVersionGTMatch extends SemanticVersionAttributeMatch { | ||
| String value; | ||
|
|
||
| protected SemanticVersionGTMatch(String target) { | ||
| this.value = target; | ||
| } | ||
|
|
||
| @Nullable | ||
| public Boolean eval(Object attributeValue) { | ||
| try { | ||
| if (attributeValue instanceof String) { | ||
| return compareVersion(value, (String) attributeValue) < 0; | ||
| } | ||
| } catch (Exception e) { | ||
| return null; | ||
| } | ||
| return null; | ||
| } | ||
| } |
39 changes: 39 additions & 0 deletions
39
core-api/src/main/java/com/optimizely/ab/config/audience/match/SemanticVersionLEMatch.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,39 @@ | ||
| /** | ||
| * | ||
| * 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 SemanticVersionLEMatch extends SemanticVersionAttributeMatch { | ||
| String value; | ||
|
|
||
| protected SemanticVersionLEMatch(String target) { | ||
| this.value = target; | ||
| } | ||
|
|
||
| @Nullable | ||
| public Boolean eval(Object attributeValue) { | ||
| try { | ||
| if (attributeValue instanceof String) { | ||
| return compareVersion(value, (String) attributeValue) >= 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.
Uh oh!
There was an error while loading. Please reload this page.