This is an automated email from the ASF dual-hosted git repository. desruisseaux pushed a commit to branch geoapi-4.0 in repository https://gitbox.apache.org/repos/asf/sis.git
commit 85aeeb0626d8ee53d7863ca9a78b82de72291a42 Author: Martin Desruisseaux <[email protected]> AuthorDate: Mon Jul 3 15:54:31 2023 +0200 Remove `Numerics.CACHE` in anticipation for "Value object" in future Java version. We note also that the standard autoboxing method is flagged as @IntrinsicCandidate. --- .../apache/sis/feature/DefaultAttributeType.java | 5 +- .../sis/parameter/DefaultParameterDescriptor.java | 6 +-- .../sis/parameter/DefaultParameterValue.java | 2 +- .../org/apache/sis/internal/util/Numerics.java | 59 +--------------------- .../java/org/apache/sis/math/SequenceVector.java | 5 +- .../java/org/apache/sis/measure/NumberRange.java | 4 +- .../src/main/java/org/apache/sis/util/Numbers.java | 13 +++-- .../org/apache/sis/internal/util/NumericsTest.java | 28 ---------- .../sis/internal/storage/MetadataBuilder.java | 5 +- .../storage/xml/stream/StaxStreamReader.java | 3 +- 10 files changed, 20 insertions(+), 110 deletions(-) diff --git a/core/sis-feature/src/main/java/org/apache/sis/feature/DefaultAttributeType.java b/core/sis-feature/src/main/java/org/apache/sis/feature/DefaultAttributeType.java index 1e9dcbb97d..45b86c8903 100644 --- a/core/sis-feature/src/main/java/org/apache/sis/feature/DefaultAttributeType.java +++ b/core/sis-feature/src/main/java/org/apache/sis/feature/DefaultAttributeType.java @@ -26,7 +26,6 @@ import java.io.InvalidObjectException; import org.opengis.util.GenericName; import org.opengis.util.InternationalString; import org.apache.sis.util.Classes; -import org.apache.sis.internal.util.Numerics; import static org.apache.sis.util.ArgumentChecks.*; @@ -94,7 +93,7 @@ import org.opengis.feature.AttributeType; * * @author Johann Sorel (Geomatys) * @author Martin Desruisseaux (Geomatys) - * @version 0.8 + * @version 1.4 * * @param <V> the type of attribute values. If the attribute supports multi-occurrences, * then this is the type of elements (not the collection type). @@ -194,7 +193,7 @@ public class DefaultAttributeType<V> extends FieldType implements AttributeType< ensureNonNull("valueClass", valueClass); ensureCanCast("defaultValue", valueClass, defaultValue); this.valueClass = valueClass; - this.defaultValue = Numerics.cached(defaultValue); + this.defaultValue = defaultValue; if (characterizedBy != null && characterizedBy.length != 0) { characteristics = CharacteristicTypeMap.create(this, characterizedBy.clone()); } diff --git a/core/sis-referencing/src/main/java/org/apache/sis/parameter/DefaultParameterDescriptor.java b/core/sis-referencing/src/main/java/org/apache/sis/parameter/DefaultParameterDescriptor.java index f7f2158732..2a11e99fe0 100644 --- a/core/sis-referencing/src/main/java/org/apache/sis/parameter/DefaultParameterDescriptor.java +++ b/core/sis-referencing/src/main/java/org/apache/sis/parameter/DefaultParameterDescriptor.java @@ -34,7 +34,6 @@ import org.apache.sis.util.ComparisonMode; import org.apache.sis.util.resources.Errors; import org.apache.sis.measure.Range; import org.apache.sis.measure.MeasurementRange; -import org.apache.sis.internal.util.Numerics; import org.apache.sis.internal.util.CollectionsExt; import org.apache.sis.internal.jaxb.Context; import org.apache.sis.internal.jaxb.gco.PropertyType; @@ -233,16 +232,15 @@ public class DefaultParameterDescriptor<T> extends AbstractParameterDescriptor i } this.valueClass = valueClass; this.valueDomain = valueDomain; - this.defaultValue = Numerics.cached(defaultValue); + this.defaultValue = defaultValue; /* * If the caller specified a set of valid values, then copy the values in * a new set and verify their type and inclusion in the [min … max] range. */ if (validValues != null) { final Set<T> valids = CollectionsExt.createSetForType(valueClass, validValues.length); - for (T value : validValues) { + for (final T value : validValues) { if (value != null) { - value = Numerics.cached(value); final Verifier error = Verifier.ensureValidValue(valueClass, null, valueDomain, value); if (error != null) { throw new IllegalArgumentException(error.message(properties, super.getName().getCode(), value)); diff --git a/core/sis-referencing/src/main/java/org/apache/sis/parameter/DefaultParameterValue.java b/core/sis-referencing/src/main/java/org/apache/sis/parameter/DefaultParameterValue.java index 6d708328c2..149fc4a4e7 100644 --- a/core/sis-referencing/src/main/java/org/apache/sis/parameter/DefaultParameterValue.java +++ b/core/sis-referencing/src/main/java/org/apache/sis/parameter/DefaultParameterValue.java @@ -697,7 +697,7 @@ convert: if (componentType != null) { if (Number.class.isAssignableFrom(valueClass)) { return Numbers.wrap(value, (Class<? extends Number>) valueClass); } else { - return Numerics.valueOf(value); + return value; } } diff --git a/core/sis-utility/src/main/java/org/apache/sis/internal/util/Numerics.java b/core/sis-utility/src/main/java/org/apache/sis/internal/util/Numerics.java index 6b8a48982b..f1f7b8ec14 100644 --- a/core/sis-utility/src/main/java/org/apache/sis/internal/util/Numerics.java +++ b/core/sis-utility/src/main/java/org/apache/sis/internal/util/Numerics.java @@ -16,12 +16,10 @@ */ package org.apache.sis.internal.util; -import java.util.Map; -import java.util.HashMap; import java.text.Format; import java.text.DecimalFormat; -import java.util.function.BiFunction; import java.math.BigInteger; +import java.util.function.BiFunction; import org.apache.sis.util.Debug; import org.apache.sis.util.Static; import org.apache.sis.util.Workaround; @@ -46,35 +44,6 @@ import org.apache.sis.internal.system.Configuration; * @since 0.3 */ public final class Numerics extends Static { - /** - * Some frequently used {@link Double} values. As of Java 11, those values do not - * seem to be cached by {@link Double#valueOf(double)} like JDK does for integers. - */ - private static final Map<Object,Object> CACHE = new HashMap<>(32); - static { - cache( 0); - cache( 1); - cache( 10); - cache( 60); - cache( 90); - cache( 100); - cache( 180); - cache( 180*60*60); - cache( 360); - cache(1000); - cache(Double.POSITIVE_INFINITY); - // Do not cache NaN values because Double.equals(Object) consider all NaN as equal. - } - - /** - * Helper method for the construction of the {@link #CACHE} map. - */ - private static void cache(final double value) { - Double boxed; - boxed = value; CACHE.put(boxed, boxed); - boxed = -value; CACHE.put(boxed, boxed); - } - /** * Maximum number of rows or columns in Apache SIS matrices. We define a maximum because SIS is expected to work * mostly with small matrices, because their sizes are related to the number of dimensions in coordinate systems. @@ -348,34 +317,10 @@ public final class Numerics extends Static { try { return Fraction.valueOf(numerator, denominator).unique(); } catch (ArithmeticException e) { - return valueOf(numerator / (double) denominator); + return numerator / (double) denominator; } } - /** - * If the given value is presents in the cache, returns the cached value. - * Otherwise returns the given value as-is. - * - * @param <T> the type of the given value. - * @param value the given value for which to get a cached instance, if one exists. - * @return an object equals to the given value (may be the given instance itself). - */ - @SuppressWarnings("unchecked") - public static <T> T cached(final T value) { - return (T) CACHE.getOrDefault(value, value); - } - - /** - * Wraps the given {@code value} in a {@link Double} wrapper, using one of the cached instance if possible. - * - * @param value the value to get as a {@code Double}. - * @return the given value as a {@code Double}. - */ - public static Double valueOf(final double value) { - final Double boxed = value; - return (Double) CACHE.getOrDefault(boxed, boxed); - } - /** * Returns {@code true} if the given floats are equals. Positive and negative zero are * considered different, while a NaN value is considered equal to all other NaN values. diff --git a/core/sis-utility/src/main/java/org/apache/sis/math/SequenceVector.java b/core/sis-utility/src/main/java/org/apache/sis/math/SequenceVector.java index c3048be397..ba1cdfa90f 100644 --- a/core/sis-utility/src/main/java/org/apache/sis/math/SequenceVector.java +++ b/core/sis-utility/src/main/java/org/apache/sis/math/SequenceVector.java @@ -22,7 +22,6 @@ import org.apache.sis.measure.NumberRange; import org.apache.sis.util.Numbers; import org.apache.sis.util.ArgumentChecks; import org.apache.sis.util.resources.Errors; -import org.apache.sis.internal.util.Numerics; /** @@ -30,7 +29,7 @@ import org.apache.sis.internal.util.Numerics; * Values may be {@code long} or {@code double} types. * * @author Martin Desruisseaux (MPO, Geomatys) - * @version 1.1 + * @version 1.4 * @since 0.8 */ abstract class SequenceVector extends Vector implements Serializable { @@ -209,7 +208,7 @@ abstract class SequenceVector extends Vector implements Serializable { /** Returns the increment between all consecutive values. */ @Override public final Number increment(final double tolerance) { - return Numerics.valueOf(increment); // Always Double even if data type is Float. + return increment; // Always Double even if data type is Float. } /** Computes the minimal and maximal values in this vector. */ diff --git a/core/sis-utility/src/main/java/org/apache/sis/measure/NumberRange.java b/core/sis-utility/src/main/java/org/apache/sis/measure/NumberRange.java index c4cc0a29a3..2de465e402 100644 --- a/core/sis-utility/src/main/java/org/apache/sis/measure/NumberRange.java +++ b/core/sis-utility/src/main/java/org/apache/sis/measure/NumberRange.java @@ -83,7 +83,7 @@ import org.opengis.referencing.operation.TransformException; * * @author Martin Desruisseaux (IRD, Geomatys) * @author Jody Garnett (for parameterized type inspiration) - * @version 1.3 + * @version 1.4 * * @param <E> the type of range elements as a subclass of {@link Number}. * @@ -306,7 +306,7 @@ public class NumberRange<E extends Number & Comparable<? super E>> extends Range if (Double.isNaN(value)) { throw new IllegalArgumentException(Errors.format(Errors.Keys.NotANumber_1, name)); } - return (value != infinity) ? Numerics.valueOf(value) : null; + return (value != infinity) ? value : null; } /** diff --git a/core/sis-utility/src/main/java/org/apache/sis/util/Numbers.java b/core/sis-utility/src/main/java/org/apache/sis/util/Numbers.java index 349489de61..4b4a0bad25 100644 --- a/core/sis-utility/src/main/java/org/apache/sis/util/Numbers.java +++ b/core/sis-utility/src/main/java/org/apache/sis/util/Numbers.java @@ -29,7 +29,6 @@ import java.math.BigDecimal; import java.math.BigInteger; import org.apache.sis.math.Fraction; import org.apache.sis.util.resources.Errors; -import org.apache.sis.internal.util.Numerics; import org.apache.sis.internal.util.DoubleDouble; import org.apache.sis.internal.util.CollectionsExt; @@ -88,7 +87,7 @@ public final class Numbers extends Static { new Numbers(BigDecimal .class, true, false, BIG_DECIMAL); new Numbers(BigInteger .class, false, true, BIG_INTEGER); new Numbers(Fraction .class, true, false, FRACTION); - new Numbers(Double .TYPE, Double .class, true, false, (byte) Double .SIZE, DOUBLE, 'D', Numerics .valueOf(Double.NaN)); + new Numbers(Double .TYPE, Double .class, true, false, (byte) Double .SIZE, DOUBLE, 'D', Double .valueOf(Double.NaN)); new Numbers(Float .TYPE, Float .class, true, false, (byte) Float .SIZE, FLOAT, 'F', Float .valueOf(Float .NaN)); new Numbers(Long .TYPE, Long .class, false, true, (byte) Long .SIZE, LONG, 'J', Long .valueOf( 0L)); new Numbers(Integer .TYPE, Integer .class, false, true, (byte) Integer .SIZE, INTEGER, 'I', Integer .valueOf( 0)); @@ -520,8 +519,8 @@ asLong: if (mapping != null) { isFloat = (doubleToLongBits(floatValue) == doubleToLongBits(doubleValue)); if (doubleValue != longValue) { // Do not use "isFloat ? … : …" operator as it inserts undesired automatic auto-(un)boxing. - if (isFloat) candidate = Float .valueOf(floatValue); - else candidate = Numerics.valueOf(doubleValue); + if (isFloat) candidate = floatValue; + else candidate = doubleValue; break; } // Fall through everywhere. @@ -619,7 +618,7 @@ asLong: if (mapping != null) { case INTEGER: return (N) Integer .valueOf(number. intValue()); case LONG: return (N) Long .valueOf(number. longValue()); case FLOAT: return (N) Float .valueOf(number. floatValue()); - case DOUBLE: return (N) Numerics.valueOf(number.doubleValue()); + case DOUBLE: return (N) Double .valueOf(number.doubleValue()); case FRACTION: return (N) Fraction.valueOf(number.doubleValue()); case BIG_INTEGER: { final BigInteger c; @@ -681,7 +680,7 @@ asLong: if (mapping != null) { case INTEGER: number = (N) Integer .valueOf((int) value); break; case LONG: number = (N) Long .valueOf((long) value); break; case FLOAT: number = (N) Float .valueOf((float) value); break; - case DOUBLE: return (N) Numerics .valueOf(value); // No need to verify. + case DOUBLE: return (N) Double .valueOf(value); // No need to verify. case FRACTION: return (N) Fraction .valueOf(value); // No need to verify. case BIG_INTEGER: number = (N) BigInteger.valueOf((long) value); break; case BIG_DECIMAL: return (N) BigDecimal.valueOf(value); // No need to verify. @@ -718,7 +717,7 @@ asLong: if (mapping != null) { case INTEGER: number = (N) Integer .valueOf((int) value); break; case LONG: return (N) Long .valueOf(value); // No need to verify. case FLOAT: number = (N) Float .valueOf((float) value); break; - case DOUBLE: number = (N) Numerics .valueOf((double) value); break; + case DOUBLE: number = (N) Double .valueOf((double) value); break; case FRACTION: number = (N) new Fraction ((int) value, 1); break; case BIG_INTEGER: return (N) BigInteger.valueOf(value); // No need to verify. case BIG_DECIMAL: return (N) BigDecimal.valueOf(value); // No need to verify. diff --git a/core/sis-utility/src/test/java/org/apache/sis/internal/util/NumericsTest.java b/core/sis-utility/src/test/java/org/apache/sis/internal/util/NumericsTest.java index 622b2eb02c..d1a28ccbd6 100644 --- a/core/sis-utility/src/test/java/org/apache/sis/internal/util/NumericsTest.java +++ b/core/sis-utility/src/test/java/org/apache/sis/internal/util/NumericsTest.java @@ -138,34 +138,6 @@ public final class NumericsTest extends TestCase { assertEquals(Long.MIN_VALUE, Numerics.saturatingSubtract(Long.MIN_VALUE + 10, +56)); } - /** - * Tests the {@link Numerics#cached(Object)} method. - */ - @Test - public void testCached() { - Double value; - assertEquals(value = Double.valueOf( 0), Numerics.cached(value)); - assertEquals(value = Double.valueOf( 1), Numerics.cached(value)); - assertEquals(value = Double.valueOf( -1), Numerics.cached(value)); - assertEquals(value = Double.valueOf( 10), Numerics.cached(value)); - assertEquals(value = Double.valueOf(-150), Numerics.cached(value)); - assertEquals(value = Double.valueOf( NaN), Numerics.cached(value)); - } - - /** - * Tests the {@link Numerics#valueOf(double)} method. - */ - @Test - public void testValueOf() { - double value; - assertEquals(Double.valueOf(value = 0), Numerics.valueOf(value)); - assertEquals(Double.valueOf(value = 1), Numerics.valueOf(value)); - assertEquals(Double.valueOf(value = -1), Numerics.valueOf(value)); - assertEquals(Double.valueOf(value = 10), Numerics.valueOf(value)); - assertEquals(Double.valueOf(value = -150), Numerics.valueOf(value)); - assertEquals(Double.valueOf(value = NaN), Numerics.valueOf(value)); - } - /** * Tests the {@link Numerics#epsilonEqual(double, double, ComparisonMode)} method. */ diff --git a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/MetadataBuilder.java b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/MetadataBuilder.java index d9e5ec14a3..2f335d3d03 100644 --- a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/MetadataBuilder.java +++ b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/MetadataBuilder.java @@ -82,7 +82,6 @@ import org.apache.sis.coverage.grid.GridGeometry; import org.apache.sis.coverage.grid.GridExtent; import org.apache.sis.coverage.SampleDimension; import org.apache.sis.internal.metadata.Merger; -import org.apache.sis.internal.util.Numerics; import org.apache.sis.internal.util.Strings; import org.apache.sis.util.AbstractInternationalString; import org.apache.sis.util.resources.Vocabulary; @@ -110,7 +109,7 @@ import org.opengis.feature.FeatureType; * @author Rémi Maréchal (Geomatys) * @author Thi Phuong Hao Nguyen (VNSC) * @author Alexis Manin (Geomatys) - * @version 1.3 + * @version 1.4 * @since 0.8 */ public class MetadataBuilder { @@ -3440,7 +3439,7 @@ parse: for (int i = 0; i < length;) { * @return the given value, but as an existing instance if possible. */ protected final Double shared(final double value) { - final Double n = Numerics.valueOf(value); + final Double n = value; final Object existing = sharedValues.putIfAbsent(n, n); return (existing != null) ? (Double) existing : n; } diff --git a/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/storage/xml/stream/StaxStreamReader.java b/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/storage/xml/stream/StaxStreamReader.java index 37f3feec03..f64ad1b5e7 100644 --- a/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/storage/xml/stream/StaxStreamReader.java +++ b/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/storage/xml/stream/StaxStreamReader.java @@ -40,7 +40,6 @@ import jakarta.xml.bind.JAXBElement; import jakarta.xml.bind.JAXBException; import org.apache.sis.internal.jaxb.Context; import org.apache.sis.internal.util.Strings; -import org.apache.sis.internal.util.Numerics; import org.apache.sis.internal.util.StandardDateFormat; import org.apache.sis.internal.storage.io.IOUtilities; import org.apache.sis.storage.DataStoreException; @@ -362,7 +361,7 @@ public abstract class StaxStreamReader extends StaxStreamIO implements XMLStream */ protected final Double getElementAsDouble() throws XMLStreamException { final String text = getElementText(); - return (text != null) ? Numerics.valueOf(parseDouble(text)) : null; + return (text != null) ? parseDouble(text) : null; } /**
