Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ private static void listFlatImpl(
public static ImmutableList<SoyValueProvider> numberListSort(
List<? extends SoyValueProvider> list) {
return ImmutableList.sortedCopyOf(
comparingDouble((SoyValueProvider arg) -> arg.resolve().numberValue()), list);
comparingDouble((SoyValueProvider arg) -> arg.resolve().floatValue()), list);
}

@Nonnull
Expand Down Expand Up @@ -344,7 +344,7 @@ 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()));
return FloatData.forValue(Math.max(arg0.floatValue(), arg1.floatValue()));
}
}

Expand All @@ -353,7 +353,7 @@ 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()));
return FloatData.forValue(Math.min(arg0.floatValue(), arg1.floatValue()));
}
}

Expand All @@ -365,7 +365,7 @@ public static FloatData parseFloat(String str) {

@Nullable
public static IntegerData parseInt(String str, SoyValue radixVal) {
int radix = SoyValue.isNullish(radixVal) ? 10 : (int) radixVal.numberValue();
int radix = SoyValue.isNullish(radixVal) ? 10 : (int) radixVal.floatValue();
if (radix < 2 || radix > 36) {
return null;
}
Expand All @@ -389,11 +389,11 @@ public static NumberData round(SoyValue value, int numDigitsAfterPoint) {
if (numDigitsAfterPoint == 0) {
return IntegerData.forValue(round(value));
} else if (numDigitsAfterPoint > 0) {
double valueDouble = value.numberValue();
double valueDouble = value.floatValue();
double shift = Math.pow(10, numDigitsAfterPoint);
return FloatData.forValue(Math.round(valueDouble * shift) / shift);
} else {
double valueDouble = value.numberValue();
double valueDouble = value.floatValue();
double shift = Math.pow(10, -numDigitsAfterPoint);
return IntegerData.forValue((int) (Math.round(valueDouble / shift) * shift));
}
Expand All @@ -404,7 +404,7 @@ public static long round(SoyValue value) {
if (value instanceof IntegerData) {
return value.longValue();
} else {
return Math.round(value.numberValue());
return Math.round(value.floatValue());
}
}

Expand Down Expand Up @@ -465,7 +465,7 @@ public static String strSub(SoyValue str, NumberData start) {

public static String strSub(SoyValue str, NumberData start, NumberData end) {
// TODO(b/74259210) -- Change the first param to String & avoid using stringValue().
if (start.numberValue() > end.numberValue()) {
if (start.floatValue() > end.floatValue()) {
return strSub(str, end, start);
}
String string = str.stringValue();
Expand Down Expand Up @@ -496,7 +496,7 @@ public static ImmutableList<StringData> strSplit(String str, String sep, NumberD
ImmutableList.Builder<StringData> builder = ImmutableList.builder();
int truncLimit = -1;
if (limit != null) {
truncLimit = (int) limit.numberValue();
truncLimit = (int) limit.floatValue();
}
if (truncLimit == 0) {
return builder.build();
Expand Down Expand Up @@ -541,22 +541,22 @@ public static boolean protoEquals(Message proto1, Message proto2) {
}

private static int clampListIndex(List<?> list, NumberData index) {
int truncIndex = (int) index.numberValue();
int truncIndex = (int) index.floatValue();
int size = list.size();
int clampLowerBound = Math.max(0, truncIndex >= 0 ? truncIndex : size + truncIndex);
// Clamp upper bound
return Math.min(size, clampLowerBound);
}

private static int clampStrIndex(String str, NumberData position) {
int clampLowerBound = Math.max(0, (int) position.numberValue());
int clampLowerBound = Math.max(0, (int) position.floatValue());
// Clamp upper bound
return Math.min(str.length(), clampLowerBound);
}

/** Returns whether the argument is a finite value (not NaN or Infinity). */
public static boolean isFinite(SoyValue arg) {
return arg instanceof NumberData && Double.isFinite(arg.numberValue());
return arg instanceof NumberData && Double.isFinite(arg.floatValue());
}

private static String joinHelper(List<SoyValue> values, String delimiter) {
Expand Down
16 changes: 8 additions & 8 deletions java/src/com/google/template/soy/data/ProtoFieldInterpreter.java
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ public SoyValue soyFromProto(Object field) {

@Override
public Object protoFromSoy(SoyValue field) {
return Ints.saturatedCast(field.coerceToLong());
return Ints.saturatedCast(field.longValue());
}
};

Expand All @@ -336,7 +336,7 @@ public SoyValue soyFromProto(Object field) {

@Override
public Object protoFromSoy(SoyValue field) {
return UnsignedInts.saturatedCast(field.coerceToLong());
return UnsignedInts.saturatedCast(field.longValue());
}
};

Expand All @@ -351,9 +351,9 @@ public SoyValue soyFromProto(Object field) {
@Override
public Object protoFromSoy(SoyValue field) {
if (field instanceof GbigintData) {
return ((GbigintData) field).longValue();
return field.longValue();
}
return field.coerceToLong();
return field.longValue();
}
};

Expand All @@ -368,7 +368,7 @@ public SoyValue soyFromProto(Object field) {
@Override
public Object protoFromSoy(SoyValue field) {
if (field instanceof GbigintData) {
return ((GbigintData) field).longValue();
return field.longValue();
}
return Long.parseLong(field.stringValue());
}
Expand Down Expand Up @@ -446,7 +446,7 @@ public SoyValue soyFromProto(Object field) {

@Override
public Object protoFromSoy(SoyValue field) {
return (float) field.numberValue();
return (float) field.floatValue();
}
};

Expand All @@ -460,7 +460,7 @@ public SoyValue soyFromProto(Object field) {

@Override
public Object protoFromSoy(SoyValue field) {
return field.numberValue();
return field.floatValue();
}
};

Expand Down Expand Up @@ -612,7 +612,7 @@ public SoyValue soyFromProto(Object field) {

@Override
public Object protoFromSoy(SoyValue field) {
return ((SoyProtoValue) field).getProto();
return field.getProto();
}
};

Expand Down
10 changes: 6 additions & 4 deletions java/src/com/google/template/soy/data/SoyValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,14 @@ public SoyValueProvider coerceToBooleanProvider() {
* #longValue()} this method is expected to succeed for any {@link
* com.google.template.soy.data.restricted.NumberData}.
*/
public long coerceToLong() {
throw new SoyDataException("'" + this + "' cannot be coerced to long");
@Deprecated
public final long coerceToLong() {
return longValue();
}

public int coerceToInt() {
throw new SoyDataException("'" + this + "' cannot be coerced to int");
@Deprecated
public final int coerceToInt() {
return integerValue();
}

/**
Expand Down
22 changes: 11 additions & 11 deletions java/src/com/google/template/soy/data/restricted/FloatData.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import com.google.errorprone.annotations.Immutable;
import com.google.template.soy.base.internal.BaseUtils;
import com.google.template.soy.data.SoyValue;
import com.google.template.soy.base.internal.NumericCoercions;
import javax.annotation.Nonnull;

/** Float data. */
Expand Down Expand Up @@ -48,11 +48,6 @@ public double getValue() {
return value;
}

@Override
public double floatValue() {
return value;
}

@Override
@Nonnull
public String toString() {
Expand All @@ -75,18 +70,23 @@ public String coerceToString() {
}

@Override
public double toFloat() {
public double floatValue() {
return value;
}

@Override
public Number javaNumberValue() {
return value;
public boolean isSafeJsInteger() {
return value % 1 == 0 && NumericCoercions.isInRange((long) value);
}

@Override
public long longValue() {
return NumericCoercions.safeLong(value);
}

@Override
public SoyValue checkNullishFloat() {
return this;
public int integerValue() {
return NumericCoercions.safeInt(value);
}

@Override
Expand Down
17 changes: 5 additions & 12 deletions java/src/com/google/template/soy/data/restricted/IntegerData.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import com.google.errorprone.annotations.Immutable;
import com.google.template.soy.base.internal.NumericCoercions;
import com.google.template.soy.data.SoyValue;
import javax.annotation.Nonnull;

/** Integer data. */
Expand Down Expand Up @@ -116,6 +115,10 @@ public long getValue() {
return value;
}

public boolean isSafeJsInteger() {
return NumericCoercions.isInRange(value);
}

@Override
public int integerValue() {
return NumericCoercions.safeInt(value);
Expand Down Expand Up @@ -148,12 +151,7 @@ public String coerceToString() {
}

@Override
public double toFloat() {
return value;
}

@Override
public Number javaNumberValue() {
public double floatValue() {
return value;
}

Expand All @@ -169,11 +167,6 @@ public boolean equals(Object other) {
}
}

@Override
public SoyValue checkNullishInt() {
return this;
}

@Override
public long coerceToIndex() {
return value;
Expand Down
49 changes: 15 additions & 34 deletions java/src/com/google/template/soy/data/restricted/NumberData.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,60 +17,41 @@
package com.google.template.soy.data.restricted;

import com.google.common.primitives.Longs;
import com.google.template.soy.base.internal.NumericCoercions;
import com.google.template.soy.data.SoyValue;
import javax.annotation.Nonnull;

/** Abstract superclass for number data (integers and floats). */
public abstract class NumberData extends PrimitiveData {

/**
* Gets the float value of this number data object. If this object is actually an integer, its
* value will be converted to a float before being returned.
*
* @return The float value of this number data object.
*/
public abstract double toFloat();

/**
* Returns true if this value is a whole integer in the range representable in JavaScript without
* a loss of precision.
*/
public boolean isSafeJsInteger() {
double val = numberValue();
return val % 1 == 0 && NumericCoercions.isInRange((long) val);
}

@Override
public long coerceToLong() {
return javaNumberValue().longValue();
public final SoyValue checkNullishInt() {
return this;
}

@Override
public int coerceToInt() {
long l = coerceToLong();
if (l > Integer.MAX_VALUE || l < Integer.MIN_VALUE) {
throw new IllegalArgumentException();
}
return (int) l;
public final SoyValue checkNullishFloat() {
return this;
}

/**
* Returns true if this value is a whole integer in the range representable in JavaScript without
* a loss of precision.
*/
public abstract boolean isSafeJsInteger();

@Override
public double numberValue() {
return toFloat();
@Deprecated
public final double numberValue() {
return floatValue();
}

@Nonnull
public abstract Number javaNumberValue();

@Override
public boolean equals(Object other) {
return other instanceof NumberData && ((NumberData) other).toFloat() == this.toFloat();
return other instanceof NumberData && ((NumberData) other).floatValue() == this.floatValue();
}

@Override
public int hashCode() {
return Longs.hashCode(Double.doubleToLongBits(toFloat()));
return Longs.hashCode(Double.doubleToLongBits(floatValue()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ public static String formatNum(
} else if (number instanceof NumberData) {
return formatInternal(
uLocale,
((NumberData) number).toFloat(),
((NumberData) number).floatValue(),
formatType,
numbersKeyword,
minFractionDigits != null ? (int) minFractionDigits.numberValue() : null,
maxFractionDigits != null ? (int) maxFractionDigits.numberValue() : null);
minFractionDigits != null ? (int) minFractionDigits.floatValue() : null,
maxFractionDigits != null ? (int) maxFractionDigits.floatValue() : null);
} else {
return "NaN";
}
Expand Down
2 changes: 1 addition & 1 deletion java/src/com/google/template/soy/jbcsrc/ProtoUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -1896,7 +1896,7 @@ private static Type unboxUnchecked(
}

if (asType.equals(double.class)) {
MethodRefs.SOY_VALUE_NUMBER_VALUE.invokeUnchecked(cb);
MethodRefs.SOY_VALUE_FLOAT_VALUE.invokeUnchecked(cb);
return Type.DOUBLE_TYPE;
}

Expand Down
Loading