Search in sources :

Example 1 with CompareNotSupportedException

use of com.googlecode.aviator.exception.CompareNotSupportedException in project aviatorscript by killme2008.

the class AviatorString method innerCompare.

@Override
public int innerCompare(final AviatorObject other, final Map<String, Object> env) {
    final String left = getLexeme(env);
    if (other.getAviatorType() == AviatorType.String) {
        final AviatorString otherString = (AviatorString) other;
        final String right = otherString.getLexeme(env);
        if (left != null && right != null) {
            return left.compareTo(right);
        } else if (left == null && right != null) {
            return -1;
        } else if (left != null && right == null) {
            return 1;
        } else {
            return 0;
        }
    }
    switch(other.getAviatorType()) {
        case JavaType:
            final AviatorJavaType javaType = (AviatorJavaType) other;
            final Object otherJavaValue = javaType.getValue(env);
            if (left == null && otherJavaValue == null) {
                return 0;
            } else if (left != null && otherJavaValue == null) {
                return 1;
            }
            if (TypeUtils.isString(otherJavaValue)) {
                if (left == null) {
                    return -1;
                } else {
                    return left.compareTo(String.valueOf(otherJavaValue));
                }
            } else if (otherJavaValue instanceof Date) {
                return tryCompareDate(env, (Date) otherJavaValue);
            } else {
                throw new CompareNotSupportedException("Could not compare " + desc(env) + " with " + other.desc(env));
            }
        case Nil:
            if (left == null) {
                return 0;
            } else {
                return 1;
            }
        default:
            throw new CompareNotSupportedException("Could not compare " + desc(env) + " with " + other.desc(env));
    }
}
Also used : CompareNotSupportedException(com.googlecode.aviator.exception.CompareNotSupportedException) Date(java.util.Date)

Example 2 with CompareNotSupportedException

use of com.googlecode.aviator.exception.CompareNotSupportedException in project aviatorscript by killme2008.

the class AviatorJavaType method innerCompare.

@Override
@SuppressWarnings("unchecked")
public int innerCompare(final AviatorObject other, final Map<String, Object> env) {
    if (this == other) {
        return 0;
    }
    switch(other.getAviatorType()) {
        case BigInt:
        case Decimal:
        case Long:
        case Double:
            AviatorNumber aviatorNumber = (AviatorNumber) other;
            return -aviatorNumber.innerCompare(this, env);
        case String:
            AviatorString aviatorString = (AviatorString) other;
            return -aviatorString.innerCompare(this, env);
        case Boolean:
            AviatorBoolean aviatorBoolean = (AviatorBoolean) other;
            return -aviatorBoolean.innerCompare(this, env);
        case JavaType:
            AviatorJavaType otherJavaType = (AviatorJavaType) other;
            final Object thisValue = getValue(env);
            final Object otherValue = otherJavaType.getValue(env);
            if (thisValue == null) {
                return AviatorNil.NIL.innerCompare(other, env);
            }
            if (thisValue.equals(otherValue)) {
                return 0;
            } else {
                if (thisValue instanceof Number) {
                    AviatorNumber thisAviatorNumber = AviatorNumber.valueOf(thisValue);
                    return thisAviatorNumber.innerCompare(other, env);
                } else if (TypeUtils.isString(thisValue)) {
                    AviatorString thisAviatorString = new AviatorString(String.valueOf(thisValue));
                    return thisAviatorString.innerCompare(other, env);
                } else if (thisValue instanceof Boolean) {
                    AviatorBoolean thisAviatorBoolean = AviatorBoolean.valueOf((Boolean) thisValue);
                    return thisAviatorBoolean.innerCompare(other, env);
                } else if (thisValue instanceof Date && otherValue instanceof String) {
                    // This is date,other is string
                    return tryCompareDate(thisValue, otherValue);
                } else if (otherValue == null) {
                    return 1;
                } else {
                    try {
                        return ((Comparable<Object>) thisValue).compareTo(otherValue);
                    } catch (ClassCastException e) {
                        throw new CompareNotSupportedException("Compare " + desc(env) + " with " + other.desc(env) + " error", e);
                    }
                }
            }
        case Nil:
            // Any object is greater than nil except nil
            if (getValue(env) == null) {
                return 0;
            } else {
                return 1;
            }
        default:
            throw new CompareNotSupportedException("Unknow aviator type");
    }
}
Also used : CompareNotSupportedException(com.googlecode.aviator.exception.CompareNotSupportedException) Date(java.util.Date)

Aggregations

CompareNotSupportedException (com.googlecode.aviator.exception.CompareNotSupportedException)2 Date (java.util.Date)2