Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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,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("[-\\.]");
String[] targetVersionParts = targetVersion.split("[-\\.]");

// Check only till the precision point of actualVersionParts
for (int i = 0; i < actualVersionParts.length; i++) {
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) {
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