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:
parent
ca33c9eb9e
commit
03b23f0e3c
|
@ -149,6 +149,15 @@ public interface ITERABLE KEY_GENERIC_TYPE extends Iterable<CLASS_TYPE>
|
||||||
return ITERABLES.distinct(this);
|
return ITERABLES.distinct(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper function to reduce the usage of Streams and allows to repeat elements a desired amount of times
|
||||||
|
* @param repeats how many times the elements should be repeated
|
||||||
|
* @return a Iterable that is repeating multiple times
|
||||||
|
*/
|
||||||
|
default ITERABLE KEY_GENERIC_TYPE repeat(int repeats) {
|
||||||
|
return ITERABLES.repeat(this, repeats);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A Helper function to reduce the usage of Streams and allows to limit the amount of elements
|
* A Helper function to reduce the usage of Streams and allows to limit the amount of elements
|
||||||
* @param limit the amount of elements it should be limited to
|
* @param limit the amount of elements it should be limited to
|
||||||
|
|
|
@ -173,6 +173,16 @@ public class ASYNC_BUILDER KEY_GENERIC_TYPE
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Repeats the elements inside of the Iterable
|
||||||
|
* @param repeats the amount of times the elements should be repeated
|
||||||
|
* @return self with a repeater applied
|
||||||
|
*/
|
||||||
|
public ASYNC_BUILDER KEY_GENERIC_TYPE repeat(int repeats) {
|
||||||
|
iterable = ITERABLES.repeat(iterable, repeats);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Limits how many elements are inside of the Iterable
|
* Limits how many elements are inside of the Iterable
|
||||||
* @param limit how many elements should max be iterated through
|
* @param limit how many elements should max be iterated through
|
||||||
|
|
|
@ -147,6 +147,28 @@ public class ITERABLES
|
||||||
return new DistinctIterableBRACES(wrap(iterable));
|
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
|
* A Helper function that hard limits the Iterable to a specific size
|
||||||
* @param iterable that should be limited
|
* @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
|
private static class FilteredIterable KEY_GENERIC_TYPE implements ITERABLE KEY_GENERIC_TYPE
|
||||||
{
|
{
|
||||||
ITERABLE KEY_GENERIC_TYPE iterable;
|
ITERABLE KEY_GENERIC_TYPE iterable;
|
||||||
|
|
|
@ -215,6 +215,28 @@ public class ITERATORS
|
||||||
return new DistinctIteratorBRACES(wrap(iterator));
|
return new DistinctIteratorBRACES(wrap(iterator));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper function that repeats the Iterator a specific amount of times
|
||||||
|
* @param iterator that should be repeated
|
||||||
|
* @param repeats the amount of times the iterator should be repeated
|
||||||
|
* @Type(T)
|
||||||
|
* @return a repeating iterator
|
||||||
|
*/
|
||||||
|
public static GENERIC_KEY_BRACES ITERATOR KEY_GENERIC_TYPE repeat(ITERATOR KEY_GENERIC_TYPE iterator, int repeats) {
|
||||||
|
return new RepeatingIteratorBRACES(iterator, repeats);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper function that repeats the Iterator a specific amount of times from a Java Iterator
|
||||||
|
* @param iterator that should be repeated
|
||||||
|
* @param repeats the amount of times the iterator should be repeated
|
||||||
|
* @Type(T)
|
||||||
|
* @return a repeating iterator
|
||||||
|
*/
|
||||||
|
public static GENERIC_KEY_BRACES ITERATOR KEY_GENERIC_TYPE repeat(Iterator<? extends CLASS_TYPE> iterator, int repeats) {
|
||||||
|
return new RepeatingIteratorBRACES(wrap(iterator), repeats);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A Helper function that hard limits the Iterator to a specific size
|
* A Helper function that hard limits the Iterator to a specific size
|
||||||
* @param iterator that should be limited
|
* @param iterator that should be limited
|
||||||
|
@ -898,6 +920,38 @@ public class ITERATORS
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static class RepeatingIterator KEY_GENERIC_TYPE implements ITERATOR KEY_GENERIC_TYPE
|
||||||
|
{
|
||||||
|
final int repeats;
|
||||||
|
int index = 0;
|
||||||
|
ITERATOR KEY_GENERIC_TYPE iter;
|
||||||
|
LIST KEY_GENERIC_TYPE list = new ARRAY_LISTBRACES();
|
||||||
|
|
||||||
|
public RepeatingIterator(ITERATOR KEY_GENERIC_TYPE iter, int repeat) {
|
||||||
|
this.iter = iter;
|
||||||
|
this.repeats = repeat;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasNext() {
|
||||||
|
if(iter.hasNext()) return true;
|
||||||
|
if(index < repeats) {
|
||||||
|
index++;
|
||||||
|
iter = list.iterator();
|
||||||
|
return iter.hasNext();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public KEY_TYPE NEXT() {
|
||||||
|
if(!hasNext()) throw new NoSuchElementException();
|
||||||
|
KEY_TYPE value = iter.NEXT();
|
||||||
|
if(index == 0) list.add(value);
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static class SortedIterator KEY_GENERIC_TYPE implements ITERATOR KEY_GENERIC_TYPE
|
private static class SortedIterator KEY_GENERIC_TYPE implements ITERATOR KEY_GENERIC_TYPE
|
||||||
{
|
{
|
||||||
ITERATOR KEY_GENERIC_TYPE iterator;
|
ITERATOR KEY_GENERIC_TYPE iterator;
|
||||||
|
|
|
@ -39,6 +39,7 @@ import speiger.src.testers.PACKAGE.tests.iterable.FILE_KEY_TYPEIterableMapTester
|
||||||
import speiger.src.testers.PACKAGE.tests.iterable.FILE_KEY_TYPEIterableMatchesTester;
|
import speiger.src.testers.PACKAGE.tests.iterable.FILE_KEY_TYPEIterableMatchesTester;
|
||||||
import speiger.src.testers.PACKAGE.tests.iterable.FILE_KEY_TYPEIterablePeekTester;
|
import speiger.src.testers.PACKAGE.tests.iterable.FILE_KEY_TYPEIterablePeekTester;
|
||||||
import speiger.src.testers.PACKAGE.tests.iterable.FILE_KEY_TYPEIterableReduceTester;
|
import speiger.src.testers.PACKAGE.tests.iterable.FILE_KEY_TYPEIterableReduceTester;
|
||||||
|
import speiger.src.testers.PACKAGE.tests.iterable.FILE_KEY_TYPEIterableRepeatTester;
|
||||||
import speiger.src.testers.PACKAGE.tests.iterable.FILE_KEY_TYPEIterableSortedTester;
|
import speiger.src.testers.PACKAGE.tests.iterable.FILE_KEY_TYPEIterableSortedTester;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -63,6 +64,7 @@ public class COLLECTION_TEST_BUILDER KEY_GENERIC_TYPE extends CollectionTestSuit
|
||||||
testers.add(FILE_KEY_TYPEIterableMatchesTester.class);
|
testers.add(FILE_KEY_TYPEIterableMatchesTester.class);
|
||||||
testers.add(FILE_KEY_TYPEIterablePeekTester.class);
|
testers.add(FILE_KEY_TYPEIterablePeekTester.class);
|
||||||
testers.add(FILE_KEY_TYPEIterableReduceTester.class);
|
testers.add(FILE_KEY_TYPEIterableReduceTester.class);
|
||||||
|
testers.add(FILE_KEY_TYPEIterableRepeatTester.class);
|
||||||
testers.add(FILE_KEY_TYPEIterableCountTester.class);
|
testers.add(FILE_KEY_TYPEIterableCountTester.class);
|
||||||
testers.add(FILE_KEY_TYPEIterableFindFirstTester.class);
|
testers.add(FILE_KEY_TYPEIterableFindFirstTester.class);
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -0,0 +1,27 @@
|
||||||
|
package speiger.src.testers.PACKAGE.tests.iterable;
|
||||||
|
|
||||||
|
import org.junit.Ignore;
|
||||||
|
|
||||||
|
import com.google.common.collect.testing.features.CollectionSize;
|
||||||
|
|
||||||
|
import speiger.src.collections.PACKAGE.lists.ARRAY_LIST;
|
||||||
|
import speiger.src.collections.PACKAGE.lists.LIST;
|
||||||
|
import speiger.src.testers.PACKAGE.tests.base.ABSTRACT_COLLECTION_TESTER;
|
||||||
|
|
||||||
|
@Ignore
|
||||||
|
@SuppressWarnings("javadoc")
|
||||||
|
public class FILE_KEY_TYPEIterableRepeatTester KEY_GENERIC_TYPE extends ABSTRACT_COLLECTION_TESTER KEY_GENERIC_TYPE
|
||||||
|
{
|
||||||
|
#ignore
|
||||||
|
@CollectionSize.Require(absent = CollectionSize.ZERO)
|
||||||
|
#endignore
|
||||||
|
public void testIterableRepeat() {
|
||||||
|
LIST KEY_GENERIC_TYPE expected = new ARRAY_LISTBRACES();
|
||||||
|
expected.addAll(collection);
|
||||||
|
expected.addAll(collection);
|
||||||
|
expected.addAll(collection);
|
||||||
|
LIST KEY_GENERIC_TYPE result = collection.repeat(2).pourAsList();
|
||||||
|
assertEquals(expected.size(), result.size());
|
||||||
|
assertEquals("Repeat does not retain the iteration order", expected, result);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue