org.neo4j.helpers.collection.Iterables Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of neo4j-kernel Show documentation
Show all versions of neo4j-kernel Show documentation
Neo4j kernel is a lightweight, embedded Java database designed to
store data structured as graphs rather than tables. For more
information, see http://neo4j.org.
/*
* Copyright (c) 2002-2015 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package org.neo4j.helpers.collection;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;
import org.neo4j.graphdb.ResourceIterable;
import org.neo4j.graphdb.ResourceIterator;
import org.neo4j.function.Function;
import org.neo4j.helpers.Predicate;
import static java.util.Arrays.asList;
import static org.neo4j.helpers.collection.IteratorUtil.asResourceIterator;
/**
* TODO: Combine this and {@link IteratorUtil} into one class
*/
public final class Iterables
{
private static Iterable EMPTY = new Iterable()
{
Iterator iterator = new Iterator()
{
@Override
public boolean hasNext()
{
return false;
}
@Override
public Object next()
{
throw new NoSuchElementException();
}
@Override
public void remove()
{
}
};
@Override
public Iterator iterator()
{
return iterator;
}
};
@SuppressWarnings("unchecked")
public static Iterable empty()
{
return EMPTY;
}
public static Iterable limit( final int limitItems, final Iterable iterable )
{
return new Iterable()
{
@Override
public Iterator iterator()
{
final Iterator iterator = iterable.iterator();
return new Iterator()
{
int count;
@Override
public boolean hasNext()
{
return count < limitItems && iterator.hasNext();
}
@Override
public T next()
{
count++;
return iterator.next();
}
@Override
public void remove()
{
iterator.remove();
}
};
}
};
}
public static Function, Iterable> limit( final int limitItems )
{
return new Function, Iterable>()
{
@Override
public Iterable apply( Iterable ts )
{
return limit( limitItems, ts );
}
};
}
public static Iterable unique( final Iterable iterable )
{
return new Iterable()
{
@Override
public Iterator iterator()
{
final Iterator iterator = iterable.iterator();
return new Iterator()
{
Set items = new HashSet<>();
T nextItem;
@Override
public boolean hasNext()
{
while ( iterator.hasNext() )
{
nextItem = iterator.next();
if ( items.add( nextItem ) )
{
return true;
}
}
return false;
}
@Override
public T next()
{
if ( nextItem == null && !hasNext() )
{
throw new NoSuchElementException();
}
return nextItem;
}
@Override
public void remove()
{
}
};
}
};
}
public static > C addAll( C collection, Iterable extends T> iterable )
{
Iterator extends T> iterator = iterable.iterator();
try
{
while (iterator.hasNext())
{
collection.add( iterator.next() );
}
}
finally
{
if (iterator instanceof AutoCloseable)
{
try
{
((AutoCloseable)iterator).close();
}
catch ( Exception e )
{
// Ignore
}
}
}
return collection;
}
public static long count( Iterable> iterable )
{
long c = 0;
for ( Iterator> iterator = iterable.iterator(); iterator.hasNext(); iterator.next() )
{
c++;
}
return c;
}
public static Iterable filter( Predicate super X> specification, Iterable i )
{
return new FilterIterable<>( i, specification );
}
public static Iterator filter( Predicate super X> specification, Iterator i )
{
return new FilterIterable.FilterIterator<>( i, specification );
}
public static X first( Iterable extends X> i )
{
Iterator extends X> iter = i.iterator();
if ( iter.hasNext() )
{
return iter.next();
}
else
{
return null;
}
}
public static X single( Iterable extends X> i )
{
return IteratorUtil.single( i );
}
public static Iterable skip( final int skip, final Iterable iterable )
{
return new Iterable()
{
@Override
public Iterator iterator()
{
Iterator iterator = iterable.iterator();
for ( int i = 0; i < skip; i++ )
{
if ( iterator.hasNext() )
{
iterator.next();
}
else
{
return Iterables.empty().iterator();
}
}
return iterator;
}
};
}
public static X last( Iterable extends X> i )
{
Iterator extends X> iter = i.iterator();
X item = null;
while ( iter.hasNext() )
{
item = iter.next();
}
return item;
}
public static Iterable reverse( Iterable iterable )
{
List list = toList( iterable );
Collections.reverse( list );
return list;
}
@SafeVarargs
public static > Iterable flatten( I... multiIterator )
{
return new FlattenIterable<>( asList(multiIterator) );
}
@SafeVarargs
public static Iterable mix( final Iterable... iterables )
{
return new Iterable()
{
@Override
public Iterator iterator()
{
final Iterable> iterators = toList( map( new Function, Iterator>()
{
@Override
public Iterator apply( Iterable iterable )
{
return iterable.iterator();
}
}, asList(iterables) ) );
return new Iterator()
{
Iterator> iterator;
Iterator iter;
@Override
public boolean hasNext()
{
for ( Iterator iterator : iterators )
{
if ( iterator.hasNext() )
{
return true;
}
}
return false;
}
@Override
public T next()
{
if ( iterator == null )
{
iterator = iterators.iterator();
}
while ( iterator.hasNext() )
{
iter = iterator.next();
if ( iter.hasNext() )
{
return iter.next();
}
}
iterator = null;
return next();
}
@Override
public void remove()
{
if ( iter != null )
{
iter.remove();
}
}
};
}
};
}
public static Iterable map( Function super FROM, ? extends TO> function, Iterable from )
{
return new MapIterable<>( from, function );
}
public static Iterator map( Function super FROM, ? extends TO> function, Iterator from )
{
return new MapIterable.MapIterator<>( from, function );
}
public static Iterator flatMap( Function super FROM, ? extends Iterator> function, Iterator from )
{
return new CombiningIterator<>( map(function, from) );
}
@SafeVarargs
@SuppressWarnings("unchecked")
public static Iterable iterable( C... items )
{
return (Iterable) asList(items);
}
@SuppressWarnings("unchecked")
public static Iterable cast( Iterable iterable )
{
return (Iterable) iterable;
}
@SafeVarargs
@SuppressWarnings("unchecked")
public static Iterable concat( Iterable extends T>... iterables )
{
return concat( asList( (Iterable[]) iterables ) );
}
public static Iterable concat( final Iterable> iterables )
{
return new CombiningIterable<>( iterables );
}
@SafeVarargs
@SuppressWarnings("unchecked")
public static Iterator concat( Iterator extends T>... iterables )
{
return concat( Arrays.asList( (Iterator[]) iterables ).iterator() );
}
public static ResourceIterator concatResourceIterators( Iterator> iterators )
{
return new CombiningResourceIterator<>(iterators);
}
public static Iterator concat( Iterator> iterators )
{
return new CombiningIterator<>(iterators);
}
public static Function cast()
{
return new Function()
{
@Override
@SuppressWarnings("unchecked")
public TO apply( FROM from )
{
return (TO) from;
}
};
}
public static Iterable prepend( final C item, final Iterable iterable )
{
return new Iterable()
{
@Override
public Iterator iterator()
{
return new Iterator()
{
T first = item;
Iterator iterator;
@Override
public boolean hasNext()
{
if ( first != null )
{
return true;
}
else
{
if ( iterator == null )
{
iterator = iterable.iterator();
}
}
return iterator.hasNext();
}
@Override
public T next()
{
if ( first != null )
{
try
{
return first;
}
finally
{
first = null;
}
}
else
{
return iterator.next();
}
}
@Override
public void remove()
{
}
};
}
};
}
public static Iterable append( final C item, final Iterable iterable )
{
return new Iterable()
{
@Override
public Iterator iterator()
{
final Iterator iterator = iterable.iterator();
return new Iterator()
{
T last = item;
@Override
public boolean hasNext()
{
return iterator.hasNext() || last != null;
}
@Override
public T next()
{
if ( iterator.hasNext() )
{
return iterator.next();
}
else
{
try
{
return last;
}
finally
{
last = null;
}
}
}
@Override
public void remove()
{
}
};
}
};
}
public static Iterable cache( Iterable iterable )
{
return new CacheIterable<>( iterable );
}
public static List toList( Iterable iterable )
{
return addAll( new ArrayList(), iterable );
}
public static List toList( Iterator iterator)
{
List list = new ArrayList<>( );
while ( iterator.hasNext() )
{
list.add(iterator.next());
}
return list;
}
public static Object[] toArray( Iterable
© 2015 - 2025 Weber Informatics LLC | Privacy Policy