Added New Iterable Feature
-Added: Repeating iterable/iterator/async feature which allows to repeate collections a desired amount of times. -Added: Tests for it.
This commit is contained in:
@@ -147,6 +147,28 @@ public class ITERABLES
|
||||
return new DistinctIterableBRACES(wrap(iterable));
|
||||
}
|
||||
|
||||
/**
|
||||
* A Helper function that repeats the Iterable a specific amount of times
|
||||
* @param iterable that should be repeated
|
||||
* @param repeats the amount of times the iterable should be repeated
|
||||
* @Type(T)
|
||||
* @return a repeating iterable
|
||||
*/
|
||||
public static GENERIC_KEY_BRACES ITERABLE KEY_GENERIC_TYPE repeat(ITERABLE KEY_GENERIC_TYPE iterable, int repeats) {
|
||||
return new RepeatingIterableBRACES(iterable, repeats);
|
||||
}
|
||||
|
||||
/**
|
||||
* A Helper function that repeats the Iterable a specific amount of times from a Java Iterable
|
||||
* @param iterable that should be repeated
|
||||
* @param repeats the amount of times the iterable should be repeated
|
||||
* @Type(T)
|
||||
* @return a repeating iterable
|
||||
*/
|
||||
public static GENERIC_KEY_BRACES ITERABLE KEY_GENERIC_TYPE repeat(Iterable<? extends CLASS_TYPE> iterable, int repeats) {
|
||||
return new RepeatingIterableBRACES(wrap(iterable), repeats);
|
||||
}
|
||||
|
||||
/**
|
||||
* A Helper function that hard limits the Iterable to a specific size
|
||||
* @param iterable that should be limited
|
||||
@@ -320,6 +342,42 @@ public class ITERABLES
|
||||
}
|
||||
}
|
||||
|
||||
private static class RepeatingIterable KEY_GENERIC_TYPE implements ITERABLE KEY_GENERIC_TYPE
|
||||
{
|
||||
ITERABLE KEY_GENERIC_TYPE iterable;
|
||||
int repeats;
|
||||
|
||||
public RepeatingIterable(ITERABLE KEY_GENERIC_TYPE iterable, int repeats) {
|
||||
this.iterable = iterable;
|
||||
this.repeats = repeats;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ITERATOR KEY_GENERIC_TYPE iterator() {
|
||||
return ITERATORS.repeat(iterable.iterator(), repeats);
|
||||
}
|
||||
|
||||
#if !TYPE_OBJECT
|
||||
@Override
|
||||
public void forEach(CONSUMER action) {
|
||||
Objects.requireNonNull(action);
|
||||
LIST KEY_GENERIC_TYPE list = new ARRAY_LISTBRACES();
|
||||
iterable.forEach(action.andThen(list::add));
|
||||
for(int i = 0;i<repeats;i++)
|
||||
list.forEach(action);
|
||||
}
|
||||
#else
|
||||
@Override
|
||||
public void forEach(Consumer<? super CLASS_TYPE> action) {
|
||||
Objects.requireNonNull(action);
|
||||
LIST KEY_GENERIC_TYPE list = new ARRAY_LISTBRACES();
|
||||
iterable.forEach(T -> {action.accept(T); list.add(T);});
|
||||
for(int i = 0;i<repeats;i++)
|
||||
list.forEach(action);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
private static class FilteredIterable KEY_GENERIC_TYPE implements ITERABLE KEY_GENERIC_TYPE
|
||||
{
|
||||
ITERABLE KEY_GENERIC_TYPE iterable;
|
||||
|
||||
Reference in New Issue
Block a user