-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleanup and brush Iterables and Iterators helpers
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
1 parent
21a4755
commit c4d311b
Showing
341 changed files
with
2,120 additions
and
2,355 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
community/collections/src/main/java/org/neo4j/helpers/collection/FilterIterable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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() | |||
{ | |||
} | |||
} | |||
} |
Oops, something went wrong.