From 3c5769e0e21d62d04d92dd2e877d5826e2c9438e Mon Sep 17 00:00:00 2001 From: Speiger Date: Tue, 28 Sep 2021 03:34:53 +0200 Subject: [PATCH] Added java type support for Stream replacer functions. --- .../templates/utils/Iterables.template | 70 +++++++++++++++++++ .../templates/utils/Iterators.template | 48 +++++++++++++ 2 files changed, 118 insertions(+) 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 a912ea85..3a80ce23 100644 --- a/src/builder/resources/speiger/assets/collections/templates/utils/Iterables.template +++ b/src/builder/resources/speiger/assets/collections/templates/utils/Iterables.template @@ -14,6 +14,18 @@ import speiger.src.collections.PACKAGE.functions.function.PREDICATE; */ public class ITERABLES { + /** + * A Helper function that maps a Java-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) + * @param The return type. + * @return a iterable that is mapped to a new result + */ + public static GENERIC_KEY_SPECIAL_BRACES ObjectIterable map(Iterable iterable, TO_OBJECT_FUNCTION KKS_GENERIC_TYPE mapper) { + return new MappedIterable<>(wrap(iterable), mapper); + } + /** * A Helper function that maps a Iterable into a new Type. * @param iterable the iterable that should be mapped @@ -26,6 +38,19 @@ public class ITERABLES return new MappedIterable<>(iterable, mapper); } + /** + * A Helper function that flatMaps a Java-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) + * @param The return type supplier. + * @param The return type. + * @return a iterable that is flatMapped to a new result + */ + public static GENERIC_KEY_SPECIAL_BRACES> ObjectIterable flatMap(Iterable iterable, TO_OBJECT_FUNCTION KKS_GENERIC_TYPE mapper) { + return new FlatMappedIterable<>(wrap(iterable), mapper); + } + /** * A Helper function that flatMaps a Iterable into a new Type. * @param iterable the iterable that should be flatMapped @@ -39,6 +64,18 @@ public class ITERABLES return new FlatMappedIterable<>(iterable, mapper); } + /** + * A Helper function that flatMaps a Java-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) + * @param The return type. + * @return a iterable that is flatMapped to a new result + */ + public static GENERIC_KEY_SPECIAL_BRACES ObjectIterable arrayFlatMap(Iterable iterable, TO_OBJECT_FUNCTION KKS_GENERIC_TYPE mapper) { + return new FlatMappedArrayIterable<>(wrap(iterable), mapper); + } + /** * A Helper function that flatMaps a Iterable into a new Type. * @param iterable the iterable that should be flatMapped @@ -51,6 +88,17 @@ public class ITERABLES return new FlatMappedArrayIterable<>(iterable, mapper); } + /** + * A Helper function that filters out all desired elements from a Java-Iterable + * @param iterable that should be filtered. + * @param filter the filter that decides that should be let through + * @Type(T) + * @return a filtered iterable + */ + public static GENERIC_KEY_BRACES ITERABLE KEY_GENERIC_TYPE filter(Iterable iterable, PREDICATE KEY_GENERIC_TYPE filter) { + return new FilteredIterableBRACES(wrap(iterable), filter); + } + /** * A Helper function that filters out all desired elements * @param iterable that should be filtered. @@ -62,6 +110,28 @@ public class ITERABLES return new FilteredIterableBRACES(iterable, filter); } + /** + * A Wrapper function that wraps a Java-Iterable into a Type Specific Iterable + * @param iterable that should be wrapped + * @return a type specific iterable + */ + public static GENERIC_KEY_BRACES ITERABLE KEY_GENERIC_TYPE wrap(Iterable iterable) { + return new WrappedIterableBRACES(iterable); + } + + private static class WrappedIterable KEY_GENERIC_TYPE implements ITERABLE KEY_GENERIC_TYPE + { + Iterable iterable; + + public WrappedIterable(Iterable iterable) { + this.iterable = iterable; + } + + public ITERATOR KEY_GENERIC_TYPE iterator() { + return ITERATORS.wrap(iterable.iterator()); + } + } + private static class MappedIterable KSS_GENERIC_TYPE implements ObjectIterable { ITERABLE KEY_SPECIAL_GENERIC_TYPE iterable; 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 b19ffc87..3d6fa56b 100644 --- a/src/builder/resources/speiger/assets/collections/templates/utils/Iterators.template +++ b/src/builder/resources/speiger/assets/collections/templates/utils/Iterators.template @@ -89,6 +89,18 @@ public class ITERATORS return iterator instanceof UnmodifiableListIterator ? iterator : new UnmodifiableListIteratorBRACES(iterator); } + /** + * A Helper function that maps a Java-Iterator into a new Type. + * @param iterator that should be mapped + * @param mapper the function that decides what the result turns into. + * @Type(T) + * @param The return type. + * @return a iterator that is mapped to a new result + */ + public static GENERIC_KEY_SPECIAL_BRACES ObjectIterator map(Iterator iterator, TO_OBJECT_FUNCTION KKS_GENERIC_TYPE mapper) { + return new MappedIterator<>(wrap(iterator), mapper); + } + /** * A Helper function that maps a Iterator into a new Type. * @param iterator that should be mapped @@ -101,6 +113,19 @@ public class ITERATORS return new MappedIterator<>(iterator, mapper); } + /** + * A Helper function that flatMaps a Java-Iterator into a new Type. + * @param iterator that should be flatMapped + * @param mapper the function that decides what the result turns into. + * @Type(T) + * @param The return type supplier. + * @param The return type. + * @return a iterator that is flatMapped to a new result + */ + public static GENERIC_KEY_SPECIAL_BRACES> ObjectIterator flatMap(Iterator iterator, TO_OBJECT_FUNCTION KKS_GENERIC_TYPE mapper) { + return new FlatMappedIterator<>(wrap(iterator), mapper); + } + /** * A Helper function that flatMaps a Iterator into a new Type. * @param iterator that should be flatMapped @@ -114,6 +139,18 @@ public class ITERATORS return new FlatMappedIterator<>(iterator, mapper); } + /** + * A Helper function that flatMaps a Java-Iterator into a new Type. + * @param iterator that should be flatMapped + * @param mapper the function that decides what the result turns into. + * @Type(T) + * @param The return type. + * @return a iterator that is flatMapped to a new result + */ + public static GENERIC_KEY_SPECIAL_BRACES ObjectIterator arrayFlatMap(Iterator iterator, TO_OBJECT_FUNCTION KKS_GENERIC_TYPE mapper) { + return new FlatMappedArrayIterator<>(wrap(iterator), mapper); + } + /** * A Helper function that flatMaps a Iterator into a new Type. * @param iterator that should be flatMapped @@ -126,6 +163,17 @@ public class ITERATORS return new FlatMappedArrayIterator<>(iterator, mapper); } + /** + * A Helper function that filters out all desired elements from a Java-Iterator + * @param iterator that should be filtered. + * @param filter the filter that decides that should be let through + * @Type(T) + * @return a filtered iterator + */ + public static GENERIC_KEY_BRACES ITERATOR KEY_GENERIC_TYPE filter(Iterator iterator, PREDICATE KEY_GENERIC_TYPE filter) { + return new FilteredIteratorBRACES(wrap(iterator), filter); + } + /** * A Helper function that filters out all desired elements * @param iterator that should be filtered.