Example usage for org.apache.commons.collections.functors AnyPredicate getInstance

List of usage examples for org.apache.commons.collections.functors AnyPredicate getInstance

Introduction

In this page you can find the example usage for org.apache.commons.collections.functors AnyPredicate getInstance.

Prototype

public static Predicate getInstance(Collection predicates) 

Source Link

Document

Factory to create the predicate.

Usage

From source file:org.springmodules.validation.valang.predicates.GenericTestPredicate.java

/**
 * <p>The evaluate method takes the result of both functions and tests with the operator.
 *
 * @param target the target bean//from   w  w  w . j a  v a  2s .c  o m
 * @return the result of the test
 */
public boolean evaluate(Object target) {
    Object leftValue = getLeftFunction().getResult(target);
    Object rightValue = null;
    boolean dates = false;
    boolean numbers = false;

    if (getRightFunction() != null) {
        rightValue = getRightFunction().getResult(target);
    }

    if (leftValue instanceof Number) {
        leftValue = new BigDecimal(leftValue.toString());
    }
    if (rightValue instanceof Number) {
        rightValue = new BigDecimal(rightValue.toString());
    }

    dates = leftValue instanceof Date && rightValue instanceof Date;
    numbers = leftValue instanceof BigDecimal && rightValue instanceof BigDecimal;

    if (getOperator() instanceof Operator.NullOperator) {
        return leftValue == null;
    } else if (getOperator() instanceof Operator.NotNullOperator) {
        return leftValue != null;
    } else if (getOperator() instanceof Operator.EqualsOperator) {
        if (leftValue instanceof BigDecimal && rightValue instanceof BigDecimal) {
            return ((BigDecimal) leftValue).compareTo((BigDecimal) rightValue) == 0;
        } else if (dates) {
            return ((Date) leftValue).getTime() == ((Date) rightValue).getTime();
        } else {
            return leftValue.equals(rightValue);
        }
    } else if (getOperator() instanceof Operator.NotEqualsOperator) {
        if (leftValue instanceof BigDecimal && rightValue instanceof BigDecimal) {
            return ((BigDecimal) leftValue).compareTo((BigDecimal) rightValue) != 0;
        } else if (dates) {
            return ((Date) leftValue).getTime() != ((Date) rightValue).getTime();
        } else {
            return !leftValue.equals(rightValue);
        }
    } else if (getOperator() instanceof Operator.LessThanOperator) {
        if (dates) {
            return ((Date) leftValue).getTime() < ((Date) rightValue).getTime();
        } else if (numbers) {
            return ((BigDecimal) leftValue).compareTo((BigDecimal) rightValue) < 0;
        } else {
            throw new ValangException("< operator only supports two date or two number values!", getLine(),
                    getColumn());
        }
    } else if (getOperator() instanceof Operator.LessThanOrEqualOperator) {
        if (dates) {
            return ((Date) leftValue).getTime() <= ((Date) rightValue).getTime();
        } else if (numbers) {
            return ((BigDecimal) leftValue).compareTo((BigDecimal) rightValue) <= 0;
        } else {
            throw new ValangException("<= operator only supports two date or two number values!", getLine(),
                    getColumn());
        }
    } else if (getOperator() instanceof Operator.MoreThanOperator) {
        if (dates) {
            return ((Date) leftValue).getTime() > ((Date) rightValue).getTime();
        } else if (numbers) {
            return ((BigDecimal) leftValue).compareTo((BigDecimal) rightValue) > 0;
        } else {
            throw new ValangException("> operator only supports two date or two number values!", getLine(),
                    getColumn());
        }
    } else if (getOperator() instanceof Operator.MoreThanOrEqualOperator) {
        if (dates) {
            return ((Date) leftValue).getTime() >= ((Date) rightValue).getTime();
        } else if (numbers) {
            return ((BigDecimal) leftValue).compareTo((BigDecimal) rightValue) >= 0;
        } else {
            throw new IllegalArgumentException(">= operator only supports two date or two number values!");
        }
    } else if (getOperator() instanceof Operator.InOperator) {
        Collection predicates = new ArrayList();
        for (Iterator iter = getIterator(rightValue); iter.hasNext();) {
            Object o = iter.next();
            if (o instanceof Function) {
                predicates.add(getPredicate(new LiteralFunction(leftValue), OperatorConstants.EQUALS_OPERATOR,
                        (Function) o, getLine(), getColumn()));
            } else {
                predicates.add(getPredicate(new LiteralFunction(leftValue), OperatorConstants.EQUALS_OPERATOR,
                        new LiteralFunction(o), getLine(), getColumn()));
            }
        }
        if (predicates.isEmpty()) {
            throw new IllegalStateException("IN expression contains no elements!");
        } else if (predicates.size() == 1) {
            predicates.add(FalsePredicate.getInstance());
        }
        return AnyPredicate.getInstance(predicates).evaluate(target);
    } else if (getOperator() instanceof Operator.NotInOperator) {
        Collection predicates = new ArrayList();
        for (Iterator iter = getIterator(rightValue); iter.hasNext();) {
            Object o = iter.next();
            if (o instanceof Function) {
                predicates.add(getPredicate(new LiteralFunction(leftValue), OperatorConstants.EQUALS_OPERATOR,
                        (Function) o, getLine(), getColumn()));
            } else {
                predicates.add(getPredicate(new LiteralFunction(leftValue), OperatorConstants.EQUALS_OPERATOR,
                        new LiteralFunction(o), getLine(), getColumn()));
            }
        }
        if (predicates.isEmpty()) {
            throw new IllegalStateException("NOT IN expression contains no elements!");
        } else if (predicates.size() == 1) {
            predicates.add(FalsePredicate.getInstance());
        }
        return !AnyPredicate.getInstance(predicates).evaluate(target);
    } else if (getOperator() instanceof Operator.BetweenOperator) {
        Object[] array = getArray(rightValue);
        Predicate predicate1 = getPredicate(new LiteralFunction(leftValue),
                OperatorConstants.MORE_THAN_OR_EQUAL_OPERATOR, (Function) array[0], getLine(), getColumn());
        Predicate predicate2 = getPredicate(new LiteralFunction(leftValue),
                OperatorConstants.LESS_THAN_OR_EQUAL_OPERATOR, (Function) array[1], getLine(), getColumn());
        return AndPredicate.getInstance(predicate1, predicate2).evaluate(target);
    } else if (getOperator() instanceof Operator.NotBetweenOperator) {
        Object[] array = getArray(rightValue);
        Predicate predicate1 = getPredicate(new LiteralFunction(leftValue),
                OperatorConstants.MORE_THAN_OR_EQUAL_OPERATOR, (Function) array[0], getLine(), getColumn());
        Predicate predicate2 = getPredicate(new LiteralFunction(leftValue),
                OperatorConstants.LESS_THAN_OR_EQUAL_OPERATOR, (Function) array[1], getLine(), getColumn());
        return !AndPredicate.getInstance(predicate1, predicate2).evaluate(target);
    } else if (getOperator() instanceof Operator.HasLengthOperator) {
        return StringUtils.hasLength(leftValue != null ? leftValue.toString() : null);
    } else if (getOperator() instanceof Operator.HasNoLengthOperator) {
        return !StringUtils.hasLength(leftValue != null ? leftValue.toString() : null);
    } else if (getOperator() instanceof Operator.HasTextOperator) {
        return StringUtils.hasText(leftValue != null ? leftValue.toString() : null);
    } else if (getOperator() instanceof Operator.HasNoTextOperator) {
        return !StringUtils.hasText(leftValue != null ? leftValue.toString() : null);
    } else if (getOperator() instanceof Operator.IsBlankOperator) {
        return isBlank(leftValue != null ? leftValue.toString() : null);
    } else if (getOperator() instanceof Operator.IsNotBlankOperator) {
        return !isBlank(leftValue != null ? leftValue.toString() : null);
    } else if (getOperator() instanceof Operator.IsWordOperator) {
        return isWord(leftValue != null ? leftValue.toString() : null);
    } else if (getOperator() instanceof Operator.IsNotWordOperator) {
        return !isWord(leftValue != null ? leftValue.toString() : null);
    } else if (getOperator() instanceof Operator.IsLowerCaseOperator) {
        return isLowerCase(leftValue != null ? leftValue.toString() : null);
    } else if (getOperator() instanceof Operator.IsNotLowerCaseOperator) {
        return !isLowerCase(leftValue != null ? leftValue.toString() : null);
    } else if (getOperator() instanceof Operator.IsUpperCaseOperator) {
        return isUpperCase(leftValue != null ? leftValue.toString() : null);
    } else if (getOperator() instanceof Operator.IsNotUpperCaseOperator) {
        return !isUpperCase(leftValue != null ? leftValue.toString() : null);
    }

    throw new IllegalStateException(
            "Operator class [" + getOperator().getClass().getName() + "] not supported!");
}

From source file:org.springmodules.validation.valang.predicates.InTestPredicate.java

/**
 * <p>The evaluate method takes the result of both functions and tests with the operator.
 *
 * @param   target      The target bean.
 * @return  boolean      Whether or not the test passed.
 *///from  w  w w . j  ava 2  s.c  o m
public boolean evaluate(Object target) {
    // constructor checks that left function is never null
    Object leftValue = getLeftFunction().getResult(target);
    Object rightValue = (getRightFunction() != null ? getRightFunction().getResult(target) : null);

    if (leftValue instanceof Number) {
        leftValue = new BigDecimal(leftValue.toString());
    }
    if (rightValue instanceof Number) {
        rightValue = new BigDecimal(rightValue.toString());
    }

    // FIX ME: make all collections and lists more efficient (also handle arrays)
    if (rightValue instanceof Set) {
        Set<?> lComparisonValues = (Set<?>) rightValue;

        return lComparisonValues.contains(leftValue);
    } else {
        Collection<Predicate> predicates = new ArrayList<Predicate>();

        for (Iterator iter = getIterator(rightValue); iter.hasNext();) {
            Object o = iter.next();
            if (o instanceof Function) {
                predicates.add(new EqualsTestPredicate(new LiteralFunction(leftValue), Operator.EQUAL,
                        (Function) o, getLine(), getColumn()));
            } else {
                predicates.add(new EqualsTestPredicate(new LiteralFunction(leftValue), Operator.EQUAL,
                        new LiteralFunction(o), getLine(), getColumn()));
            }
        }

        if (predicates.isEmpty()) {
            throw new IllegalStateException("IN expression contains no elements!");
        } else if (predicates.size() == 1) {
            predicates.add(FalsePredicate.getInstance());
        }

        return AnyPredicate.getInstance(predicates).evaluate(target);
    }
}