jena-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a...@apache.org
Subject svn commit: r1466024 - /jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/iterator/Iter.java
Date Tue, 09 Apr 2013 13:34:22 GMT
Author: andy
Date: Tue Apr  9 13:34:22 2013
New Revision: 1466024

URL: http://svn.apache.org/r1466024
Log:
Add take(Iterator, N)

Modified:
    jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/iterator/Iter.java

Modified: jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/iterator/Iter.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/iterator/Iter.java?rev=1466024&r1=1466023&r2=1466024&view=diff
==============================================================================
--- jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/iterator/Iter.java (original)
+++ jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/iterator/Iter.java Tue Apr  9
13:34:22 2013
@@ -26,8 +26,6 @@ import org.apache.jena.atlas.lib.ActionK
 import org.apache.jena.atlas.lib.Closeable ;
 import org.apache.jena.atlas.lib.Sink ;
 
-
-
 public class Iter<T> implements Iterable<T>, Iterator<T>
 {
     // First part : the static function library.
@@ -460,6 +458,55 @@ public class Iter<T> implements Iterable
         return filter(iter, new FilterOutNulls<T>()) ;
     }
     
+    /** Take the first N elements of an iterator - stop early if too few */ 
+    public static <T> List<T> take(Iterator<T> iter, int N)
+    {
+        iter = new IteratorN<T>(iter, N) ;
+        List<T> x = new ArrayList<T>(N) ;
+        for ( ; iter.hasNext() ; )
+            x.add(iter.next()) ;
+        return x ;
+    }
+    
+    /** Iterator that only returns upto N items */
+    static class IteratorN<T> implements Iterator<T>
+    {
+        private final Iterator<T> iter ;
+        private final int N ;
+        private int count ;
+
+        IteratorN(Iterator<T> iter, int N) {
+            this.iter = iter ;
+            this.N  = N ;
+            this.count = 0 ;
+        }
+
+        @Override
+        public boolean hasNext()
+        {
+            if ( count >= N )
+                return false ;
+            return iter.hasNext() ;
+        }
+
+        @Override
+        public T next()
+        {
+            if ( count >= N )
+                throw new NoSuchElementException() ;
+            T x = iter.next() ;
+            count++ ;
+            return x ;
+        }
+
+        @Override
+        public void remove()
+        {
+            // But leave the count as-is.
+            iter.remove() ;
+        }
+    }
+    
     @SuppressWarnings("unchecked")
     public static <T> Iterator<T> convert(Iterator<?> iterator) { return
(Iterator<T>)iterator ; }
     
@@ -816,6 +863,12 @@ public class Iter<T> implements Iterable
         return new Iter<T>(IteratorCons.create(iterator, iter)) ;
     }
 
+    /** Return an Iter that yields at most the first N items */
+    public Iter<T> take(int N)
+    {
+        return Iter.iter(take(iterator, N)) ;
+    }
+    
     /** Count the iterator (this is destructive on the iterator) */ 
     public long count()
     {



Mime
View raw message