Search in sources :

Example 1 with Operation

use of com.tvd12.dahlia.math.Operation in project dahlia by youngmonkeys.

the class QueryToPredicate method toDefaultPredicate.

protected Predicate<EzyObject> toDefaultPredicate(EzyObject query) {
    for (Entry kv : query.entrySet()) {
        String field = null;
        Object value = kv.getValue();
        String fieldOrOperation = (String) kv.getKey();
        Operation operation = Operation.valueOfKeyword(fieldOrOperation);
        if (operation != null) {
            EzyObject oValue = (EzyObject) value;
            for (Entry e : oValue.entrySet()) {
                field = (String) e.getKey();
                value = e.getValue();
                break;
            }
        } else if (value instanceof EzyObject) {
            EzyObject oValue = (EzyObject) value;
            for (Entry e : oValue.entrySet()) {
                String keyword = (String) e.getKey();
                operation = Operation.valueOfKeyword(keyword);
                value = e.getValue();
                break;
            }
        } else {
            field = fieldOrOperation;
            operation = Operation.EQ;
        }
        String qField = field;
        Object qValue = value;
        Operation qOperation = operation;
        return record -> {
            Object rValue = record.get(qField);
            int compareResult = compareValue(rValue, qValue);
            if (qOperation == Operation.GT)
                return compareResult > 0;
            if (qOperation == Operation.GTE)
                return compareResult >= 0;
            if (qOperation == Operation.LT)
                return compareResult < 0;
            if (qOperation == Operation.LTE)
                return compareResult <= 0;
            if (qOperation == Operation.NEQ)
                return compareResult != 0;
            return compareResult == 0;
        };
    }
    return record -> false;
}
Also used : EzyPredicates(com.tvd12.ezyfox.function.EzyPredicates) EzyArray(com.tvd12.ezyfox.entity.EzyArray) Predicate(java.util.function.Predicate) HashMap(java.util.HashMap) Function(java.util.function.Function) ArrayList(java.util.ArrayList) InvalidQueryException(com.tvd12.dahlia.exception.InvalidQueryException) Operation(com.tvd12.dahlia.math.Operation) List(java.util.List) Map(java.util.Map) Keywords(com.tvd12.dahlia.constant.Keywords) EzyObject(com.tvd12.ezyfox.entity.EzyObject) Entry(java.util.Map.Entry) Comparators(com.tvd12.dahlia.core.comparator.Comparators) Comparator(java.util.Comparator) Entry(java.util.Map.Entry) EzyObject(com.tvd12.ezyfox.entity.EzyObject) Operation(com.tvd12.dahlia.math.Operation) EzyObject(com.tvd12.ezyfox.entity.EzyObject)

Aggregations

Keywords (com.tvd12.dahlia.constant.Keywords)1 Comparators (com.tvd12.dahlia.core.comparator.Comparators)1 InvalidQueryException (com.tvd12.dahlia.exception.InvalidQueryException)1 Operation (com.tvd12.dahlia.math.Operation)1 EzyArray (com.tvd12.ezyfox.entity.EzyArray)1 EzyObject (com.tvd12.ezyfox.entity.EzyObject)1 EzyPredicates (com.tvd12.ezyfox.function.EzyPredicates)1 ArrayList (java.util.ArrayList)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Function (java.util.function.Function)1 Predicate (java.util.function.Predicate)1