This is an automated email from the ASF dual-hosted git repository. orpiske pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push: new d5feb9644e9 (chores) camel-core: reduce method size d5feb9644e9 is described below commit d5feb9644e9a4bf65c707db2da7d0a9e45ec25e8 Author: Otavio Rodolfo Piske <angusyo...@gmail.com> AuthorDate: Tue Oct 24 19:02:25 2023 +0200 (chores) camel-core: reduce method size This should help inlining and provide better profiling information --- .../language/simple/ast/BinaryExpression.java | 24 ++- .../org/apache/camel/support/ObjectHelper.java | 182 +++++++++++++-------- .../java/org/apache/camel/util/ObjectHelper.java | 38 +++-- 3 files changed, 154 insertions(+), 90 deletions(-) diff --git a/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/ast/BinaryExpression.java b/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/ast/BinaryExpression.java index e1229058b5f..76931d3b3c8 100644 --- a/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/ast/BinaryExpression.java +++ b/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/ast/BinaryExpression.java @@ -138,21 +138,17 @@ public class BinaryExpression extends BaseSimpleNode { return new Expression() { @Override public <T> T evaluate(Exchange exchange, Class<T> type) { - Predicate predicate; + String name = rightExp.evaluate(exchange, String.class); if (name == null || "null".equals(name)) { - throw new SimpleIllegalSyntaxException( - expression, right.getToken().getIndex(), - operator + " operator cannot accept null. A class type must be provided."); + throwMissingClass(); } Class<?> rightType = camelContext.getClassResolver().resolveClass(name); if (rightType == null) { - throw new SimpleIllegalSyntaxException( - expression, right.getToken().getIndex(), - operator + " operator cannot find class with name: " + name); + throwClassNotFound(name); } - predicate = PredicateBuilder.isInstanceOf(leftExp, rightType); + Predicate predicate = PredicateBuilder.isInstanceOf(leftExp, rightType); if (operator == BinaryOperatorType.NOT_IS) { predicate = PredicateBuilder.not(predicate); } @@ -161,6 +157,18 @@ public class BinaryExpression extends BaseSimpleNode { return camelContext.getTypeConverter().convertTo(type, answer); } + private void throwClassNotFound(String name) { + throw new SimpleIllegalSyntaxException( + expression, right.getToken().getIndex(), + operator + " operator cannot find class with name: " + name); + } + + private void throwMissingClass() { + throw new SimpleIllegalSyntaxException( + expression, right.getToken().getIndex(), + operator + " operator cannot accept null. A class type must be provided."); + } + @Override public String toString() { return left + " " + token.getText() + " " + right; diff --git a/core/camel-support/src/main/java/org/apache/camel/support/ObjectHelper.java b/core/camel-support/src/main/java/org/apache/camel/support/ObjectHelper.java index 4651c1e015c..fbbd3512957 100644 --- a/core/camel-support/src/main/java/org/apache/camel/support/ObjectHelper.java +++ b/core/camel-support/src/main/java/org/apache/camel/support/ObjectHelper.java @@ -78,80 +78,57 @@ public final class ObjectHelper { */ public static boolean typeCoerceEquals(TypeConverter converter, Object leftValue, Object rightValue, boolean ignoreCase) { // sanity check - if (leftValue == null && rightValue == null) { - // they are equal - return true; - } else if (leftValue == null || rightValue == null) { - // only one of them is null so they are not equal - return false; + if (leftValue == null || rightValue == null) { + return evalNulls(leftValue, rightValue); } // optimize for common combinations of comparing numbers - if (leftValue instanceof String && rightValue instanceof String) { - String leftNum = (String) leftValue; - String rightNum = (String) rightValue; - if (isNumber(leftNum) && isNumber(rightNum)) { - // favour to use numeric comparison - Long num1 = Long.parseLong(leftNum); - Long num2 = Long.parseLong(rightNum); - return num1.compareTo(num2) == 0; - } - if (ignoreCase) { - return leftNum.compareToIgnoreCase(rightNum) == 0; - } else { - return leftNum.compareTo(rightNum) == 0; - } + if (leftValue instanceof String) { + return typeCoerceString(converter, leftValue, rightValue, ignoreCase); + } else if (rightValue instanceof String && + (leftValue instanceof Integer || leftValue instanceof Long) && isNumber((String) rightValue)) { + return typeCoerceIntLong(leftValue, (String) rightValue); } else if (leftValue instanceof Integer && rightValue instanceof Integer) { - Integer leftNum = (Integer) leftValue; - Integer rightNum = (Integer) rightValue; - return leftNum.compareTo(rightNum) == 0; + return integerPairComparison(leftValue, rightValue); } else if (leftValue instanceof Long && rightValue instanceof Long) { - Long leftNum = (Long) leftValue; - Long rightNum = (Long) rightValue; - return leftNum.compareTo(rightNum) == 0; + return longPairComparison(leftValue, rightValue); } else if (leftValue instanceof Double && rightValue instanceof Double) { - Double leftNum = (Double) leftValue; - Double rightNum = (Double) rightValue; - return leftNum.compareTo(rightNum) == 0; - } else if ((rightValue instanceof Integer || rightValue instanceof Long) && - leftValue instanceof String && isNumber((String) leftValue)) { - if (rightValue instanceof Integer) { - Integer leftNum = Integer.valueOf((String) leftValue); - Integer rightNum = (Integer) rightValue; - return leftNum.compareTo(rightNum) == 0; - } else { - Long leftNum = Long.valueOf((String) leftValue); - Long rightNum = (Long) rightValue; - return leftNum.compareTo(rightNum) == 0; - } - } else if (rightValue instanceof String && - (leftValue instanceof Integer || leftValue instanceof Long) && isNumber((String) rightValue)) { - if (leftValue instanceof Integer) { - Integer leftNum = (Integer) leftValue; - Integer rightNum = Integer.valueOf((String) rightValue); - return leftNum.compareTo(rightNum) == 0; - } else { - Long leftNum = (Long) leftValue; - Long rightNum = Long.valueOf((String) rightValue); - return leftNum.compareTo(rightNum) == 0; - } - } else if (rightValue instanceof Double && leftValue instanceof String && isFloatingNumber((String) leftValue)) { - Double leftNum = Double.valueOf((String) leftValue); - Double rightNum = (Double) rightValue; - return leftNum.compareTo(rightNum) == 0; - } else if (rightValue instanceof Boolean && leftValue instanceof String) { - Boolean leftBool = Boolean.valueOf((String) leftValue); - Boolean rightBool = (Boolean) rightValue; - return leftBool.compareTo(rightBool) == 0; + return doublePairComparison(leftValue, rightValue); } else if (rightValue instanceof String && leftValue instanceof Boolean) { - Boolean leftBool = (Boolean) leftValue; - Boolean rightBool = Boolean.valueOf((String) rightValue); - return leftBool.compareTo(rightBool) == 0; + return booleanStringComparison((Boolean) leftValue, (String) rightValue); } // try without type coerce - boolean answer = org.apache.camel.util.ObjectHelper.equal(leftValue, rightValue, ignoreCase); - if (answer) { + return tryConverters(converter, leftValue, rightValue, ignoreCase); + } + + private static boolean typeCoerceString(TypeConverter converter, Object leftValue, Object rightValue, boolean ignoreCase) { + if (rightValue instanceof String) { + return typeCoerceStringPair((String) leftValue, (String) rightValue, ignoreCase); + } else if ((rightValue instanceof Integer || rightValue instanceof Long) && + isNumber((String) leftValue)) { + return typeCoerceILString((String) leftValue, rightValue); + } else if (rightValue instanceof Double && isFloatingNumber((String) leftValue)) { + return stringDoubleComparison((String) leftValue, (Double) rightValue); + } else if (rightValue instanceof Boolean) { + return stringBooleanComparison(leftValue, rightValue); + } + + return tryConverters(converter, leftValue, rightValue, ignoreCase); + } + + private static boolean evalNulls(Object leftValue, Object rightValue) { + // they are equal + if (leftValue == rightValue) { + return true; + } + // only one of them is null so they are not equal + return false; + } + + private static boolean tryConverters(TypeConverter converter, Object leftValue, Object rightValue, boolean ignoreCase) { + final boolean isEqual = org.apache.camel.util.ObjectHelper.equal(leftValue, rightValue, ignoreCase); + if (isEqual) { return true; } @@ -162,15 +139,84 @@ public final class ObjectHelper { // convert left to right Object value = converter.tryConvertTo(rightValue.getClass(), leftValue); - answer = org.apache.camel.util.ObjectHelper.equal(value, rightValue, ignoreCase); - if (answer) { + final boolean isEqualLeftToRight = org.apache.camel.util.ObjectHelper.equal(value, rightValue, ignoreCase); + if (isEqualLeftToRight) { return true; } // convert right to left value = converter.tryConvertTo(leftValue.getClass(), rightValue); - answer = org.apache.camel.util.ObjectHelper.equal(leftValue, value, ignoreCase); - return answer; + return org.apache.camel.util.ObjectHelper.equal(leftValue, value, ignoreCase); + } + + private static boolean booleanStringComparison(Boolean leftBool, String rightValue) { + Boolean rightBool = Boolean.valueOf(rightValue); + return leftBool.compareTo(rightBool) == 0; + } + + private static boolean doublePairComparison(Object leftValue, Object rightValue) { + return doublePairComparison((Double) leftValue, (Double) rightValue); + } + + private static boolean doublePairComparison(Double leftValue, Double rightValue) { + return leftValue.compareTo(rightValue) == 0; + } + + private static boolean longPairComparison(Object leftValue, Object rightValue) { + return longPairComparison((Long) leftValue, (Long) rightValue); + } + + private static boolean longPairComparison(Long leftValue, Long rightValue) { + return leftValue.compareTo(rightValue) == 0; + } + + private static boolean integerPairComparison(Object leftValue, Object rightValue) { + return integerPairComparison((Integer) leftValue, (Integer) rightValue); + } + + private static boolean integerPairComparison(Integer leftValue, Integer rightValue) { + return leftValue.compareTo(rightValue) == 0; + } + + private static boolean stringBooleanComparison(Object leftValue, Object rightValue) { + return stringBooleanComparison((String) leftValue, (Boolean) rightValue); + } + + private static boolean stringBooleanComparison(String leftValue, Boolean rightValue) { + Boolean leftBool = Boolean.valueOf(leftValue); + return leftBool.compareTo(rightValue) == 0; + } + + private static boolean stringDoubleComparison(String leftValue, Double rightValue) { + return doublePairComparison(Double.valueOf(leftValue), rightValue); + } + + private static boolean typeCoerceIntLong(Object leftValue, String rightValue) { + if (leftValue instanceof Integer) { + return integerPairComparison((Integer) leftValue, Integer.valueOf(rightValue)); + } else { + return longPairComparison((Long) leftValue, Long.valueOf(rightValue)); + } + } + + private static boolean typeCoerceILString(String leftValue, Object rightValue) { + if (rightValue instanceof Integer) { + return integerPairComparison(Integer.valueOf(leftValue), (Integer) rightValue); + } else { + return longPairComparison(Long.valueOf(leftValue), (Long) rightValue); + } + } + + private static boolean typeCoerceStringPair(String leftNum, String rightNum, boolean ignoreCase) { + if (isNumber(leftNum) && isNumber(rightNum)) { + // favour to use numeric comparison + return longPairComparison(Long.parseLong(leftNum), Long.parseLong(rightNum)); + } + if (ignoreCase) { + return leftNum.compareToIgnoreCase(rightNum) == 0; + } else { + return leftNum.compareTo(rightNum) == 0; + } } /** diff --git a/core/camel-util/src/main/java/org/apache/camel/util/ObjectHelper.java b/core/camel-util/src/main/java/org/apache/camel/util/ObjectHelper.java index 470fef456ab..705b7f98cd0 100644 --- a/core/camel-util/src/main/java/org/apache/camel/util/ObjectHelper.java +++ b/core/camel-util/src/main/java/org/apache/camel/util/ObjectHelper.java @@ -1149,21 +1149,9 @@ public final class ObjectHelper { if (value instanceof Boolean) { return (Boolean) value; } else if (value instanceof String) { - String str = ((String) value).trim(); - if (str.isEmpty()) { - return false; - } else if ("true".equalsIgnoreCase(str)) { - return true; - } else if ("false".equalsIgnoreCase(str)) { - return false; - } + return evaluateString((String) value); } else if (value instanceof NodeList) { - // is it an empty dom with empty attributes - if (value instanceof Node && ((Node) value).hasAttributes()) { - return true; - } - NodeList list = (NodeList) value; - return list.getLength() > 0; + return evaluateNodeList(value); } else if (value instanceof Collection) { // is it an empty collection return !((Collection<?>) value).isEmpty(); @@ -1171,6 +1159,28 @@ public final class ObjectHelper { return value != null; } + private static boolean evaluateString(String value) { + final String str = value.trim(); + if (str.isEmpty()) { + return false; + } else if ("true".equalsIgnoreCase(str)) { + return true; + } else if ("false".equalsIgnoreCase(str)) { + return false; + } + + return true; + } + + private static boolean evaluateNodeList(Object value) { + // is it an empty dom with empty attributes + if (value instanceof Node && ((Node) value).hasAttributes()) { + return true; + } + NodeList list = (NodeList) value; + return list.getLength() > 0; + } + /** * Creates an Iterable to walk the exception from the bottom up (the last caused by going upwards to the root * exception).