/** * Precondition: The specified index contains an integer. Gets the integer at the given index. * * @param index The index. * @return The integer at the given index, or null of the index is undefined. */ public int getInteger(int index) { return get(index).integerValue(); }
/** * Precondition: The specified index contains an integer. Gets the integer at the given index. * * @param index The index. * @return The integer at the given index, or null of the index is undefined. */ public int getInteger(int index) { return get(index).integerValue(); }
/** * Precondition: The specified index contains an integer. * Gets the integer at the given index. * @param index The index. * @return The integer at the given index, or null of the index is undefined. */ public int getInteger(int index) { return get(index).integerValue(); }
@Override public SoyData compute(List<SoyData> args) { SoyData arg = args.get(0); return toSoyData((int) Math.floor(Math.random() * arg.integerValue())); }
/** * Precondition: The specified key string is the path to an integer. Gets the integer at the * specified key string. * * @param keyStr One or more map keys and/or list indices (separated by '.' if multiple parts). * Indicates the path to the location within this data tree. * @return The integer at the specified key string. * @throws IllegalArgumentException If no data is stored at the specified key. */ public int getInteger(String keyStr) { SoyData valueData = get(keyStr); if (valueData == null) { throw new IllegalArgumentException("Missing key: " + keyStr); } return valueData.integerValue(); }
@Override public SoyData compute(List<SoyData> args) { SoyData arg0 = args.get(0); SoyData arg1 = args.get(1); if (arg0 instanceof IntegerData && arg1 instanceof IntegerData) { return toSoyData(Math.max(arg0.integerValue(), arg1.integerValue())); } else { return toSoyData(Math.max(arg0.numberValue(), arg1.numberValue())); } }
@Override public SoyData compute(List<SoyData> args) { SoyData arg0 = args.get(0); SoyData arg1 = args.get(1); if (arg0 instanceof IntegerData && arg1 instanceof IntegerData) { return toSoyData(Math.min(arg0.integerValue(), arg1.integerValue())); } else { return toSoyData(Math.min(arg0.numberValue(), arg1.numberValue())); } }
@Override protected SoyData visitModOpNode(ModOpNode node) { SoyData operand0 = visit(node.getChild(0)); SoyData operand1 = visit(node.getChild(1)); return convertResult(operand0.integerValue() % operand1.integerValue()); }
private SoyData visitIsLastFunction(FunctionNode node) { int localVarIndex, localVarLastIndex; try { DataRefNode dataRef = (DataRefNode) node.getChild(0); String localVarName = dataRef.getFirstKey(); localVarIndex = getLocalVar(localVarName + "__index").integerValue(); localVarLastIndex = getLocalVar(localVarName + "__lastIndex").integerValue(); } catch (Exception e) { throw new RenderException("Failed to evaluate function call " + node.toSourceString() + "."); } return convertResult(localVarIndex == localVarLastIndex); }
public static SoyData $$plus(SoyData operand0, SoyData operand1) { if (operand0 instanceof NumberData && operand1 instanceof NumberData) { if (operand0 instanceof IntegerData && operand1 instanceof IntegerData) { return IntegerData.forValue(operand0.integerValue() + operand1.integerValue()); } else { return FloatData.forValue(operand0.numberValue() + operand1.numberValue()); } } else { // String concatenation. Note we're calling toString() instead of stringValue() in case one // of the operands needs to be coerced to a string. return StringData.forValue(operand0.toString() + operand1.toString()); } }
@Override protected SoyData visitLessThanOrEqualOpNode(LessThanOrEqualOpNode node) { SoyData operand0 = visit(node.getChild(0)); SoyData operand1 = visit(node.getChild(1)); if (operand0 instanceof IntegerData && operand1 instanceof IntegerData) { return convertResult(operand0.integerValue() <= operand1.integerValue()); } else { return convertResult(operand0.numberValue() <= operand1.numberValue()); } }
@Override protected SoyData visitGreaterThanOrEqualOpNode(GreaterThanOrEqualOpNode node) { SoyData operand0 = visit(node.getChild(0)); SoyData operand1 = visit(node.getChild(1)); if (operand0 instanceof IntegerData && operand1 instanceof IntegerData) { return convertResult(operand0.integerValue() >= operand1.integerValue()); } else { return convertResult(operand0.numberValue() >= operand1.numberValue()); } }
@Override protected SoyData visitMinusOpNode(MinusOpNode node) { SoyData operand0 = visit(node.getChild(0)); SoyData operand1 = visit(node.getChild(1)); if (operand0 instanceof IntegerData && operand1 instanceof IntegerData) { return convertResult(operand0.integerValue() - operand1.integerValue()); } else { return convertResult(operand0.numberValue() - operand1.numberValue()); } }
@Override protected SoyData visitGreaterThanOpNode(GreaterThanOpNode node) { SoyData operand0 = visit(node.getChild(0)); SoyData operand1 = visit(node.getChild(1)); if (operand0 instanceof IntegerData && operand1 instanceof IntegerData) { return convertResult(operand0.integerValue() > operand1.integerValue()); } else { return convertResult(operand0.numberValue() > operand1.numberValue()); } }
@Override protected SoyData visitTimesOpNode(TimesOpNode node) { SoyData operand0 = visit(node.getChild(0)); SoyData operand1 = visit(node.getChild(1)); if (operand0 instanceof IntegerData && operand1 instanceof IntegerData) { return convertResult(operand0.integerValue() * operand1.integerValue()); } else { return convertResult(operand0.numberValue() * operand1.numberValue()); } }
@Override protected SoyData visitLessThanOpNode(LessThanOpNode node) { SoyData operand0 = visit(node.getChild(0)); SoyData operand1 = visit(node.getChild(1)); if (operand0 instanceof IntegerData && operand1 instanceof IntegerData) { return convertResult(operand0.integerValue() < operand1.integerValue()); } else { return convertResult(operand0.numberValue() < operand1.numberValue()); } }
@Override protected SoyData visitNegativeOpNode(NegativeOpNode node) { SoyData operand = visit(node.getChild(0)); if (operand instanceof IntegerData) { return convertResult( - operand.integerValue() ); } else { return convertResult( - operand.floatValue() ); } }
@Override protected SoyData visitPlusOpNode(PlusOpNode node) { SoyData operand0 = visit(node.getChild(0)); SoyData operand1 = visit(node.getChild(1)); if (operand0 instanceof IntegerData && operand1 instanceof IntegerData) { return convertResult(operand0.integerValue() + operand1.integerValue()); } else if (operand0 instanceof StringData || operand1 instanceof StringData) { // String concatenation. Note we're calling toString() instead of stringValue() in case one // of the operands needs to be coerced to a string. return convertResult(operand0.toString() + operand1.toString()); } else { return convertResult(operand0.numberValue() + operand1.numberValue()); } }
private SoyData visitIsFirstFunction(FunctionNode node) { int localVarIndex; try { DataRefNode dataRef = (DataRefNode) node.getChild(0); String localVarName = dataRef.getFirstKey(); localVarIndex = getLocalVar(localVarName + "__index").integerValue(); } catch (Exception e) { throw new RenderException("Failed to evaluate function call " + node.toSourceString() + "."); } return convertResult(localVarIndex == 0); }
private SoyData visitIndexFunction(FunctionNode node) { int localVarIndex; try { DataRefNode dataRef = (DataRefNode) node.getChild(0); String localVarName = dataRef.getFirstKey(); localVarIndex = getLocalVar(localVarName + "__index").integerValue(); } catch (Exception e) { throw new RenderException("Failed to evaluate function call " + node.toSourceString() + "."); } return convertResult(localVarIndex); }