Neo4j Community

org.neo4j.helpers.collection
Class IteratorUtil

java.lang.Object
  extended by org.neo4j.helpers.collection.IteratorUtil

public abstract class IteratorUtil
extends Object

Contains common functionality regarding Iterators and Iterables.


Constructor Summary
IteratorUtil()
           
 
Method Summary
static
<C extends Collection<T>,T>
C
addToCollection(Iterable<T> iterable, C collection)
          Adds all the items in iterator to collection.
static
<C extends Collection<T>,T>
C
addToCollection(Iterator<T> iterator, C collection)
          Adds all the items in iterator to collection.
static
<T> Collection<T>
asCollection(Iterable<T> iterable)
          Creates a collection from an iterable.
static ClosableIterable<String> asIterable(File file)
          Creates an Iterable for iterating over the lines of a text file.
static
<T> Iterable<T>
asIterable(Iterator<T> iterator)
          Exposes iterator as an Iterable.
static ClosableIterator<String> asIterator(File file)
          Creates an Iterator for iterating over the lines of a text file.
static
<T> int
count(Iterable<T> iterable)
          Counts the number of items in the iterable by looping through it.
static
<T> int
count(Iterator<T> iterator)
          Counts the number of items in the iterator by looping through it.
static
<T> T
first(Iterable<T> iterable)
          Returns the given iterable's first element.
static
<T> T
first(Iterator<T> iterator)
          Returns the given iterator's first element.
static
<T> T
firstOrNull(Iterable<T> iterable)
          Returns the given iterable's first element or null if no element found.
static
<T> T
firstOrNull(Iterator<T> iterator)
          Returns the given iterator's first element or null if no element found.
static
<T> T
fromEnd(Iterable<T> iterable, int n)
          Returns the iterator's n:th item from the end of the iteration.
static
<T> T
fromEnd(Iterator<T> iterator, int n)
          Returns the iterator's n:th item from the end of the iteration.
static
<T> T
fromEndOrNull(Iterable<T> iterable, int countFromEnd)
          Returns the iterator's n:th item from the end of the iteration.
static
<T> T
fromEndOrNull(Iterator<T> iterator, int n)
          Returns the iterator's n:th item from the end of the iteration.
static
<T> T
last(Iterable<T> iterable)
          Returns the given iterable's last element.
static
<T> T
last(Iterator<T> iterator)
          Returns the given iterator's last element.
static
<T> T
lastOrNull(Iterable<T> iterable)
          Returns the given iterable's last element or null if no element found.
static
<T> T
lastOrNull(Iterator<T> iterator)
          Returns the given iterator's last element or null if no element found.
static
<T> Iterable<T>
loop(Iterator<T> iterator)
          Convenience method for looping over an Iterator.
static
<T> T
single(Iterable<T> iterable)
          Returns the given iterable's single element.
static
<T> T
single(Iterator<T> iterator)
          Returns the given iterator's single element.
static
<T> T
singleOrNull(Iterable<T> iterable)
          Returns the given iterable's single element or null if no element found.
static
<T> T
singleOrNull(Iterator<T> iterator)
          Returns the given iterator's single element or null if no element found.
static
<T> void
streamToFile(Iterable<T> iterable, File file)
           
static
<T> void
streamToFile(Iterator<T> iterator, File file)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

IteratorUtil

public IteratorUtil()
Method Detail

firstOrNull

public static <T> T firstOrNull(Iterator<T> iterator)
Returns the given iterator's first element or null if no element found.

Type Parameters:
T - the type of elements in iterator.
Parameters:
iterator - the Iterator to get elements from.
Returns:
the first element in the iterator, or null if no element found.

first

public static <T> T first(Iterator<T> iterator)
Returns the given iterator's first element. If no element is found a NoSuchElementException is thrown.

Type Parameters:
T - the type of elements in iterator.
Parameters:
iterator - the Iterator to get elements from.
Returns:
the first element in the iterator.
Throws:
{@link - NoSuchElementException} if no element found.

lastOrNull

public static <T> T lastOrNull(Iterator<T> iterator)
Returns the given iterator's last element or null if no element found.

Type Parameters:
T - the type of elements in iterator.
Parameters:
iterator - the Iterator to get elements from.
Returns:
the last element in the iterator, or null if no element found.

last

public static <T> T last(Iterator<T> iterator)
Returns the given iterator's last element. If no element is found a NoSuchElementException is thrown.

Type Parameters:
T - the type of elements in iterator.
Parameters:
iterator - the Iterator to get elements from.
Returns:
the last element in the iterator.
Throws:
{@link - NoSuchElementException} if no element found.

singleOrNull

public static <T> T singleOrNull(Iterator<T> iterator)
Returns the given iterator's single element or null if no element found. If there is more than one element in the iterator a NoSuchElementException will be thrown.

Type Parameters:
T - the type of elements in iterator.
Parameters:
iterator - the Iterator to get elements from.
Returns:
the single element in iterator, or null if no element found.
Throws:
{@link - NoSuchElementException} if more than one element was found.

single

public static <T> T single(Iterator<T> iterator)
Returns the given iterator's single element. If there are no elements or more than one element in the iterator a NoSuchElementException will be thrown.

Type Parameters:
T - the type of elements in iterator.
Parameters:
iterator - the Iterator to get elements from.
Returns:
the single element in the iterator.
Throws:
{@link - NoSuchElementException} if there isn't exactly one element.

fromEnd

public static <T> T fromEnd(Iterator<T> iterator,
                            int n)
Returns the iterator's n:th item from the end of the iteration. If the iterator has got less than n-1 items in it NoSuchElementException is thrown.

Type Parameters:
T - the type of elements in iterator.
Parameters:
iterator - the Iterator to get elements from.
n - the n:th item from the end to get.
Returns:
the iterator's n:th item from the end of the iteration.
Throws:
NoSuchElementException - if the iterator contains less than n-1 items.

fromEndOrNull

public static <T> T fromEndOrNull(Iterator<T> iterator,
                                  int n)
Returns the iterator's n:th item from the end of the iteration. If the iterator has got less than n-1 items in it null is returned.

Type Parameters:
T - the type of elements in iterator.
Parameters:
iterator - the Iterator to get elements from.
n - the n:th item from the end to get.
Returns:
the iterator's n:th item from the end of the iteration, or null if the iterator doesn't contain that many items.

firstOrNull

public static <T> T firstOrNull(Iterable<T> iterable)
Returns the given iterable's first element or null if no element found.

Type Parameters:
T - the type of elements in iterable.
Parameters:
iterable - the Iterable to get elements from.
Returns:
the first element in the iterable, or null if no element found.

first

public static <T> T first(Iterable<T> iterable)
Returns the given iterable's first element. If no element is found a NoSuchElementException is thrown.

Type Parameters:
T - the type of elements in iterable.
Parameters:
iterable - the Iterable to get elements from.
Returns:
the first element in the iterable.
Throws:
{@link - NoSuchElementException} if no element found.

lastOrNull

public static <T> T lastOrNull(Iterable<T> iterable)
Returns the given iterable's last element or null if no element found.

Type Parameters:
T - the type of elements in iterable.
Parameters:
iterable - the Iterable to get elements from.
Returns:
the last element in the iterable, or null if no element found.

last

public static <T> T last(Iterable<T> iterable)
Returns the given iterable's last element. If no element is found a NoSuchElementException is thrown.

Type Parameters:
T - the type of elements in iterable.
Parameters:
iterable - the Iterable to get elements from.
Returns:
the last element in the iterable.
Throws:
{@link - NoSuchElementException} if no element found.

singleOrNull

public static <T> T singleOrNull(Iterable<T> iterable)
Returns the given iterable's single element or null if no element found. If there is more than one element in the iterable a NoSuchElementException will be thrown.

Type Parameters:
T - the type of elements in iterable.
Parameters:
iterable - the Iterable to get elements from.
Returns:
the single element in iterable, or null if no element found.
Throws:
{@link - NoSuchElementException} if more than one element was found.

single

public static <T> T single(Iterable<T> iterable)
Returns the given iterable's single element. If there are no elements or more than one element in the iterable a NoSuchElementException will be thrown.

Type Parameters:
T - the type of elements in iterable.
Parameters:
iterable - the Iterable to get elements from.
Returns:
the single element in the iterable.
Throws:
{@link - NoSuchElementException} if there isn't exactly one element.

fromEndOrNull

public static <T> T fromEndOrNull(Iterable<T> iterable,
                                  int countFromEnd)
Returns the iterator's n:th item from the end of the iteration. If the iterator has got less than n-1 items in it null is returned.

Type Parameters:
T - the type of elements in iterator.
Parameters:
iterable - the Iterable to get elements from.
countFromEnd - the n:th item from the end to get.
Returns:
the iterator's n:th item from the end of the iteration, or null if the iterator doesn't contain that many items.

fromEnd

public static <T> T fromEnd(Iterable<T> iterable,
                            int n)
Returns the iterator's n:th item from the end of the iteration. If the iterator has got less than n-1 items in it NoSuchElementException is thrown.

Type Parameters:
T - the type of elements in iterator.
Parameters:
iterable - the Iterable to get elements from.
n - the n:th item from the end to get.
Returns:
the iterator's n:th item from the end of the iteration.
Throws:
NoSuchElementException - if the iterator contains less than n-1 items.

addToCollection

public static <C extends Collection<T>,T> C addToCollection(Iterator<T> iterator,
                                                            C collection)
Adds all the items in iterator to collection.

Type Parameters:
C - the type of Collection to add to items to.
T - the type of items in the collection and iterator.
Parameters:
iterator - the Iterator to grab the items from.
collection - the Collection to add the items to.
Returns:
the collection which was passed in, now filled with the items from iterator.

addToCollection

public static <C extends Collection<T>,T> C addToCollection(Iterable<T> iterable,
                                                            C collection)
Adds all the items in iterator to collection.

Type Parameters:
C - the type of Collection to add to items to.
T - the type of items in the collection and iterator.
Parameters:
iterable - the Iterator to grab the items from.
collection - the Collection to add the items to.
Returns:
the collection which was passed in, now filled with the items from iterator.

loop

public static <T> Iterable<T> loop(Iterator<T> iterator)
Convenience method for looping over an Iterator. Converts the Iterator to an Iterable by wrapping it in an Iterable that returns the Iterator. It breaks the contract of Iterable in that it returns the supplied iterator instance for each call to iterator() on the returned Iterable instance. This method exists to make it easy to use an Iterator in a for-loop.

Type Parameters:
T - the type of items in the iterator.
Parameters:
iterator - the iterator to expose as an Iterable.
Returns:
the supplied iterator posing as an Iterable.

asIterable

public static <T> Iterable<T> asIterable(Iterator<T> iterator)
Exposes iterator as an Iterable. It breaks the contract of Iterable in that it returns the supplied iterator instance for each call to iterator() on the returned Iterable instance. This method mostly exists to make it easy to use an Iterator in a for-loop.

Type Parameters:
T - the type of items in the iterator.
Parameters:
iterator - the iterator to expose as an Iterable.
Returns:
the supplied iterator posing as an Iterable.

count

public static <T> int count(Iterator<T> iterator)
Counts the number of items in the iterator by looping through it.

Type Parameters:
T - the type of items in the iterator.
Parameters:
iterator - the Iterator to count items in.
Returns:
the number of found in iterator.

count

public static <T> int count(Iterable<T> iterable)
Counts the number of items in the iterable by looping through it.

Type Parameters:
T - the type of items in the iterator.
Parameters:
iterable - the Iterable to count items in.
Returns:
the number of found in iterator.

asCollection

public static <T> Collection<T> asCollection(Iterable<T> iterable)
Creates a collection from an iterable.

Type Parameters:
T - The generic type of both the iterable and the collection.
Parameters:
iterable - The iterable to create the collection from.
Returns:
a collection containing all items from the iterable.

asIterable

public static ClosableIterable<String> asIterable(File file)
Creates an Iterable for iterating over the lines of a text file.

Parameters:
file - the file to get the lines for.
Returns:
an Iterable for iterating over the lines of a text file.

asIterator

public static ClosableIterator<String> asIterator(File file)
                                           throws IOException
Creates an Iterator for iterating over the lines of a text file. The opened file is closed if an exception occurs during reading or when the files has been read through all the way.

Parameters:
file - the file to get the lines for.
Returns:
an Iterator for iterating over the lines of a text file.
Throws:
IOException

streamToFile

public static <T> void streamToFile(Iterable<T> iterable,
                                    File file)
                         throws IOException
Throws:
IOException

streamToFile

public static <T> void streamToFile(Iterator<T> iterator,
                                    File file)
                         throws IOException
Throws:
IOException

Neo4j Community

Copyright © 2002-2012 The Neo4j Graph Database Project. All Rights Reserved.