From 4a3cc6640195a0019178809894a0dc393d670d45 Mon Sep 17 00:00:00 2001 From: Speiger Date: Wed, 21 Jul 2021 16:32:30 +0200 Subject: [PATCH] Added Flat/Mapping Functions for Iterables/Iterators (Object Only) --- Changelog.md | 3 + .../templates/collections/Iterable.template | 37 +++ .../templates/utils/Iterators.template | 234 +++++++++++++++++- .../ints/base/BaseIntSortedSetTest.java | 1 - 4 files changed, 273 insertions(+), 2 deletions(-) diff --git a/Changelog.md b/Changelog.md index 448db3ee..559fbb79 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,6 +1,9 @@ # Changelog of versions +### Version 0.3.3 +- Added: Flat/Mapping function for Iterables/Iterators to help avoid streams for cleaner looking code + ### Version 0.3.2 - Fixed: Map.put wasn't referring to primitive variants. - Added: ImmutableList. 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 baaf1956..db0992f8 100644 --- a/src/builder/resources/speiger/assets/collections/templates/collections/Iterable.template +++ b/src/builder/resources/speiger/assets/collections/templates/collections/Iterable.template @@ -5,6 +5,9 @@ import java.util.Objects; import java.util.function.Consumer; import speiger.src.collections.PACKAGE.functions.CONSUMER; +#else +import java.util.function.Function; +import speiger.src.collections.PACKAGE.utils.ITERATORS; #endif import speiger.src.collections.PACKAGE.collections.SPLIT_ITERATOR; import speiger.src.collections.PACKAGE.utils.SPLIT_ITERATORS; @@ -60,4 +63,38 @@ public interface ITERABLE KEY_GENERIC_TYPE extends Iterable */ @Override default SPLIT_ITERATOR KEY_GENERIC_TYPE spliterator() { return SPLIT_ITERATORS.createUnknownSplititerator(iterator(), 0); } + +#if TYPE_OBJECT + /** + * A Helper function to reduce the usage of Streams and allows to convert a Iterable to something else. + * @param map the mapping function + * @Type(E) + * @return a new Iterable that returns the desired result + */ + public default ObjectIterable map(Function map) { + return ObjectIterators.map(this, map); + } + + /** + * A Helper function to reduce the usage of Streams and allows to convert a Iterable to something else. + * @param map the flatMapping function + * @Type(E) + * @Type(V) + * @return a new Iterable that returns the desired result + */ + public default > ObjectIterable flatMap(Function map) { + return ObjectIterators.flatMap(this, map); + } + + /** + * A Helper function to reduce the usage of Streams and allows to convert a Iterable to something else. + * @param map the flatMapping function + * @Type(E) + * @return a new Iterable that returns the desired result + */ + public default ObjectIterable arrayflatMap(Function map) { + return ObjectIterators.arrayFlatMap(this, map); + } + +#endif } \ No newline at end of file diff --git a/src/builder/resources/speiger/assets/collections/templates/utils/Iterators.template b/src/builder/resources/speiger/assets/collections/templates/utils/Iterators.template index 01032b9e..84fd7573 100644 --- a/src/builder/resources/speiger/assets/collections/templates/utils/Iterators.template +++ b/src/builder/resources/speiger/assets/collections/templates/utils/Iterators.template @@ -2,12 +2,17 @@ package speiger.src.collections.PACKAGE.utils; import java.util.Iterator; import java.util.NoSuchElementException; +import java.util.function.Function; + import speiger.src.collections.PACKAGE.collections.ITERATOR; import speiger.src.collections.PACKAGE.lists.ARRAY_LIST; import speiger.src.collections.PACKAGE.lists.LIST; import speiger.src.collections.PACKAGE.lists.LIST_ITERATOR; import speiger.src.collections.PACKAGE.collections.BI_ITERATOR; import speiger.src.collections.PACKAGE.collections.COLLECTION; +#if TYPE_OBJECT +import speiger.src.collections.PACKAGE.collections.ITERABLE; +#endif /** * A Helper class for Iterators @@ -81,7 +86,84 @@ public class ITERATORS public static GENERIC_KEY_BRACES LIST_ITERATOR KEY_GENERIC_TYPE unmodifiable(LIST_ITERATOR KEY_GENERIC_TYPE iterator) { return iterator instanceof UnmodifiableListIterator ? iterator : new UnmodifiableListIteratorBRACES(iterator); } - /** + +#if TYPE_OBJECT + /** + * A Helper function that maps a Iterable into a new Type. + * @param iterable the iterable that should be mapped + * @param mapper the function that decides what the result turns into. + * @Type(T) + * @Type(E) + * @return a iterable that is mapped to a new result + */ + public static ITERABLE map(ITERABLE iterable, Function mapper) { + return new MappedIterableBRACES(iterable, mapper); + } + + /** + * A Helper function that maps a Iterator into a new Type. + * @param iterator that should be mapped + * @param mapper the function that decides what the result turns into. + * @Type(T) + * @Type(E) + * @return a iterator that is mapped to a new result + */ + public static ITERATOR map(ITERATOR iterator, Function mapper) { + return new MappedIteratorBRACES(iterator, mapper); + } + + /** + * A Helper function that flatMaps a Iterable into a new Type. + * @param iterable the iterable that should be flatMapped + * @param mapper the function that decides what the result turns into. + * @Type(T) + * @Type(V) + * @Type(E) + * @return a iterable that is flatMapped to a new result + */ + public static > ITERABLE flatMap(ITERABLE iterable, Function mapper) { + return new FlatMappedIterableBRACES(iterable, mapper); + } + + /** + * A Helper function that flatMaps a Iterable into a new Type. + * @param iterable the iterable that should be flatMapped + * @param mapper the function that decides what the result turns into. + * @Type(T) + * @Type(E) + * @return a iterable that is flatMapped to a new result + */ + public static ITERABLE arrayFlatMap(ITERABLE iterable, Function mapper) { + return new FlatMappedArrayIterableBRACES(iterable, mapper); + } + + /** + * A Helper function that flatMaps a Iterator into a new Type. + * @param iterator that should be flatMapped + * @param mapper the function that decides what the result turns into. + * @Type(T) + * @Type(V) + * @Type(E) + * @return a iterator that is flatMapped to a new result + */ + public static > ITERATOR flatMap(ITERATOR iterator, Function mapper) { + return new FlatMappedIteratorBRACES(iterator, mapper); + } + + /** + * A Helper function that flatMaps a Iterator into a new Type. + * @param iterator that should be flatMapped + * @param mapper the function that decides what the result turns into. + * @Type(T) + * @Type(E) + * @return a iterator that is flatMapped to a new result + */ + public static ITERATOR arrayFlatMap(ITERATOR iterator, Function mapper) { + return new FlatMappedArrayIteratorBRACES(iterator, mapper); + } + +#endif + /** * Helper function to convert a Object Iterator into a Primitive Iterator * @param iterator that should be converted to a unboxing iterator * @ArrayType(T) @@ -596,4 +678,154 @@ public class ITERATORS return amount - left; } } + +#if TYPE_OBJECT + private static class MappedIterable implements ITERABLE + { + ITERABLE iterable; + Function mapper; + + MappedIterable(ITERABLE iterable, Function mapper) { + this.iterable = iterable; + this.mapper = mapper; + } + + public ITERATOR iterator() { + return new MappedIterator<>(iterable.iterator(), mapper); + } + } + + private static class MappedIterator implements ITERATOR + { + ITERATOR iterator; + Function mapper; + + MappedIterator(ITERATOR iterator, Function mapper) { + this.iterator = iterator; + this.mapper = mapper; + } + + @Override + public boolean hasNext() { + return iterator.hasNext(); + } + + @Override + public KEY_TYPE NEXT() { + return mapper.apply(iterator.NEXT()); + } + + @Override + public int skip(int amount) { + return iterator.skip(amount); + } + } + + private static class FlatMappedIterable> implements ITERABLE + { + ITERABLE iterable; + Function mapper; + + FlatMappedIterable(ITERABLE iterable, Function mapper) { + this.iterable = iterable; + this.mapper = mapper; + } + + @Override + public ITERATOR iterator() { + return new FlatMappedIterator<>(iterable.iterator(), mapper); + } + } + + private static class FlatMappedArrayIterable implements ITERABLE + { + ITERABLE iterable; + Function mapper; + + FlatMappedArrayIterable(ITERABLE iterable, Function mapper) { + this.iterable = iterable; + this.mapper = mapper; + } + + @Override + public ITERATOR iterator() { + return new FlatMappedArrayIterator<>(iterable.iterator(), mapper); + } + } + + private static class FlatMappedIterator> implements ITERATOR + { + ITERATOR iterator; + Iterator last = null; + Function mapper; + boolean foundNext = false; + + FlatMappedIterator(ITERATOR iterator, Function mapper) { + this.iterator = iterator; + this.mapper = mapper; + } + + void compute() { + if(foundNext) return; + foundNext = true; + while(iterator.hasNext()) + { + if(last != null && last.hasNext()) return; + last = mapper.apply(iterator.next()).iterator(); + } + } + + @Override + public boolean hasNext() { + compute(); + return last != null && last.hasNext(); + } + + @Override + public KEY_TYPE NEXT() { + if(!hasNext()) throw new IllegalStateException("End of Iterator"); + KEY_TYPE result = last.next(); + foundNext = false; + return result; + } + } + + private static class FlatMappedArrayIterator implements ITERATOR + { + ITERATOR iterator; + Iterator last = null; + Function mapper; + boolean foundNext = false; + + FlatMappedArrayIterator(ITERATOR iterator, Function mapper) { + this.iterator = iterator; + this.mapper = mapper; + } + + void compute() { + if(foundNext) return; + foundNext = true; + while(iterator.hasNext()) + { + if(last != null && last.hasNext()) return; + last = wrap(mapper.apply(iterator.next())); + } + } + + @Override + public boolean hasNext() { + compute(); + return last != null && last.hasNext(); + } + + @Override + public T next() { + if(!hasNext()) throw new IllegalStateException("End of Iterator"); + KEY_TYPE result = last.next(); + foundNext = false; + return result; + } + } + +#endif } \ No newline at end of file diff --git a/src/test/java/speiger/src/collections/ints/base/BaseIntSortedSetTest.java b/src/test/java/speiger/src/collections/ints/base/BaseIntSortedSetTest.java index f970827c..9aa7a887 100644 --- a/src/test/java/speiger/src/collections/ints/base/BaseIntSortedSetTest.java +++ b/src/test/java/speiger/src/collections/ints/base/BaseIntSortedSetTest.java @@ -51,7 +51,6 @@ public abstract class BaseIntSortedSetTest extends BaseIntCollectionTest IntSortedSet set = create(TEST_ARRAY); Assert.assertEquals(set.pollFirstInt(), 0); Assert.assertEquals(set.pollLastInt(), 99); - } }