Skip to content
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
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;
}
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright 2018-2019, Optimizely and contributors
* Copyright 2018-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.
Expand Down Expand Up @@ -50,11 +50,21 @@ public static MatchType getMatchType(String matchType, Object conditionValue) th
return new MatchType(matchType, new SubstringMatch((String) conditionValue));
}
break;
case "ge":
if (isValidNumber(conditionValue)) {
return new MatchType(matchType, new GEMatch((Number) conditionValue));
}
break;
case "gt":
if (isValidNumber(conditionValue)) {
return new MatchType(matchType, new GTMatch((Number) conditionValue));
}
break;
case "le":
if (isValidNumber(conditionValue)) {
return new MatchType(matchType, new LEMatch((Number) conditionValue));
}
break;
case "lt":
if (isValidNumber(conditionValue)) {
return new MatchType(matchType, new LTMatch((Number) conditionValue));
Expand All @@ -65,6 +75,31 @@ public static MatchType getMatchType(String matchType, Object conditionValue) th
return new MatchType(matchType, new DefaultMatchForLegacyAttributes<String>((String) conditionValue));
}
break;
case "semver_eq":
if (conditionValue instanceof String) {
return new MatchType(matchType, new SemanticVersionEqualsMatch((String) conditionValue));
}
break;
case "semver_ge":
if (conditionValue instanceof String) {
return new MatchType(matchType, new SemanticVersionGEMatch((String) conditionValue));
}
break;
case "semver_gt":
if (conditionValue instanceof String) {
return new MatchType(matchType, new SemanticVersionGTMatch((String) conditionValue));
}
break;
case "semver_le":
if (conditionValue instanceof String) {
return new MatchType(matchType, new SemanticVersionLEMatch((String) conditionValue));
}
break;
case "semver_lt":
if (conditionValue instanceof String) {
return new MatchType(matchType, new SemanticVersionLTMatch((String) conditionValue));
}
break;
default:
throw new UnknownMatchTypeException();
}
Expand Down
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 {
Copy link
Contributor

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?

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]);
}
}
}
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;
}
}
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;
}
}
Loading