You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by an...@apache.org on 2015/12/29 22:23:25 UTC

[04/12] jena git commit: Tuple.asArray

Tuple.asArray


Project: http://git-wip-us.apache.org/repos/asf/jena/repo
Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/9687f836
Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/9687f836
Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/9687f836

Branch: refs/heads/master
Commit: 9687f8368960d6505d910313d752a68194dbea23
Parents: c665f63
Author: Andy Seaborne <an...@seaborne.org>
Authored: Mon Dec 28 16:57:39 2015 +0000
Committer: Andy Seaborne <an...@seaborne.org>
Committed: Mon Dec 28 16:57:39 2015 +0000

----------------------------------------------------------------------
 .../org/apache/jena/atlas/lib/tuple/Tuple.java  | 10 ++++++++
 .../apache/jena/atlas/lib/tuple/TestTuple.java  | 24 ++++++++++++++++++++
 2 files changed, 34 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jena/blob/9687f836/jena-base/src/main/java/org/apache/jena/atlas/lib/tuple/Tuple.java
----------------------------------------------------------------------
diff --git a/jena-base/src/main/java/org/apache/jena/atlas/lib/tuple/Tuple.java b/jena-base/src/main/java/org/apache/jena/atlas/lib/tuple/Tuple.java
index ec818b2..1f60286 100644
--- a/jena-base/src/main/java/org/apache/jena/atlas/lib/tuple/Tuple.java
+++ b/jena-base/src/main/java/org/apache/jena/atlas/lib/tuple/Tuple.java
@@ -22,6 +22,8 @@ import java.util.List ;
 import java.util.function.Consumer ;
 import java.util.stream.Stream ;
 
+import org.apache.jena.atlas.lib.ArrayUtils ;
+
 /** A Tuple is the same class of item */
 public interface Tuple<X> extends Iterable<X> {
     /** Get the i'th element, for i in the range 0 to len()-1 
@@ -60,4 +62,12 @@ public interface Tuple<X> extends Iterable<X> {
     
     /** Copy the Tuple into the array */ 
     public void copyInto(X[] array, int start, int length) ;
+    
+    /** Copy the Tuple into the array */ 
+    public default X[] asArray(Class<X> cls) {
+        X[] elts = ArrayUtils.alloc(cls, len()) ;
+        for ( int i = 0 ; i < len() ; i++ )
+            elts[i] = get(i) ;
+        return elts ;
+    }
 }

http://git-wip-us.apache.org/repos/asf/jena/blob/9687f836/jena-base/src/test/java/org/apache/jena/atlas/lib/tuple/TestTuple.java
----------------------------------------------------------------------
diff --git a/jena-base/src/test/java/org/apache/jena/atlas/lib/tuple/TestTuple.java b/jena-base/src/test/java/org/apache/jena/atlas/lib/tuple/TestTuple.java
index 4c31746..f74de38 100644
--- a/jena-base/src/test/java/org/apache/jena/atlas/lib/tuple/TestTuple.java
+++ b/jena-base/src/test/java/org/apache/jena/atlas/lib/tuple/TestTuple.java
@@ -18,6 +18,7 @@
 
 package org.apache.jena.atlas.lib.tuple;
 
+import static org.junit.Assert.assertArrayEquals ;
 import static org.junit.Assert.assertEquals ;
 import static org.junit.Assert.assertNotEquals ;
 import static org.junit.Assert.fail ;
@@ -173,6 +174,29 @@ public class TestTuple {
         assertNotEquals(tuple1, tuple2) ;
     }
 
+    @Test public void tuple_array_1() {
+        Tuple<Integer> tuple1 = TupleFactory.tuple(9,8,7) ;
+        Integer[] array = tuple1.asArray(Integer.class) ;
+        Tuple<Integer> tuple2 = TupleFactory.create(array) ;
+        assertEquals(tuple1, tuple2) ;
+    }
+
+    @Test public void tuple_array_2() {
+        Tuple<Integer> tuple1 = TupleFactory.tuple(9,8,7) ;
+        Integer[] array = new Integer[2] ;
+        tuple1.copyInto(array, 0, 2) ;
+        Integer[] array1 = { 9, 8 } ;
+        assertArrayEquals(array1, array) ;
+    }
+
+    @Test public void tuple_array_3() {
+        Tuple<Integer> tuple1 = TupleFactory.tuple(9,8,7) ;
+        Integer[] array = new Integer[3] ;
+        tuple1.copyInto(array) ;
+        Integer[] array1 = { 9, 8, 7 } ;
+        assertArrayEquals(array1, array) ;
+    }
+
     private void check(Tuple<Integer> tuple) {
         int val = 9 ;
         for ( int i = 0 ; i < tuple.len() ; i++ ) {