From 03b23f0e3c3d487a8e9c55ec5f6eab803769ab7a Mon Sep 17 00:00:00 2001 From: Speiger Date: Sat, 16 Jul 2022 05:00:22 +0200 Subject: [PATCH] Added New Iterable Feature -Added: Repeating iterable/iterator/async feature which allows to repeate collections a desired amount of times. -Added: Tests for it. --- .../templates/collections/Iterable.template | 9 +++ .../templates/utils/AsyncBuilder.template | 10 ++++ .../templates/utils/Iterables.template | 58 +++++++++++++++++++ .../templates/utils/Iterators.template | 54 +++++++++++++++++ .../CollectionTestSuiteBuilder.template | 2 + .../iterable/IterableRepeatTester.template | 27 +++++++++ 6 files changed, 160 insertions(+) create mode 100644 src/builder/resources/speiger/assets/testers/templates/tests/iterable/IterableRepeatTester.template diff --git a/src/builder/resources/speiger/assets/collections/templates/collections/Iterable.template b/src/builder/resources/speiger/assets/collections/templates/collections/Iterable.template index 09527331..4d0cfeea 100644 --- a/src/builder/resources/speiger/assets/collections/templates/collections/Iterable.template +++ b/src/builder/resources/speiger/assets/collections/templates/collections/Iterable.template @@ -149,6 +149,15 @@ public interface ITERABLE KEY_GENERIC_TYPE extends Iterable 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 * @param limit the amount of elements it should be limited to diff --git a/src/builder/resources/speiger/assets/collections/templates/utils/AsyncBuilder.template b/src/builder/resources/speiger/assets/collections/templates/utils/AsyncBuilder.template index 41209182..2de567ec 100644 --- a/src/builder/resources/speiger/assets/collections/templates/utils/AsyncBuilder.template +++ b/src/builder/resources/speiger/assets/collections/templates/utils/AsyncBuilder.template @@ -173,6 +173,16 @@ public class ASYNC_BUILDER KEY_GENERIC_TYPE 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 * @param limit how many elements should max be iterated through diff --git a/src/builder/resources/speiger/assets/collections/templates/utils/Iterables.template b/src/builder/resources/speiger/assets/collections/templates/utils/Iterables.template index 5269bf5b..17744526 100644 --- a/src/builder/resources/speiger/assets/collections/templates/utils/Iterables.template +++ b/src/builder/resources/speiger/assets/collections/templates/utils/Iterables.template @@ -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 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 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 iterator, int repeats) { + return new RepeatingIteratorBRACES(wrap(iterator), repeats); + } + /** * A Helper function that hard limits the Iterator to a specific size * @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 { ITERATOR KEY_GENERIC_TYPE iterator; diff --git a/src/builder/resources/speiger/assets/testers/templates/builder/CollectionTestSuiteBuilder.template b/src/builder/resources/speiger/assets/testers/templates/builder/CollectionTestSuiteBuilder.template index b6512098..42f5e219 100644 --- a/src/builder/resources/speiger/assets/testers/templates/builder/CollectionTestSuiteBuilder.template +++ b/src/builder/resources/speiger/assets/testers/templates/builder/CollectionTestSuiteBuilder.template @@ -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_TYPEIterablePeekTester; 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; #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_TYPEIterablePeekTester.class); testers.add(FILE_KEY_TYPEIterableReduceTester.class); + testers.add(FILE_KEY_TYPEIterableRepeatTester.class); testers.add(FILE_KEY_TYPEIterableCountTester.class); testers.add(FILE_KEY_TYPEIterableFindFirstTester.class); #endif diff --git a/src/builder/resources/speiger/assets/testers/templates/tests/iterable/IterableRepeatTester.template b/src/builder/resources/speiger/assets/testers/templates/tests/iterable/IterableRepeatTester.template new file mode 100644 index 00000000..b3db9a4a --- /dev/null +++ b/src/builder/resources/speiger/assets/testers/templates/tests/iterable/IterableRepeatTester.template @@ -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); + } +}