-
Notifications
You must be signed in to change notification settings - Fork 629
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ISPN-9242 Address refactorings brought up in segmented data container
review * Making all DataContainer instances segmented * Update all references internally to use SegmentedDataContainer * Removed SSC#UNKNOWN_SEGMENT * CommandFactory require segment for commands * Rename SegmentedDataContainer to InternalDataContainer
- Loading branch information
1 parent
3cee685
commit 55b67fa
Showing
106 changed files
with
1,433 additions
and
1,010 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
commons/src/main/java/org/infinispan/commons/util/FilterSpliterator.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,81 @@ | |||
package org.infinispan.commons.util; | |||
|
|||
import java.util.Spliterator; | |||
import java.util.function.Consumer; | |||
import java.util.function.Predicate; | |||
|
|||
/** | |||
* Spliterator that only returns entries that pass the given predicate. This spliterator will inherit all of the | |||
* characteristics of the underlying spliterator, except that it won't return {@link Spliterator#SIZED} or | |||
* {@link Spliterator#SUBSIZED}. | |||
* <p> | |||
* The {@link #forEachRemaining(Consumer)} method should provide better performance than calling | |||
* {@link #tryAdvance(Consumer)} until it returns false. This is due to having to capture the argument before testing | |||
* it and finally invoking the provided {@link Consumer}. | |||
* @author wburns | |||
* @since 9.3 | |||
*/ | |||
public class FilterSpliterator<T> implements CloseableSpliterator<T> { | |||
private final Spliterator<T> spliterator; | |||
private final Predicate<? super T> predicate; | |||
|
|||
// We assume that spliterator is not used concurrently - normally it is split so we can use these variables safely | |||
private final Consumer<? super T> consumer = t -> current = t; | |||
|
|||
private T current; | |||
|
|||
public FilterSpliterator(Spliterator<T> spliterator, Predicate<? super T> predicate) { | |||
this.spliterator = spliterator; | |||
this.predicate = predicate; | |||
} | |||
|
|||
@Override | |||
public void close() { | |||
if (spliterator instanceof CloseableSpliterator) { | |||
((CloseableSpliterator) spliterator).close(); | |||
} | |||
} | |||
|
|||
@Override | |||
public boolean tryAdvance(Consumer<? super T> action) { | |||
while (spliterator.tryAdvance(consumer)) { | |||
T objectToUse = current; | |||
// If object passes then accept it and return | |||
if (predicate.test(objectToUse)) { | |||
action.accept(objectToUse); | |||
return true; | |||
} | |||
} | |||
|
|||
return false; | |||
} | |||
|
|||
@Override | |||
public void forEachRemaining(Consumer<? super T> action) { | |||
spliterator.forEachRemaining(e -> { | |||
if (predicate.test(e)) { | |||
action.accept(e); | |||
} | |||
}); | |||
} | |||
|
|||
@Override | |||
public Spliterator<T> trySplit() { | |||
Spliterator<T> split = spliterator.trySplit(); | |||
if (split != null) { | |||
return new FilterSpliterator<>(split, predicate); | |||
} | |||
return null; | |||
} | |||
|
|||
@Override | |||
public long estimateSize() { | |||
return spliterator.estimateSize(); | |||
} | |||
|
|||
@Override | |||
public int characteristics() { | |||
// Unset the SIZED and SUBSIZED as we don't have an exact amount | |||
return spliterator.characteristics() & ~(Spliterator.SIZED | Spliterator.SUBSIZED); | |||
} | |||
} |
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
Oops, something went wrong.