Java Code Examples for org.springframework.expression.spel.SpelMessage#NOT_COMPARABLE

The following examples show how to use org.springframework.expression.spel.SpelMessage#NOT_COMPARABLE . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StandardTypeComparator.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public int compare(@Nullable Object left, @Nullable Object right) throws SpelEvaluationException {
	// If one is null, check if the other is
	if (left == null) {
		return (right == null ? 0 : -1);
	}
	else if (right == null) {
		return 1;  // left cannot be null at this point
	}

	// Basic number comparisons
	if (left instanceof Number && right instanceof Number) {
		Number leftNumber = (Number) left;
		Number rightNumber = (Number) right;

		if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) {
			BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class);
			BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class);
			return leftBigDecimal.compareTo(rightBigDecimal);
		}
		else if (leftNumber instanceof Double || rightNumber instanceof Double) {
			return Double.compare(leftNumber.doubleValue(), rightNumber.doubleValue());
		}
		else if (leftNumber instanceof Float || rightNumber instanceof Float) {
			return Float.compare(leftNumber.floatValue(), rightNumber.floatValue());
		}
		else if (leftNumber instanceof BigInteger || rightNumber instanceof BigInteger) {
			BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClass(leftNumber, BigInteger.class);
			BigInteger rightBigInteger = NumberUtils.convertNumberToTargetClass(rightNumber, BigInteger.class);
			return leftBigInteger.compareTo(rightBigInteger);
		}
		else if (leftNumber instanceof Long || rightNumber instanceof Long) {
			// Don't call Long.compare here - only available on JDK 1.7+
			return compare(leftNumber.longValue(), rightNumber.longValue());
		}
		else if (leftNumber instanceof Integer || rightNumber instanceof Integer) {
			// Don't call Integer.compare here - only available on JDK 1.7+
			return compare(leftNumber.intValue(), rightNumber.intValue());
		}
		else if (leftNumber instanceof Short || rightNumber instanceof Short) {
			// Don't call Short.compare here - only available on JDK 1.7+
			return compare(leftNumber.shortValue(), rightNumber.shortValue());
		}
		else if (leftNumber instanceof Byte || rightNumber instanceof Byte) {
			// Don't call Short.compare here - only available on JDK 1.7+
			return compare(leftNumber.byteValue(), rightNumber.byteValue());
		}
		else {
			// Unknown Number subtypes -> best guess is double multiplication
			return Double.compare(leftNumber.doubleValue(), rightNumber.doubleValue());
		}
	}

	try {
		if (left instanceof Comparable) {
			return ((Comparable<Object>) left).compareTo(right);
		}
	}
	catch (ClassCastException ex) {
		throw new SpelEvaluationException(ex, SpelMessage.NOT_COMPARABLE, left.getClass(), right.getClass());
	}

	throw new SpelEvaluationException(SpelMessage.NOT_COMPARABLE, left.getClass(), right.getClass());
}
 
Example 2
Source File: StandardTypeComparator.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public int compare(@Nullable Object left, @Nullable Object right) throws SpelEvaluationException {
	// If one is null, check if the other is
	if (left == null) {
		return (right == null ? 0 : -1);
	}
	else if (right == null) {
		return 1;  // left cannot be null at this point
	}

	// Basic number comparisons
	if (left instanceof Number && right instanceof Number) {
		Number leftNumber = (Number) left;
		Number rightNumber = (Number) right;

		if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) {
			BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class);
			BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class);
			return leftBigDecimal.compareTo(rightBigDecimal);
		}
		else if (leftNumber instanceof Double || rightNumber instanceof Double) {
			return Double.compare(leftNumber.doubleValue(), rightNumber.doubleValue());
		}
		else if (leftNumber instanceof Float || rightNumber instanceof Float) {
			return Float.compare(leftNumber.floatValue(), rightNumber.floatValue());
		}
		else if (leftNumber instanceof BigInteger || rightNumber instanceof BigInteger) {
			BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClass(leftNumber, BigInteger.class);
			BigInteger rightBigInteger = NumberUtils.convertNumberToTargetClass(rightNumber, BigInteger.class);
			return leftBigInteger.compareTo(rightBigInteger);
		}
		else if (leftNumber instanceof Long || rightNumber instanceof Long) {
			// Don't call Long.compare here - only available on JDK 1.7+
			return compare(leftNumber.longValue(), rightNumber.longValue());
		}
		else if (leftNumber instanceof Integer || rightNumber instanceof Integer) {
			// Don't call Integer.compare here - only available on JDK 1.7+
			return compare(leftNumber.intValue(), rightNumber.intValue());
		}
		else if (leftNumber instanceof Short || rightNumber instanceof Short) {
			// Don't call Short.compare here - only available on JDK 1.7+
			return compare(leftNumber.shortValue(), rightNumber.shortValue());
		}
		else if (leftNumber instanceof Byte || rightNumber instanceof Byte) {
			// Don't call Short.compare here - only available on JDK 1.7+
			return compare(leftNumber.byteValue(), rightNumber.byteValue());
		}
		else {
			// Unknown Number subtypes -> best guess is double multiplication
			return Double.compare(leftNumber.doubleValue(), rightNumber.doubleValue());
		}
	}

	try {
		if (left instanceof Comparable) {
			return ((Comparable<Object>) left).compareTo(right);
		}
	}
	catch (ClassCastException ex) {
		throw new SpelEvaluationException(ex, SpelMessage.NOT_COMPARABLE, left.getClass(), right.getClass());
	}

	throw new SpelEvaluationException(SpelMessage.NOT_COMPARABLE, left.getClass(), right.getClass());
}
 
Example 3
Source File: StandardTypeComparator.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public int compare(Object left, Object right) throws SpelEvaluationException {
	// If one is null, check if the other is
	if (left == null) {
		return (right == null ? 0 : -1);
	}
	else if (right == null) {
		return 1;  // left cannot be null at this point
	}

	// Basic number comparisons
	if (left instanceof Number && right instanceof Number) {
		Number leftNumber = (Number) left;
		Number rightNumber = (Number) right;

		if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) {
			BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class);
			BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class);
			return leftBigDecimal.compareTo(rightBigDecimal);
		}
		else if (leftNumber instanceof Double || rightNumber instanceof Double) {
			return Double.compare(leftNumber.doubleValue(), rightNumber.doubleValue());
		}
		else if (leftNumber instanceof Float || rightNumber instanceof Float) {
			return Float.compare(leftNumber.floatValue(), rightNumber.floatValue());
		}
		else if (leftNumber instanceof BigInteger || rightNumber instanceof BigInteger) {
			BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClass(leftNumber, BigInteger.class);
			BigInteger rightBigInteger = NumberUtils.convertNumberToTargetClass(rightNumber, BigInteger.class);
			return leftBigInteger.compareTo(rightBigInteger);
		}
		else if (leftNumber instanceof Long || rightNumber instanceof Long) {
			// Don't call Long.compare here - only available on JDK 1.7+
			return compare(leftNumber.longValue(), rightNumber.longValue());
		}
		else if (leftNumber instanceof Integer || rightNumber instanceof Integer) {
			// Don't call Integer.compare here - only available on JDK 1.7+
			return compare(leftNumber.intValue(), rightNumber.intValue());
		}
		else if (leftNumber instanceof Short || rightNumber instanceof Short) {
			// Don't call Short.compare here - only available on JDK 1.7+
			return compare(leftNumber.shortValue(), rightNumber.shortValue());
		}
		else if (leftNumber instanceof Byte || rightNumber instanceof Byte) {
			// Don't call Short.compare here - only available on JDK 1.7+
			return compare(leftNumber.byteValue(), rightNumber.byteValue());
		}
		else {
			// Unknown Number subtypes -> best guess is double multiplication
			return Double.compare(leftNumber.doubleValue(), rightNumber.doubleValue());
		}
	}

	try {
		if (left instanceof Comparable) {
			return ((Comparable<Object>) left).compareTo(right);
		}
	}
	catch (ClassCastException ex) {
		throw new SpelEvaluationException(ex, SpelMessage.NOT_COMPARABLE, left.getClass(), right.getClass());
	}

	throw new SpelEvaluationException(SpelMessage.NOT_COMPARABLE, left.getClass(), right.getClass());
}
 
Example 4
Source File: StandardTypeComparator.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public int compare(Object left, Object right) throws SpelEvaluationException {
	// If one is null, check if the other is
	if (left == null) {
		return (right == null ? 0 : -1);
	}
	else if (right == null) {
		return 1;  // left cannot be null at this point
	}

	// Basic number comparisons
	if (left instanceof Number && right instanceof Number) {
		Number leftNumber = (Number) left;
		Number rightNumber = (Number) right;

		if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) {
			BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class);
			BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class);
			return leftBigDecimal.compareTo(rightBigDecimal);
		}
		else if (leftNumber instanceof Double || rightNumber instanceof Double) {
			return Double.compare(leftNumber.doubleValue(), rightNumber.doubleValue());
		}
		else if (leftNumber instanceof Float || rightNumber instanceof Float) {
			return Float.compare(leftNumber.floatValue(), rightNumber.floatValue());
		}
		else if (leftNumber instanceof BigInteger || rightNumber instanceof BigInteger) {
			BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClass(leftNumber, BigInteger.class);
			BigInteger rightBigInteger = NumberUtils.convertNumberToTargetClass(rightNumber, BigInteger.class);
			return leftBigInteger.compareTo(rightBigInteger);
		}
		else if (leftNumber instanceof Long || rightNumber instanceof Long) {
			// Don't call Long.compare here - only available on JDK 1.7+
			return compare(leftNumber.longValue(), rightNumber.longValue());
		}
		else if (leftNumber instanceof Integer || rightNumber instanceof Integer) {
			// Don't call Integer.compare here - only available on JDK 1.7+
			return compare(leftNumber.intValue(), rightNumber.intValue());
		}
		else if (leftNumber instanceof Short || rightNumber instanceof Short) {
			// Don't call Short.compare here - only available on JDK 1.7+
			return compare(leftNumber.shortValue(), rightNumber.shortValue());
		}
		else if (leftNumber instanceof Byte || rightNumber instanceof Byte) {
			// Don't call Short.compare here - only available on JDK 1.7+
			return compare(leftNumber.byteValue(), rightNumber.byteValue());
		}
		else {
			// Unknown Number subtypes -> best guess is double multiplication
			return Double.compare(leftNumber.doubleValue(), rightNumber.doubleValue());
		}
	}

	try {
		if (left instanceof Comparable) {
			return ((Comparable<Object>) left).compareTo(right);
		}
	}
	catch (ClassCastException ex) {
		throw new SpelEvaluationException(ex, SpelMessage.NOT_COMPARABLE, left.getClass(), right.getClass());
	}

	throw new SpelEvaluationException(SpelMessage.NOT_COMPARABLE, left.getClass(), right.getClass());
}