sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject [sis] 01/02: Mave Vector.buffer() optional.
Date Tue, 11 Dec 2018 19:11:17 GMT
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 5d1392b6a32429578fe4f78656a30fe9a9560077
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Tue Dec 11 12:45:14 2018 +0100

    Mave Vector.buffer() optional.
---
 .../main/java/org/apache/sis/math/ArrayVector.java | 25 +++++++++++-----------
 .../src/main/java/org/apache/sis/math/Vector.java  | 25 ++++++++++++++++------
 .../apache/sis/storage/netcdf/GridResource.java    |  3 ++-
 3 files changed, 33 insertions(+), 20 deletions(-)

diff --git a/core/sis-utility/src/main/java/org/apache/sis/math/ArrayVector.java b/core/sis-utility/src/main/java/org/apache/sis/math/ArrayVector.java
index f6a4a70..ed42e74 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/math/ArrayVector.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/math/ArrayVector.java
@@ -25,6 +25,7 @@ import java.nio.LongBuffer;
 import java.nio.FloatBuffer;
 import java.nio.DoubleBuffer;
 import java.util.Arrays;
+import java.util.Optional;
 import java.util.function.IntSupplier;
 import org.apache.sis.util.Numbers;
 import org.apache.sis.util.resources.Errors;
@@ -288,8 +289,8 @@ abstract class ArrayVector<E extends Number> extends Vector implements
CheckedCo
         }
 
         /** Wraps this vector in a buffer. */
-        @Override public Buffer buffer() {
-            return DoubleBuffer.wrap(array);
+        @Override public Optional<Buffer> buffer() {
+            return Optional.of(DoubleBuffer.wrap(array));
         }
 
         /** Returns a copy of current data as a floating point array. */
@@ -403,8 +404,8 @@ abstract class ArrayVector<E extends Number> extends Vector implements
CheckedCo
         }
 
         /** Wraps this vector in a buffer. */
-        @Override public final Buffer buffer() {
-            return FloatBuffer.wrap(array);
+        @Override public final Optional<Buffer> buffer() {
+            return Optional.of(FloatBuffer.wrap(array));
         }
 
         /** Returns a copy of current data as a floating point array. */
@@ -567,8 +568,8 @@ abstract class ArrayVector<E extends Number> extends Vector implements
CheckedCo
         }
 
         /** Wraps this vector in a buffer. */
-        @Override public final Buffer buffer() {
-            return LongBuffer.wrap(array);
+        @Override public final Optional<Buffer> buffer() {
+            return Optional.of(LongBuffer.wrap(array));
         }
 
         /** Applies hash code contract specified {@link Vector#hashCode()}. */
@@ -689,8 +690,8 @@ abstract class ArrayVector<E extends Number> extends Vector implements
CheckedCo
         }
 
         /** Wraps this vector in a buffer. */
-        @Override public final Buffer buffer() {
-            return IntBuffer.wrap(array);
+        @Override public final Optional<Buffer> buffer() {
+            return Optional.of(IntBuffer.wrap(array));
         }
 
         /** Applies hash code contract specified {@link Vector#hashCode()}. */
@@ -786,8 +787,8 @@ abstract class ArrayVector<E extends Number> extends Vector implements
CheckedCo
          */
 
         /** Wraps this vector in a buffer. */
-        @Override public final Buffer buffer() {
-            return ShortBuffer.wrap(array);
+        @Override public final Optional<Buffer> buffer() {
+            return Optional.of(ShortBuffer.wrap(array));
         }
 
         /** Applies hash code contract specified {@link Vector#hashCode()}. */
@@ -884,8 +885,8 @@ abstract class ArrayVector<E extends Number> extends Vector implements
CheckedCo
          */
 
         /** Wraps this vector in a buffer. */
-        @Override public final Buffer buffer() {
-            return ByteBuffer.wrap(array);
+        @Override public final Optional<Buffer> buffer() {
+            return Optional.of(ByteBuffer.wrap(array));
         }
 
         /** Applies hash code contract specified {@link Vector#hashCode()}. */
diff --git a/core/sis-utility/src/main/java/org/apache/sis/math/Vector.java b/core/sis-utility/src/main/java/org/apache/sis/math/Vector.java
index fc384a6..defac99 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/math/Vector.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/math/Vector.java
@@ -22,6 +22,7 @@ import java.util.Arrays;
 import java.util.AbstractList;
 import java.util.RandomAccess;
 import java.util.StringJoiner;
+import java.util.Optional;
 import java.util.function.IntSupplier;
 import org.apache.sis.internal.jdk9.JDK9;
 import org.apache.sis.measure.NumberRange;
@@ -85,7 +86,9 @@ import static org.apache.sis.util.ArgumentChecks.ensureValidIndex;
  * by the code that <em>create</em> a {@code Vector} object, but the data type
may not be known
  * by the code that will <em>use</em> the {@code Vector} object.
  * For example a method performing a numerical calculation may want to see the data as {@code
double} values
- * without concern about whether the data were really stored as {@code double} or as {@code
float} values.</div>
+ * without concern about whether the data were really stored as {@code double} or as {@code
float} values.
+ * For the situations where a {@link Buffer} is needed, inter-operability is provided by
the {@link #buffer()}
+ * method and by accepting buffer in the {@link #create(Object, boolean)} method.</div>
  *
  * @author  Martin Desruisseaux (MPO, Geomatys)
  * @version 1.0
@@ -104,6 +107,7 @@ public abstract class Vector extends AbstractList<Number> implements
RandomAcces
      *   <li>A {@code Number[]} array.</li>
      *   <li>A {@code String[]} array (not recommended, but happen with some file formats).</li>
      *   <li>A {@code Vector}, in which case it is returned unchanged.</li>
+     *   <li>A {@link Buffer} backed by an array.</li>
      *   <li>The {@code null} value, in which case {@code null} is returned.</li>
      * </ul>
      *
@@ -133,6 +137,14 @@ public abstract class Vector extends AbstractList<Number> implements
RandomAcces
         if (array instanceof Vector) {
             return (Vector) array;
         }
+        if (array instanceof Buffer) {
+            final Buffer buffer = (Buffer) array;
+            if (buffer.hasArray()) {
+                final int offset = buffer.arrayOffset();
+                return ArrayVector.newInstance(buffer.array(), isUnsigned)
+                        .subList(offset + buffer.position(), offset + buffer.limit());
+            }
+        }
         throw new IllegalArgumentException(Errors.format(Errors.Keys.IllegalArgumentClass_2,
"array", array.getClass()));
     }
 
@@ -948,9 +960,9 @@ search:     for (;;) {
          * Returns a buffer over the sub-section represented by this {@code SubSampling}
instance.
          */
         @Override
-        public Buffer buffer() {
+        public Optional<Buffer> buffer() {
             if (step == 1) {
-                return JDK9.slice(Vector.this.buffer().position(first).limit(first + length));
+                Vector.this.buffer().map((b) -> JDK9.slice(b.position(first).limit(first
+ length)));
             }
             return super.buffer();
         }
@@ -1275,13 +1287,12 @@ search:     for (;;) {
      * Date are provided in their "raw" form. For example unsigned integers are given as
plain {@code int} elements
      * and it is caller responsibility to use {@link Integer#toUnsignedLong(int)} if needed.
      *
-     * @return the vector data as a buffer.
-     * @throws UnsupportedOperationException if this vector can not be represented by a buffer.
+     * @return the vector data as a buffer. Absent if this vector is not backed by an array
or a buffer.
      *
      * @since 1.0
      */
-    public Buffer buffer() {
-        throw new UnsupportedOperationException();
+    public Optional<Buffer> buffer() {
+        return Optional.empty();
     }
 
     /**
diff --git a/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/GridResource.java
b/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/GridResource.java
index 4c69e52..9d4f4bc 100644
--- a/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/GridResource.java
+++ b/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/GridResource.java
@@ -262,7 +262,8 @@ final class GridResource extends AbstractGridResource implements ResourceOnFileS
         } catch (IOException e) {
             throw new DataStoreException(e);
         }
-        return new Image(domain, getSampleDimensions(), data.getDataType().toJava2D(samples.buffer()));
+        // Optional.orElseThrow() below should never fail since Variable.read(…) wraps
primitive array.
+        return new Image(domain, getSampleDimensions(), data.getDataType().toJava2D(samples.buffer().get()));
     }
 
     /**


Mime
View raw message