public class BasicLoop<T extends BasicLoop.Item<T>> extends java.lang.Object implements Countable<T>
This basic implementation allows to use user-defined double linked items
based on BasicLoop.Item
because it makes some algorithms easier
to add behavior to the items themselves. For loop with items holding
generic values see Loop
.
This class is not thread-safe.
Modifier and Type | Class and Description |
---|---|
static class |
BasicLoop.Item<V extends BasicLoop.Item<V>>
Base implementation of a double linked loop item.
|
Countable.Base<TT>
Constructor and Description |
---|
BasicLoop()
Create an empty loop.
|
BasicLoop(java.util.function.Function<? super V,Function3<T,BasicLoop<T>,T,T>> creatorCreator,
V... elements)
Create a loop from an array.
|
BasicLoop(java.lang.Iterable<V> elements,
java.util.function.Function<? super V,Function3<T,BasicLoop<T>,T,T>> creatorCreator)
Create a loop from an iterable.
|
Modifier and Type | Method and Description |
---|---|
T |
add(Function3<T,BasicLoop<T>,T,T> creator)
Add an item.
|
<V> void |
addAll(java.lang.Iterable<V> elements,
java.util.function.Function<? super V,Function3<T,BasicLoop<T>,T,T>> creatorCreator)
Add all the elements to the loop.
|
void |
clear()
Clear this loop.
|
Countable<T> |
closedItemView()
View this loop's items as a countable which is "closed".
|
T |
findFirst(java.util.function.Predicate<? super T> condition)
Find the first item in this loop which fulfills a condition.
|
T |
findLast(java.util.function.Predicate<? super T> condition)
Find the first item in this loop which fulfills a condition.
|
T |
getFirstItem()
Get the first item in the loop.
|
T |
getFirstItemOrNull()
Get the first item in the loop, or
null if the loop is empty. |
T |
getLastItem()
Get the last item in the loop.
|
T |
getLastItemOrNull()
Get the last item in the loop, or
null if the loop is empty. |
boolean |
isEmpty()
Is this countable empty?
|
java.util.Iterator<T> |
iterator()
Get an iterator over the items of this loop.
|
void |
removeSuccessiveDuplicates()
Remove duplicate items which are neighbors.
|
void |
removeSuccessiveDuplicates(java.util.function.BiPredicate<? super T,? super T> equality)
Remove duplicate items which are neighbors.
|
void |
rotate(int steps)
Rotate through the loop so the first item is exchanged
|
boolean |
rotateBackwardUntil(java.util.function.Predicate<? super T> condition)
Rotate backward until a condition is fulfilled for the first item.
|
boolean |
rotateForwardUntil(java.util.function.Predicate<? super T> condition)
Rotate forward until a condition is fulfilled for the first item.
|
int |
size()
Get the size.
|
java.lang.String |
toMultiLineString()
Create a multi-line string from this loop.
|
java.lang.String |
toString() |
java.lang.String |
toString(java.lang.String connect)
Create a string containing all items, connect with the given connector string.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
addAllTo, addToArray, asBase, asCollection, combined, combined, containsEq, containsRef, downCast, empty, equal, equals, filtered, filteredToIndexable, filterToCountable, first, firstOrNull, foldLeft, forEachFragile, freeze, fromOptional, frozen, frozen, groupingBy, groupingBy, hasAll, hasAny, isSorted, isStrictlySorted, last, lastOrNull, mappingBy, mappingBy, optFirst, optional, optLast, orderedCombination, singleton, sorted, toArray, toArray, toList, toString, uniform, view, viewArray, viewArray, viewCollection, viewCollection, viewCollectionN, viewCollectionN, viewFragile
public BasicLoop()
public BasicLoop(@NotNull java.lang.Iterable<V> elements, @NotNull java.util.function.Function<? super V,Function3<T,BasicLoop<T>,T,T>> creatorCreator)
V
- element type of the iterableelements
- elements to insert into this loop, in the given ordercreatorCreator
- function which maps the elements to an item creator@SafeVarargs public BasicLoop(@NotNull java.util.function.Function<? super V,Function3<T,BasicLoop<T>,T,T>> creatorCreator, V... elements)
V
- element type of the arraycreatorCreator
- function which maps the array elements to an item creatorelements
- elements to add to this loop@NotNull public T getFirstItem()
EmptyLoopError
- when loop is empty@Nullable public T getFirstItemOrNull()
null
if the loop is empty.null
@NotNull public T getLastItem()
EmptyLoopError
- when loop is empty@Nullable public T getLastItemOrNull()
null
if the loop is empty.null
public void rotate(int steps)
steps
- steps to rotate,public boolean rotateForwardUntil(@NotNull java.util.function.Predicate<? super T> condition)
true
for the first value no rotation occurs.condition
- condition tested on valuetrue
if the condition is now fulfilled for the first
value,
false
if not or if this loop is emptypublic boolean rotateBackwardUntil(@NotNull java.util.function.Predicate<? super T> condition)
true
for the first value no rotation occurs.condition
- condition tested on valuetrue
if the condition is now fulfilled for the first
value,
false
if not or if this loop is empty@Nullable public T findFirst(@NotNull java.util.function.Predicate<? super T> condition)
Use BasicLoop.Item.findNext(Predicate)
on the result to continue searching.
condition
- condition to be checkednull
if no item matches the condition@Nullable public T findLast(@NotNull java.util.function.Predicate<? super T> condition)
Use BasicLoop.Item.findPrevious(Predicate)
on the result to continue searching.
condition
- condition to be checkednull
if no item matches the condition@NotNull public T add(@NotNull Function3<T,BasicLoop<T>,T,T> creator)
creator
- item creator, called with 2nd and 3rd arguments being null
for the initial loop elementpublic <V> void addAll(@NotNull java.lang.Iterable<V> elements, @NotNull java.util.function.Function<? super V,Function3<T,BasicLoop<T>,T,T>> creatorCreator)
V
- incoming value typeelements
- elements to add to this loopcreatorCreator
- function which creates the loop element creators from the incoming elementspublic int size()
Sizeable
public boolean isEmpty()
Countable
Sizeable.size()
will return 0
.
This default implementation just checks for Sizeable.size()
returning 0
isEmpty
in interface Countable<T extends BasicLoop.Item<T>>
true
if this countable is empty, false
otherwise.
Because calculating the size might be expensive sometimes in these cases
implementations should override this method if possible.
public void clear()
@NotNull public java.util.Iterator<T> iterator()
iterator
in interface java.lang.Iterable<T extends BasicLoop.Item<T>>
@NotNull public Countable<T> closedItemView()
public void removeSuccessiveDuplicates(@NotNull java.util.function.BiPredicate<? super T,? super T> equality)
equality
- equality check for items, expected to return true
for duplicatespublic void removeSuccessiveDuplicates()
removeSuccessiveDuplicates(BiPredicate)
with
Objects.deepEquals(Object, Object)
as equality check.public java.lang.String toString()
toString
in class java.lang.Object
@NotNull public java.lang.String toString(@NotNull java.lang.String connect)
connect
- connector stringconnect
@NotNull public java.lang.String toMultiLineString()