Skip to content

Commit

Permalink
Cleanup and brush Iterables and Iterators helpers
Browse files Browse the repository at this point in the history
Rename IteratorUtils to Iterators.
Move methods that expect/operate on various iterators to Iterators helper
Move methods that expect/operate on various iterables to Iterables helper
  • Loading branch information
MishaDemianenko committed Mar 8, 2016
1 parent 21a4755 commit c4d311b
Show file tree
Hide file tree
Showing 341 changed files with 2,120 additions and 2,355 deletions.
Expand Up @@ -27,6 +27,7 @@
import java.util.Map;

import org.neo4j.bolt.v1.messaging.BoltIOException;
import org.neo4j.bolt.v1.messaging.Neo4jPack;
import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Label;
Expand All @@ -35,7 +36,6 @@
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.helpers.collection.Iterables;
import org.neo4j.kernel.api.exceptions.Status;
import org.neo4j.bolt.v1.messaging.Neo4jPack;

public class ValueNode
extends ValuePropertyContainer
Expand All @@ -48,7 +48,7 @@ public static void pack( Neo4jPack.Packer packer, Node node )
{
packer.packStructHeader( STRUCT_FIELD_COUNT, Neo4jPack.NODE );
packer.pack( node.getId() );
Collection<Label> collectedLabels = Iterables.toList( node.getLabels() );
Collection<Label> collectedLabels = Iterables.asList( node.getLabels() );
packer.packListHeader( collectedLabels.size() );
for ( Label label : collectedLabels )
{
Expand Down
Expand Up @@ -153,7 +153,7 @@ else if ( value.equals( "FAILURE { \"code\": \"Neo.ClientError.Statement.Invalid
(Map<String, Object>) args.get( 1 ) );
break;
case "RECORD":
writer.handleRecordMessage( new ImmutableRecord( Iterables.toArray(
writer.handleRecordMessage( new ImmutableRecord( Iterables.asArray(
Object.class, (List<Object>) args.get( 0 ) ) ) );
break;
case "SUCCESS":
Expand Down
@@ -0,0 +1,115 @@
/*
* Copyright (c) 2002-2016 "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 <http://www.gnu.org/licenses/>.
*/
package org.neo4j.helpers.collection;

import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.function.Predicate;

class FilterIterable<T> implements Iterable<T>
{
private final Iterable<T> iterable;

private final Predicate<? super T> specification;

public FilterIterable( Iterable<T> iterable, Predicate<? super T> specification )
{
this.iterable = iterable;
this.specification = specification;
}

@Override
public Iterator<T> iterator()
{
return new FilterIterator<>( iterable.iterator(), specification );
}

static class FilterIterator<T> implements Iterator<T>
{
private final Iterator<T> iterator;

private final Predicate<? super T> specification;

private T currentValue;
boolean finished = false;
boolean nextConsumed = true;

public FilterIterator( Iterator<T> iterator, Predicate<? super T> specification )
{
this.specification = specification;
this.iterator = iterator;
}

public boolean moveToNextValid()
{
boolean found = false;
while ( !found && iterator.hasNext() )
{
T currentValue = iterator.next();
boolean satisfies = specification.test( currentValue );

if ( satisfies )
{
found = true;
this.currentValue = currentValue;
nextConsumed = false;
}
}
if ( !found )
{
finished = true;
}
return found;
}

@Override
public T next()
{
if ( !nextConsumed )
{
nextConsumed = true;
return currentValue;
}
else
{
if ( !finished )
{
if ( moveToNextValid() )
{
nextConsumed = true;
return currentValue;
}
}
}
throw new NoSuchElementException( "This iterator is exhausted." );
}

@Override
public boolean hasNext()
{
return !finished && (!nextConsumed || moveToNextValid());
}

@Override
public void remove()
{
}
}
}

0 comments on commit c4d311b

Please sign in to comment.