Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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 @@ -55,11 +55,46 @@ public static MatchType getMatchType(String matchType, Object conditionValue) th
return new MatchType(matchType, new GTMatch((Number) conditionValue));
}
break;
case "ge":
if (isValidNumber(conditionValue)) {
return new MatchType(matchType, new GEMatch((Number) conditionValue));
}
break;
case "lt":
if (isValidNumber(conditionValue)) {
return new MatchType(matchType, new LTMatch((Number) conditionValue));
}
break;
case "le":
if (isValidNumber(conditionValue)) {
return new MatchType(matchType, new LEMatch((Number) conditionValue));
}
break;
case "semver_eq":
if (conditionValue instanceof String) {
return new MatchType(matchType, new SemanticVersionEqualsMatch((String) conditionValue));
}
break;
case "semver_lt":
if (conditionValue instanceof String) {
return new MatchType(matchType, new SemanticVersionLTMatch((String) conditionValue));
}
break;
case "semver_le":
if (conditionValue instanceof String) {
return new MatchType(matchType, new SemanticVersionLEMatch((String) conditionValue));
}
break;
case "semver_gt":
if (conditionValue instanceof String) {
return new MatchType(matchType, new SemanticVersionGTMatch((String) conditionValue));
}
break;
case "semver_ge":
if (conditionValue instanceof String) {
return new MatchType(matchType, new SemanticVersionGEMatch((String) conditionValue));
}
break;
case "legacy_custom_attribute":
if (conditionValue instanceof String) {
return new MatchType(matchType, new DefaultMatchForLegacyAttributes<String>((String) conditionValue));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
*
* 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 {

/**
* Compare an actual version against a targetedVersion; return -1 if the actual version is "semantically less"
* than the targetedVersion, 1 if it is "semantically greater", and 0 if they are "semantically identical".
*
* "Semantically" means the following: given both version numbers expressed in x.y.z... format, to the level of
* precision of the targetedVersion, compare the corresponding version parts (e.g. major to major, minor to minor).
*
* @param version expressed as a string x.y.z...
* @param targetedVersion expressed as a string x.y.z...
* @return -1 if version < targetedVersion, 1 if version > targetedVersion, 0 if they are approx. equal
*/
public int compareVersion(String version, String targetedVersion) {
if (targetedVersion == null || targetedVersion.isEmpty()) {
// Any version.
return 0;
}

// Expect a version string of the form x.y.z
String[] versionParts = version.split("\\.");
String[] targetVersionParts = targetedVersion.split("\\.");

// Check only till the precision point of targetVersionParts
for (int targetIndex = 0; targetIndex < targetVersionParts.length; targetIndex++) {
if ((versionParts.length - 1) < targetIndex) {
return -1;
}
Double part = parseNumeric(versionParts[targetIndex]);
Double target = parseNumeric(targetVersionParts[targetIndex]);

if (part == null) {
//Compare strings
if (!versionParts[targetIndex].equals(targetVersionParts[targetIndex])) {
return -1;
}
} else if (target != null) {
if (part < target) {
return -1;
} else if (part > target) {
return 1;
}
}
}
return 0;
}

private static Double parseNumeric(String str) {
try {
return Double.parseDouble(str);
} catch (NumberFormatException e) {
return null;
}
}
}
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;
}
}
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;
}
}
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;
}
}
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;
}
}
Loading