This is an automated email from the ASF dual-hosted git repository. asf-gitbox-commits pushed a commit to branch geoapi-4.0 in repository https://gitbox.apache.org/repos/asf/sis.git
commit 86e52fe8734d3d9207d7aeb02edaa1b692bdbb26 Author: Martin Desruisseaux <[email protected]> AuthorDate: Thu Oct 8 12:54:08 2026 +0200 Add `Vector.doubleStream()` et `Vector.createNumberFormat(Locale)` methods. --- .../main/org/apache/sis/math/ArrayVector.java | 17 ++++++++ .../main/org/apache/sis/math/DecimalFunctions.java | 2 +- .../main/org/apache/sis/math/RepeatedVector.java | 12 ++++++ .../main/org/apache/sis/math/Vector.java | 48 +++++++++++++++++++++- .../test/org/apache/sis/math/VectorTest.java | 4 ++ 5 files changed, 81 insertions(+), 2 deletions(-) diff --git a/endorsed/src/org.apache.sis.util/main/org/apache/sis/math/ArrayVector.java b/endorsed/src/org.apache.sis.util/main/org/apache/sis/math/ArrayVector.java index baea507adc..551bb722b0 100644 --- a/endorsed/src/org.apache.sis.util/main/org/apache/sis/math/ArrayVector.java +++ b/endorsed/src/org.apache.sis.util/main/org/apache/sis/math/ArrayVector.java @@ -26,6 +26,7 @@ import java.nio.FloatBuffer; import java.nio.DoubleBuffer; import java.util.Arrays; import java.util.Optional; +import java.util.stream.DoubleStream; import java.util.function.IntSupplier; import org.apache.sis.util.Classes; import org.apache.sis.util.Numbers; @@ -44,6 +45,7 @@ import org.apache.sis.measure.NumberRange; * * @param <E> the base type of elements in the vector. */ +@SuppressWarnings("EqualsAndHashcode") abstract class ArrayVector<E extends Number> extends Vector implements CheckedContainer<E>, Serializable { /** * For cross-version compatibility. @@ -307,6 +309,11 @@ abstract class ArrayVector<E extends Number> extends Vector implements CheckedCo return Optional.of(DoubleBuffer.wrap(array)); } + /** Returns a stream of current data. */ + @Override public DoubleStream doubleStream() { + return Arrays.stream(array); + } + /** Returns a copy of current data as a floating point array. */ @Override public double[] doubleValues() { return array.clone(); @@ -615,6 +622,11 @@ abstract class ArrayVector<E extends Number> extends Vector implements CheckedCo return Optional.of(LongBuffer.wrap(array)); } + /** Returns a stream of current data. */ + @Override public DoubleStream doubleStream() { + return Arrays.stream(array).mapToDouble((value) -> value); + } + /** Applies hash code contract specified {@link Vector#hashCode()}. */ @Override public final int hashCode() { return Arrays.hashCode(array); @@ -748,6 +760,11 @@ abstract class ArrayVector<E extends Number> extends Vector implements CheckedCo return Optional.of(IntBuffer.wrap(array)); } + /** Returns a stream of current data. */ + @Override public DoubleStream doubleStream() { + return Arrays.stream(array).mapToDouble((value) -> value); + } + /** Applies hash code contract specified {@link Vector#hashCode()}. */ @Override public final int hashCode() { return Arrays.hashCode(array); diff --git a/endorsed/src/org.apache.sis.util/main/org/apache/sis/math/DecimalFunctions.java b/endorsed/src/org.apache.sis.util/main/org/apache/sis/math/DecimalFunctions.java index 62bb3b7288..172dbda70e 100644 --- a/endorsed/src/org.apache.sis.util/main/org/apache/sis/math/DecimalFunctions.java +++ b/endorsed/src/org.apache.sis.util/main/org/apache/sis/math/DecimalFunctions.java @@ -523,7 +523,7 @@ public final class DecimalFunctions { } /** - * Returns {@code true} if the given numbers or equal or differ only by {@code accurate} + * Returns {@code true} if the given numbers are equal or differ only by {@code accurate} * having more non-zero trailing decimal fraction digits than {@code approximate}. * * <table class="sis"> diff --git a/endorsed/src/org.apache.sis.util/main/org/apache/sis/math/RepeatedVector.java b/endorsed/src/org.apache.sis.util/main/org/apache/sis/math/RepeatedVector.java index 21a3ce8dda..3b156133f9 100644 --- a/endorsed/src/org.apache.sis.util/main/org/apache/sis/math/RepeatedVector.java +++ b/endorsed/src/org.apache.sis.util/main/org/apache/sis/math/RepeatedVector.java @@ -17,6 +17,8 @@ package org.apache.sis.math; import java.io.Serializable; +import java.text.NumberFormat; +import java.util.Locale; import java.util.Objects; import java.util.function.IntSupplier; import org.apache.sis.measure.NumberRange; @@ -284,4 +286,14 @@ opti: if (count > 1 && cycleLength * occurrences >= size) { } return super.createSubSampling(first, step, length); } + + /** + * Suggests a format configured to the data contained in this vector. + * Because this vector is a repetition of the {@linkplain #base} vector, + * delegating to the base vector is sufficient. + */ + @Override + public NumberFormat createNumberFormat(final Locale locale) { + return base.createNumberFormat(locale); + } } diff --git a/endorsed/src/org.apache.sis.util/main/org/apache/sis/math/Vector.java b/endorsed/src/org.apache.sis.util/main/org/apache/sis/math/Vector.java index 517d867515..9a850a09e5 100644 --- a/endorsed/src/org.apache.sis.util/main/org/apache/sis/math/Vector.java +++ b/endorsed/src/org.apache.sis.util/main/org/apache/sis/math/Vector.java @@ -18,12 +18,16 @@ package org.apache.sis.math; import java.io.Serializable; import java.nio.Buffer; +import java.text.NumberFormat; +import java.util.Locale; import java.util.Arrays; import java.util.AbstractList; import java.util.RandomAccess; import java.util.StringJoiner; import java.util.Optional; import java.util.Objects; +import java.util.stream.IntStream; +import java.util.stream.DoubleStream; import java.util.function.IntSupplier; import static java.util.logging.Logger.getLogger; import org.apache.sis.measure.NumberRange; @@ -242,7 +246,7 @@ public abstract class Vector extends AbstractList<Number> implements RandomAcces * not {@link Float#TYPE}. * * <p>The information returned by this method is only indicative; it is not guaranteed to specify accurately - * this kind of objects returned by the {@link #get(int)} method. There is various situations where the types + * the kind of objects returned by the {@link #get(int)} method. There is various situations where the types * may not match:</p> * * <ul> @@ -1503,6 +1507,19 @@ search: for (;;) { return Optional.empty(); } + /** + * Returns all values in a stream of double precision floating point numbers. + * + * <p>The default implementation invokes {@link #doubleValue(int)} for all indices from 0 inclusive + * to {@link #size()} exclusive. Subclasses may override with more efficient implementation.</p> + * + * @return a stream of all floating point values in this vector. + * @since 1.7 + */ + public DoubleStream doubleStream() { + return IntStream.range(0, size()).mapToDouble(this::doubleValue); + } + /** * Copies all values in an array of double precision floating point numbers. * This method is for inter-operability with APIs requiring an array of primitive type. @@ -1603,6 +1620,35 @@ search: for (;;) { return ArrayVector.newInstance(array, isUnsigned()); } + /** + * Suggests a format configured with a number of fraction digits adapted to the data contained in this vector. + * The format will not necessarily show all significant digits. Instead, this method can use heuristic rules + * for choosing a number of fraction digits sufficient for distinguishing the majority of the values. + * + * @param locale the desired locale. + * @return format configured for the data contained in this vector. + * + * @since 1.7 + */ + public NumberFormat createNumberFormat(final Locale locale) { + if (isInteger()) { + return NumberFormat.getIntegerInstance(locale); + } + final int digits; + final Number increment = increment(0); + if (increment != null) { + digits = DecimalFunctions.fractionDigitsForDelta(increment.doubleValue(), false); + } else { + final var stats = new Statistics(null); + doubleStream().forEach(stats); + digits = Numerics.suggestFractionDigits(stats); + } + final NumberFormat format = NumberFormat.getNumberInstance(locale); + format.setMinimumFractionDigits(digits); + format.setMaximumFractionDigits(digits); + return format; + } + /** * Returns a string representation of this vector. * diff --git a/endorsed/src/org.apache.sis.util/test/org/apache/sis/math/VectorTest.java b/endorsed/src/org.apache.sis.util/test/org/apache/sis/math/VectorTest.java index f5e69f908d..8dc4f00034 100644 --- a/endorsed/src/org.apache.sis.util/test/org/apache/sis/math/VectorTest.java +++ b/endorsed/src/org.apache.sis.util/test/org/apache/sis/math/VectorTest.java @@ -30,6 +30,7 @@ import org.apache.sis.test.TestCase; * * @author Martin Desruisseaux (Geomatys) */ +@SuppressWarnings("exports") public final class VectorTest extends TestCase { /** * The tested vector. @@ -182,6 +183,7 @@ public final class VectorTest extends TestCase { assertSame(vector, Vector.create(vector, false)); assertEquals(array.length, vector.size()); assertEquals(Float.class, vector.getElementType()); + assertArrayEquals(array, vector.floatValues()); /* * Tests element values. */ @@ -205,6 +207,8 @@ public final class VectorTest extends TestCase { assertSame(vector, Vector.create(vector, false)); assertEquals(array.length, vector.size()); assertEquals(Double.class, vector.getElementType()); + assertArrayEquals(array, vector.doubleValues()); + assertArrayEquals(array, vector.doubleStream().toArray()); /* * Test element values. */
