/** Performs the {@code /} operator on the two values. */ public static double dividedBy(SoyValue operand0, SoyValue operand1) { // Note: Soy always performs floating-point division, even on two integers (like JavaScript). // Note that this *will* lose precision for longs. return operand0.numberValue() / operand1.numberValue(); }
/** Performs the {@code /} operator on the two values. */ public static double dividedBy(SoyValue operand0, SoyValue operand1) { // Note: Soy always performs floating-point division, even on two integers (like JavaScript). // Note that this *will* lose precision for longs. return operand0.numberValue() / operand1.numberValue(); }
/** Determines if the operand's string form can be equality-compared with a string. */ public static boolean compareString(String string, SoyValue other) { // This follows similarly to the Javascript specification, to ensure similar operation // over Javascript and Java: http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3 if (other instanceof StringData || other instanceof SanitizedContent) { return string.equals(other.toString()); } if (other instanceof NumberData) { try { // Parse the string as a number. return Double.parseDouble(string) == other.numberValue(); } catch (NumberFormatException nfe) { // Didn't parse as a number. return false; } } return false; }
/** Rounds the given value to the closest integer. */ public static long round(SoyValue value) { if (value instanceof IntegerData) { return value.longValue(); } else { return Math.round(value.numberValue()); } }
/** Rounds the given value to the closest integer. */ public static long round(SoyValue value) { if (value instanceof IntegerData) { return value.longValue(); } else { return Math.round(value.numberValue()); } }
/** Returns the numeric maximum of the two arguments. */ public static NumberData max(SoyValue arg0, SoyValue arg1) { if (arg0 instanceof IntegerData && arg1 instanceof IntegerData) { return IntegerData.forValue(Math.max(arg0.longValue(), arg1.longValue())); } else { return FloatData.forValue(Math.max(arg0.numberValue(), arg1.numberValue())); } } /** Returns the numeric minimum of the two arguments. */
/** Returns the numeric minimum of the two arguments. */ public static NumberData min(SoyValue arg0, SoyValue arg1) { if (arg0 instanceof IntegerData && arg1 instanceof IntegerData) { return IntegerData.forValue(Math.min(arg0.longValue(), arg1.longValue())); } else { return FloatData.forValue(Math.min(arg0.numberValue(), arg1.numberValue())); } }
/** Returns the numeric maximum of the two arguments. */ public static NumberData max(SoyValue arg0, SoyValue arg1) { if (arg0 instanceof IntegerData && arg1 instanceof IntegerData) { return IntegerData.forValue(Math.max(arg0.longValue(), arg1.longValue())); } else { return FloatData.forValue(Math.max(arg0.numberValue(), arg1.numberValue())); } } /** Returns the numeric minimum of the two arguments. */
/** Returns the numeric minimum of the two arguments. */ public static NumberData min(SoyValue arg0, SoyValue arg1) { if (arg0 instanceof IntegerData && arg1 instanceof IntegerData) { return IntegerData.forValue(Math.min(arg0.longValue(), arg1.longValue())); } else { return FloatData.forValue(Math.min(arg0.numberValue(), arg1.numberValue())); } }
/** Performs the {@code <} operator on the two values. */ public static boolean lessThan(SoyValue left, SoyValue right) { if (left instanceof SoyString && right instanceof SoyString) { return left.stringValue().compareTo(right.stringValue()) < 0; } else if (left instanceof IntegerData && right instanceof IntegerData) { return left.longValue() < right.longValue(); } else { return left.numberValue() < right.numberValue(); } }
/** Performs the {@code <=} operator on the two values. */ public static boolean lessThanOrEqual(SoyValue left, SoyValue right) { if (left instanceof SoyString && right instanceof SoyString) { return left.stringValue().compareTo(right.stringValue()) <= 0; } else if (left instanceof IntegerData && right instanceof IntegerData) { return left.longValue() <= right.longValue(); } else { return left.numberValue() <= right.numberValue(); } }
/** Performs the {@code <=} operator on the two values. */ public static boolean lessThanOrEqual(SoyValue left, SoyValue right) { if (left instanceof SoyString && right instanceof SoyString) { return left.stringValue().compareTo(right.stringValue()) <= 0; } else if (left instanceof IntegerData && right instanceof IntegerData) { return left.longValue() <= right.longValue(); } else { return left.numberValue() <= right.numberValue(); } }
/** Performs the {@code <} operator on the two values. */ public static boolean lessThan(SoyValue left, SoyValue right) { if (left instanceof SoyString && right instanceof SoyString) { return left.stringValue().compareTo(right.stringValue()) < 0; } else if (left instanceof IntegerData && right instanceof IntegerData) { return left.longValue() < right.longValue(); } else { return left.numberValue() < right.numberValue(); } }
/** Performs the {@code -} operator on the two values. */ public static SoyValue minus(SoyValue operand0, SoyValue operand1) { if (operand0 instanceof IntegerData && operand1 instanceof IntegerData) { return IntegerData.forValue(operand0.longValue() - operand1.longValue()); } else { return FloatData.forValue(operand0.numberValue() - operand1.numberValue()); } }
/** Performs the {@code *} operator on the two values. */ public static NumberData times(SoyValue operand0, SoyValue operand1) { if (operand0 instanceof IntegerData && operand1 instanceof IntegerData) { return IntegerData.forValue(operand0.longValue() * operand1.longValue()); } else { return FloatData.forValue(operand0.numberValue() * operand1.numberValue()); } }
/** Performs the {@code *} operator on the two values. */ public static NumberData times(SoyValue operand0, SoyValue operand1) { if (operand0 instanceof IntegerData && operand1 instanceof IntegerData) { return IntegerData.forValue(operand0.longValue() * operand1.longValue()); } else { return FloatData.forValue(operand0.numberValue() * operand1.numberValue()); } }
/** * Rounds the given value to the closest decimal point left (negative numbers) or right (positive * numbers) of the decimal point */ public static NumberData round(SoyValue value, int numDigitsAfterPoint) { // NOTE: for more accurate rounding, this should really be using BigDecimal which can do correct // decimal arithmetic. However, for compatibility with js, that probably isn't an option. if (numDigitsAfterPoint == 0) { return IntegerData.forValue(round(value)); } else if (numDigitsAfterPoint > 0) { double valueDouble = value.numberValue(); double shift = Math.pow(10, numDigitsAfterPoint); return FloatData.forValue(Math.round(valueDouble * shift) / shift); } else { double valueDouble = value.numberValue(); double shift = Math.pow(10, -numDigitsAfterPoint); return IntegerData.forValue((int) (Math.round(valueDouble / shift) * shift)); } }
/** Performs the {@code -} operator on the two values. */ public static SoyValue minus(SoyValue operand0, SoyValue operand1) { if (operand0 instanceof IntegerData && operand1 instanceof IntegerData) { return IntegerData.forValue(operand0.longValue() - operand1.longValue()); } else { return FloatData.forValue(operand0.numberValue() - operand1.numberValue()); } }
/** Performs the {@code +} operator on the two values. */ public static SoyValue plus(SoyValue operand0, SoyValue operand1) { if (operand0 instanceof IntegerData && operand1 instanceof IntegerData) { return IntegerData.forValue(operand0.longValue() + operand1.longValue()); } else if (operand0 instanceof NumberData && operand1 instanceof NumberData) { return FloatData.forValue(operand0.numberValue() + operand1.numberValue()); } else { // String concatenation is the fallback for other types (like in JS). Use the implemented // coerceToString() for the type. return StringData.forValue(operand0.coerceToString() + operand1.coerceToString()); } }
/** Performs the {@code +} operator on the two values. */ public static SoyValue plus(SoyValue operand0, SoyValue operand1) { if (operand0 instanceof IntegerData && operand1 instanceof IntegerData) { return IntegerData.forValue(operand0.longValue() + operand1.longValue()); } else if (operand0 instanceof NumberData && operand1 instanceof NumberData) { return FloatData.forValue(operand0.numberValue() + operand1.numberValue()); } else { // String concatenation is the fallback for other types (like in JS). Use the implemented // coerceToString() for the type. return StringData.forValue(operand0.coerceToString() + operand1.coerceToString()); } }