Search in sources :

Example 1 with EmptyCondition

use of com.optimizely.ab.config.audience.EmptyCondition in project java-sdk by optimizely.

the class ConditionUtils method parseConditions.

/**
 * parse conditions using List and Map
 *
 * @param clazz the class of parsed condition
 * @param rawObjectList list of conditions
 * @param <T> This is the type parameter
 * @return audienceCondition
 */
public static <T> Condition parseConditions(Class<T> clazz, List<Object> rawObjectList) throws InvalidAudienceCondition {
    if (rawObjectList.size() == 0) {
        return new EmptyCondition();
    }
    List<Condition> conditions = new ArrayList<Condition>();
    int startingParseIndex = 0;
    String operand = operand(rawObjectList.get(startingParseIndex));
    if (operand != null) {
        startingParseIndex = 1;
    } else {
        operand = "or";
    }
    for (int i = startingParseIndex; i < rawObjectList.size(); i++) {
        Object obj = rawObjectList.get(i);
        conditions.add(parseConditions(clazz, obj));
    }
    return buildCondition(operand, conditions);
}
Also used : EmptyCondition(com.optimizely.ab.config.audience.EmptyCondition) Condition(com.optimizely.ab.config.audience.Condition) NullCondition(com.optimizely.ab.config.audience.NullCondition) OrCondition(com.optimizely.ab.config.audience.OrCondition) NotCondition(com.optimizely.ab.config.audience.NotCondition) AndCondition(com.optimizely.ab.config.audience.AndCondition) AudienceIdCondition(com.optimizely.ab.config.audience.AudienceIdCondition) EmptyCondition(com.optimizely.ab.config.audience.EmptyCondition) ArrayList(java.util.ArrayList) JSONObject(org.json.simple.JSONObject)

Example 2 with EmptyCondition

use of com.optimizely.ab.config.audience.EmptyCondition in project java-sdk by optimizely.

the class ConditionUtils method parseConditions.

/**
 * Parse conditions from org.json.JsonArray
 *
 * @param clazz the class of parsed condition
 * @param conditionJson jsonArray to parse
 * @param <T> This is the type parameter
 * @return condition parsed from conditionJson.
 */
public static <T> Condition parseConditions(Class<T> clazz, org.json.JSONArray conditionJson) throws InvalidAudienceCondition {
    if (conditionJson.length() == 0) {
        return new EmptyCondition();
    }
    List<Condition> conditions = new ArrayList<Condition>();
    int startingParseIndex = 0;
    String operand = operand(conditionJson.get(startingParseIndex));
    if (operand != null) {
        startingParseIndex = 1;
    } else {
        operand = "or";
    }
    for (int i = startingParseIndex; i < conditionJson.length(); i++) {
        Object obj = conditionJson.get(i);
        conditions.add(parseConditions(clazz, obj));
    }
    return buildCondition(operand, conditions);
}
Also used : EmptyCondition(com.optimizely.ab.config.audience.EmptyCondition) Condition(com.optimizely.ab.config.audience.Condition) NullCondition(com.optimizely.ab.config.audience.NullCondition) OrCondition(com.optimizely.ab.config.audience.OrCondition) NotCondition(com.optimizely.ab.config.audience.NotCondition) AndCondition(com.optimizely.ab.config.audience.AndCondition) AudienceIdCondition(com.optimizely.ab.config.audience.AudienceIdCondition) EmptyCondition(com.optimizely.ab.config.audience.EmptyCondition) ArrayList(java.util.ArrayList) JSONObject(org.json.simple.JSONObject)

Example 3 with EmptyCondition

use of com.optimizely.ab.config.audience.EmptyCondition in project java-sdk by optimizely.

the class ConditionJacksonDeserializer method parseConditions.

protected static <T> Condition parseConditions(Class<T> clazz, ObjectMapper objectMapper, JsonNode conditionNode) throws JsonProcessingException, InvalidAudienceCondition {
    if (conditionNode.isArray() && conditionNode.size() == 0) {
        return new EmptyCondition();
    }
    List<Condition> conditions = new ArrayList<>();
    int startingParsingIndex = 0;
    JsonNode opNode = conditionNode.get(0);
    String operand = operand(opNode);
    if (operand == null) {
        operand = "or";
    } else {
        // the operand is valid so move to the next node.
        startingParsingIndex = 1;
    }
    for (int i = startingParsingIndex; i < conditionNode.size(); i++) {
        JsonNode subNode = conditionNode.get(i);
        conditions.add(ConditionJacksonDeserializer.<T>parseCondition(clazz, objectMapper, subNode));
    }
    Condition condition;
    switch(operand) {
        case "and":
            condition = new AndCondition(conditions);
            break;
        case "not":
            condition = new NotCondition(conditions.isEmpty() ? new NullCondition() : conditions.get(0));
            break;
        default:
            condition = new OrCondition(conditions);
            break;
    }
    return condition;
}
Also used : EmptyCondition(com.optimizely.ab.config.audience.EmptyCondition) Condition(com.optimizely.ab.config.audience.Condition) NullCondition(com.optimizely.ab.config.audience.NullCondition) OrCondition(com.optimizely.ab.config.audience.OrCondition) NotCondition(com.optimizely.ab.config.audience.NotCondition) AndCondition(com.optimizely.ab.config.audience.AndCondition) InvalidAudienceCondition(com.optimizely.ab.internal.InvalidAudienceCondition) AudienceIdCondition(com.optimizely.ab.config.audience.AudienceIdCondition) EmptyCondition(com.optimizely.ab.config.audience.EmptyCondition) NotCondition(com.optimizely.ab.config.audience.NotCondition) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) NullCondition(com.optimizely.ab.config.audience.NullCondition) OrCondition(com.optimizely.ab.config.audience.OrCondition) AndCondition(com.optimizely.ab.config.audience.AndCondition)

Aggregations

AndCondition (com.optimizely.ab.config.audience.AndCondition)3 AudienceIdCondition (com.optimizely.ab.config.audience.AudienceIdCondition)3 Condition (com.optimizely.ab.config.audience.Condition)3 EmptyCondition (com.optimizely.ab.config.audience.EmptyCondition)3 NotCondition (com.optimizely.ab.config.audience.NotCondition)3 NullCondition (com.optimizely.ab.config.audience.NullCondition)3 OrCondition (com.optimizely.ab.config.audience.OrCondition)3 ArrayList (java.util.ArrayList)3 JSONObject (org.json.simple.JSONObject)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 InvalidAudienceCondition (com.optimizely.ab.internal.InvalidAudienceCondition)1