Package io.datarouter.scanner
Interface Scanner<T>
- All Superinterfaces:
AutoCloseable,Closeable
- All Known Implementing Classes:
AdvanceUntilScanner,AdvanceWhileScanner,ArrayScanner,BaseLinkedScanner,BaseScanner,BatchingScanner,CollatingScanner,ComparableScanner,ConcatenatingScanner,DeduplicatingConsecutiveScanner,DistinctScanner,EachScanner,EmptyScanner,FilteringScanner,GeneratingScanner,IteratingScanner,IteratorScanner,LimitingScanner,MappingScanner,NaturalSortingScanner,ObjectScanner,PagingScanner,ParallelMappingScanner,PrefetchingScanner,RandomAccessScanner,RetainingScanner,ReverseListScanner,SamplingScanner,ShufflingScanner,SortingScanner,SplittingScanner,StreamScanner
A form of iterator that operates as lazily as possible, not knowing if the next item is available until advancing and
dropping the reference to the previous item. Default interface methods are included so Scanners can be assembled into
a pipeline before advancing through the items.
Similar to Iterator or Stream, a Scanner can only be consumed once.
-
Method Summary
Modifier and TypeMethodDescriptionbooleanadvance()Try to update current to the next item, if there is one.advanceUntil(Predicate<? super T> predicate) Stop the scanner when the predicate matches, excluding the item that caused it to stop.advanceWhile(Predicate<? super T> predicate) Stop the scanner when the predicated fails to match, excluding the item that caused it to stop.default booleanApply the Predicate to every item in the Scanner, returning whether they all match.default booleanApply the predicate to each item in the Scanner until one matches, returning true, otherwise return false if the Scanner is consumed with no matches.Concats the provided Scanner after the current Scanner.Concats the provided Iterable items after the current Scanner.Concats the provided array items after the current Scanner.default <R> RBeta: Apply the provided Function which returns another Scanner.batch(int batchSize) default voidclose()Override to cleanup any resources.default <R> Scanner<R>Similar to the merge phase of a merge sort, assuming the input Scanners are sorted.default <R> Scanner<R>collate(Function<? super T, Scanner<R>> mapper, Comparator<? super R> comparator) Similar to the merge phase of a merge sort, assuming the input Scanners are sorted.default <C extends Collection<T>>
Cdefault <R,A> R static <T> Scanner<T>Combine the items from multiple Scanners into a single Scanner.static <T> Scanner<T>Combine the items from multiple Iterables into a single Scanner.default <R> Scanner<R>Combine the items from multiple Scanners into a single Scanner.default <R> Scanner<R>concatIter(Function<? super T, Iterable<R>> mapToIterable) Combine the items from multiple Iterables into a single Scanner.default longcount()Advance through every item in the Scanner, returning the count of items seen.current()Skips consecutive duplicates.deduplicateConsecutiveBy(Function<T, ?> mapper) Skips items where the mapper outputs the same value as the previous item as compared with Objects::equals.deduplicateConsecutiveBy(Function<T, R> mapper, BiPredicate<R, R> equalsPredicate) Skips items where the mapper outputs the same value as the previous item when compared with the supplied equalsPredicate.distinct()distinctBy(Function<T, ?> mapper) Calls Consumer::accept on each item.static <T> Scanner<T>empty()Skips items where the Predicate returns true.Return the first item encountered in the Scanner, wrapped in an Optional, otherwise Optional.empty() if no items were found.findLast()Advance through every item in the Scanner, returning the last item wrapped in an Optional, otherwise Optional.empty() if the Scanner had no items.findMax(Comparator<? super T> comparator) Advance through all items, retaining the maximum as computed by the Comparator and returning it.findMin(Comparator<? super T> comparator) Advance through all items, retaining the minimum as computed by the Comparator and returning it.default voidPerform an operation on each item.static <T> Scanner<T>default <K,V, C extends Collection<V>, M extends Map<K, C>>
MgroupBy(Function<T, K> keyFunction, Function<T, V> valueFunction, Supplier<M> mapSupplier, Supplier<C> collectionSupplier) default booleanhasAny()Test whether the first advance() returns true.Skips items where the Predicate returns false.default booleanisEmpty()Test whether the first advance() returns false.iterable()static <T> Scanner<T>iterate(T seed, UnaryOperator<T> unaryOperator) Generate a sequence where each item is calculated off the one before it.iterator()limit(long limit) Ends the Scanner when the limit has been reached.default <R> Scanner<R>A caller can extend BaseLinkedScanner, which has exception handling logic, and fluently include it in the Scanner pipeline.list()default <R> Rdefault <R> Scanner<R>For each input item, outputs the result of Function::apply.maxN(Comparator<? super T> comparator, int num) Retains the max N items as defined by the comparator and returns a Scanner of them in descending order.minN(Comparator<? super T> comparator, int num) Retains the min N items as defined by the comparator and returns a Scanner of them in ascending order.default booleanReturn false as soon as the Predicate passes, otherwise true if all items fail the Predicate.static <T> Scanner<T>Create a Scanner of items in the Iterable.static <T> Scanner<T>Create a Scanner of items in the Iterator.static <T> Scanner<T>Create a Scanner of items in the Stream.static <T> Scanner<T>of(T object) Convert an Object into a Scanner.static <T> Scanner<T>of(T... array) Create a Scanner of items in the array.static <T> Scanner<T>ofNullable(T object) Convert an Object into a Scanner if non-null.default ParallelScanner<T>parallel(ParallelScannerContext context) prefetch(ExecutorService exec, int batchSize) reduce(BinaryOperator<T> reducer) default Treduce(T seed, BinaryOperator<T> reducer) default Scanner<RetainingGroup<T>>retain(int retaining) For retaining a window of N previous items.reverse()Collect all items into an List and return a Scanner that iterates through them backwards.sample(long sampleSize, boolean includeLast) Return every Nth item.shuffle()skip(long numToSkip) Skip the leading items from the Scanner.sort()sort(Comparator<? super T> comparator) Applies the Function to each item in the Scanner, returning a new Scanner each time the mapped value changes.stream()default DoubleStreamstreamDoubles(ToDoubleFunction<? super T> mapper) default IntStreamstreamInts(ToIntFunction<? super T> mapper) default LongStreamstreamLongs(ToLongFunction<? super T> mapper) take(int numToTake) Consume up to N items without closing, only closing the scanner if the last item was consumed.default voidBeta: Pass the current Scanner to a method that will Consume this Scanner and return nothing.default Object[]toArray()toMap()default <K,V> Map<K, V> default <K,V> Map<K, V> default <K,V> Map<K, V> default <K,V, M extends Map<K, V>>
MtoMapSupplied(Function<T, K> keyFunction, Function<T, V> valueFunction, ScannerToMap.Replace replacePolicy, Supplier<M> mapSupplier) default <K,V, M extends Map<K, V>>
MtoMapSupplied(Function<T, K> keyFunction, Function<T, V> valueFunction, BinaryOperator<V> mergeFunction, Supplier<M> mapSupplier) default <K,V, M extends Map<K, V>>
MtoMapSupplied(Function<T, K> keyFunction, Supplier<M> mapSupplier)
-
Method Details
-
advance
boolean advance()Try to update current to the next item, if there is one.- Returns:
- True if it advanced
-
current
T current()- Returns:
- The current item, only valid if advance() returned true
-
close
default void close()Override to cleanup any resources. The call should be propagated to all parent scanners. In the infrequent case of calling advance/current in an application, the Scanner should be explicitly closed. Because the included Scanner operations will close themselves when they fail to advance, it frequently a no-op for the application, unless closing the scanner before it stops advancing.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
forEach
Perform an operation on each item.- Parameters:
action- Consumer::accept is performed on each item
-
take
Consume up to N items without closing, only closing the scanner if the last item was consumed.- Parameters:
numToTake- Maximum returned items- Returns:
- List with up to numToTake items
-
empty
- Returns:
- A scanner that immediately returns advance=false
-
generate
- Parameters:
supplier- Supplier that generates items indefinitely- Returns:
- A scanner that advances through the items
-
iterate
Generate a sequence where each item is calculated off the one before it.- Parameters:
seed- The first itemunaryOperator- A function applied to the current item to generate the next item- Returns:
- A scanner that advances through the items
-
ofNullable
Convert an Object into a Scanner if non-null.- Parameters:
object- A nullable Object- Returns:
- An empty scanner if the Object was null, otherwise a single-item Scanner with the Object
-
of
Convert an Object into a Scanner.- Parameters:
object- A non-null Object- Returns:
- A single-item Scanner with the Object
-
of
Create a Scanner of items in the array.- Parameters:
array- An array or var-args- Returns:
- A Scanner that visits each item in the array
-
of
Create a Scanner of items in the Iterator.- Parameters:
iterator- An Iterator- Returns:
- A Scanner that visits each item in the Iterator
-
of
Create a Scanner of items in the Iterable.- Parameters:
iterable- An Iterable, which includes any Collection- Returns:
- A Scanner that visits each item in the Iterable
-
of
Create a Scanner of items in the Stream.- Parameters:
stream- A Stream- Returns:
- A Scanner that visits each item in the Stream
-
concatIter
Combine the items from multiple Iterables into a single Scanner. Use Function.identity() if they're already Iterables.- Parameters:
mapToIterable- Converts the input items into the Iterables to be combined- Returns:
- Scanner containing the items from all input Iterables, starting with the first
-
concat
Combine the items from multiple Scanners into a single Scanner. Use Function.identity() if they're already Scanners.- Parameters:
mapToScanner- Converts the input items into the Scanners to be combined- Returns:
- Scanner containing the items from all input Scanners, starting with the first
-
concat
Combine the items from multiple Scanners into a single Scanner.- Parameters:
scanners- Input Scanners to be combined- Returns:
- Scanner containing the items from all input Scanners, starting with the first
-
concat
Combine the items from multiple Iterables into a single Scanner.- Parameters:
iterables- Input Iterables to be combined, where Iterable includes any Collection- Returns:
- Scanner containing the items from all input Iterables, starting with the first
-
append
Concats the provided Scanner after the current Scanner. -
append
Concats the provided array items after the current Scanner. -
append
Concats the provided Iterable items after the current Scanner. -
collate
Similar to the merge phase of a merge sort, assuming the input Scanners are sorted. Converts the input items to Scanners and feeds all Scanners through a PriorityQueue with "natural" comparator ordering.- Returns:
- Assuming input scanners are sorted, a single Scanner of all items in sorted order.
-
collate
default <R> Scanner<R> collate(Function<? super T, Scanner<R>> mapper, Comparator<? super R> comparator) Similar to the merge phase of a merge sort, assuming the input Scanners are sorted. Converts the input items to Scanners and feeds all Scanners through a PriorityQueue with the provided comparator.- Returns:
- Assuming input scanners are sorted according to the comparator, a single Scanner of all items in sorted order.
-
link
A caller can extend BaseLinkedScanner, which has exception handling logic, and fluently include it in the Scanner pipeline.- Parameters:
scannerBuilder- Function to build the BaseLinkedScanner
-
apply
Beta: Apply the provided Function which returns another Scanner. The other Scanner is now responsible for consuming the Scanner, or returning a continued Scanner.- Parameters:
function- A method reference that accepts this Scanner
-
then
Beta: Pass the current Scanner to a method that will Consume this Scanner and return nothing.- Parameters:
consumer- A method reference that accepts this Scanner
-
parallel
-
prefetch
-
advanceUntil
Stop the scanner when the predicate matches, excluding the item that caused it to stop. -
advanceWhile
Stop the scanner when the predicated fails to match, excluding the item that caused it to stop. -
batch
-
deduplicateConsecutive
Skips consecutive duplicates. Lighter weight than distinct() because all elements need not be collected into memory. -
deduplicateConsecutiveBy
default <R> Scanner<T> deduplicateConsecutiveBy(Function<T, R> mapper, BiPredicate<R, R> equalsPredicate) Skips items where the mapper outputs the same value as the previous item when compared with the supplied equalsPredicate. -
deduplicateConsecutiveBy
Skips items where the mapper outputs the same value as the previous item as compared with Objects::equals. -
each
Calls Consumer::accept on each item. -
exclude
Skips items where the Predicate returns true. -
include
Skips items where the Predicate returns false. -
limit
Ends the Scanner when the limit has been reached. -
map
For each input item, outputs the result of Function::apply. -
retain
For retaining a window of N previous items.- Parameters:
retaining- The number of extra items to retain, in addition to the Scanner's default of zero.- Returns:
- A RetainingGroup allowing you to call peekBack(1), to get the previous current() value. Calling peekBack with a value higher than the "retaining" param will cause an exception.
-
sample
Return every Nth item.- Parameters:
sampleSize- A Scanner with 8 items and sample size 8 will return either 2 or 3 results.includeLast- Whether to include the last item in case of a partial sample.- Returns:
- A Scanner of the sampled items.
-
skip
Skip the leading items from the Scanner.- Parameters:
numToSkip- Skips up to this many items, or fewer if the Scanner had fewer items.- Returns:
- A Scanner with the remaining items, or an empty Scanner if all items were dropped.
-
splitBy
Applies the Function to each item in the Scanner, returning a new Scanner each time the mapped value changes. Similar to groupBy on an unbounded amount of data, but will result in multiple of the same groupings depending on the order of the input data. Useful in the case of Scanning child objects and grouping by a parent. -
allMatch
Apply the Predicate to every item in the Scanner, returning whether they all match. -
anyMatch
Apply the predicate to each item in the Scanner until one matches, returning true, otherwise return false if the Scanner is consumed with no matches. -
count
default long count()Advance through every item in the Scanner, returning the count of items seen. -
findFirst
Return the first item encountered in the Scanner, wrapped in an Optional, otherwise Optional.empty() if no items were found. -
findLast
Advance through every item in the Scanner, returning the last item wrapped in an Optional, otherwise Optional.empty() if the Scanner had no items. -
findMax
Advance through all items, retaining the maximum as computed by the Comparator and returning it. -
findMin
Advance through all items, retaining the minimum as computed by the Comparator and returning it. -
hasAny
default boolean hasAny()Test whether the first advance() returns true. -
isEmpty
default boolean isEmpty()Test whether the first advance() returns false. -
noneMatch
Return false as soon as the Predicate passes, otherwise true if all items fail the Predicate. -
reduce
-
reduce
-
collect
-
collect
-
distinct
-
distinctBy
-
flush
-
list
-
listTo
-
maxN
Retains the max N items as defined by the comparator and returns a Scanner of them in descending order. -
minN
Retains the min N items as defined by the comparator and returns a Scanner of them in ascending order. -
reverse
Collect all items into an List and return a Scanner that iterates through them backwards. -
shuffle
-
sort
-
sort
-
toArray
-
toMap
-
toMap
-
toMap
-
toMap
-
toMap
-
toMapSupplied
-
toMapSupplied
-
toMapSupplied
-
toMapSupplied
-
groupBy
-
groupBy
-
groupBy
-
groupBy
-
iterator
-
iterable
-
stream
-
streamInts
-
streamLongs
-
streamDoubles
-