From fc1a738625a98a96158e2d91a39dc2416bc21579 Mon Sep 17 00:00:00 2001 From: Speiger Date: Wed, 15 Sep 2021 05:09:14 +0200 Subject: [PATCH] Started Next larger patch. - Changed: Iterable specific helper functions were moved out of Iterators and moved into Iterables - Added: New Stream replacing functions: findFirst, matchesAny/All/None --- Changelog.md | 4 + .../speiger/src/builder/GlobalVariables.java | 2 + .../templates/collections/Iterable.template | 73 ++++++++++++- .../templates/utils/Iterables.template | 100 ++++++++++++++++++ .../templates/utils/Iterators.template | 87 --------------- 5 files changed, 175 insertions(+), 91 deletions(-) create mode 100644 src/builder/resources/speiger/assets/collections/templates/utils/Iterables.template diff --git a/Changelog.md b/Changelog.md index 1971ae45..6a66cf38 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,6 +1,10 @@ # Changelog of versions +### Version 0.3.7/0.4.0 +- Changed: Iterable specific helper functions were moved out of Iterators and moved into Iterables +- Added: New Stream replacing functions: findFirst, matchesAny/All/None + ### Version 0.3.6 - Fixed: addAll non Type Specific Lists was causing crashes. - Fixed/Changed: clearAndTrim's implementation was all over the place. In some cases causing crash scenarios. diff --git a/src/builder/java/speiger/src/builder/GlobalVariables.java b/src/builder/java/speiger/src/builder/GlobalVariables.java index 66514bd6..300d33d1 100644 --- a/src/builder/java/speiger/src/builder/GlobalVariables.java +++ b/src/builder/java/speiger/src/builder/GlobalVariables.java @@ -176,6 +176,7 @@ public class GlobalVariables addClassMapper("PRIORITY_QUEUES", "PriorityQueues"); addClassMapper("SPLIT_ITERATORS", "Splititerators"); addClassMapper("ITERATORS", "Iterators"); + addClassMapper("ITERABLES", "Iterables"); addBiClassMapper("MAPS", "Maps", "2"); //Interfaces @@ -195,6 +196,7 @@ public class GlobalVariables addClassMapper("NAVIGABLE_SET", "NavigableSet"); addClassMapper("PRIORITY_QUEUE", "PriorityQueue"); addClassMapper("PRIORITY_DEQUEUE", "PriorityDequeue"); + addClassMapper("PREDICATE", "2BooleanFunction"); addClassMapper("SORTED_SET", "SortedSet"); addClassMapper("SET", "Set"); addClassMapper("STRATEGY", "Strategy"); 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 db0992f8..cc43712b 100644 --- a/src/builder/resources/speiger/assets/collections/templates/collections/Iterable.template +++ b/src/builder/resources/speiger/assets/collections/templates/collections/Iterable.template @@ -7,8 +7,9 @@ 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; +import speiger.src.collections.PACKAGE.utils.ITERABLES; #endif +import speiger.src.collections.PACKAGE.functions.function.PREDICATE; import speiger.src.collections.PACKAGE.collections.SPLIT_ITERATOR; import speiger.src.collections.PACKAGE.utils.SPLIT_ITERATORS; @@ -72,7 +73,7 @@ public interface ITERABLE KEY_GENERIC_TYPE extends Iterable * @return a new Iterable that returns the desired result */ public default ObjectIterable map(Function map) { - return ObjectIterators.map(this, map); + return ObjectIterables.map(this, map); } /** @@ -83,7 +84,7 @@ public interface ITERABLE KEY_GENERIC_TYPE extends Iterable * @return a new Iterable that returns the desired result */ public default > ObjectIterable flatMap(Function map) { - return ObjectIterators.flatMap(this, map); + return ObjectIterables.flatMap(this, map); } /** @@ -93,8 +94,72 @@ public interface ITERABLE KEY_GENERIC_TYPE extends Iterable * @return a new Iterable that returns the desired result */ public default ObjectIterable arrayflatMap(Function map) { - return ObjectIterators.arrayFlatMap(this, map); + return ObjectIterables.arrayFlatMap(this, map); } #endif + /** + * Helper function to reduce stream usage that allows to filter for any matches. + * @param filter that should be applied + * @return true if any matches were found + */ + public default boolean matchesAny(PREDICATE KEY_GENERIC_TYPE filter) { + for(ITERATOR KEY_GENERIC_TYPE iter = iterator();iter.hasNext();) { +#if TYPE_OBJECT + if(filter.getBoolean(iter.NEXT())) return true; +#else + if(filter.GET_VALUE(iter.NEXT())) return true; +#endif + } + return false; + } + + /** + * Helper function to reduce stream usage that allows to filter for no matches. + * @param filter that should be applied + * @return true if no matches were found + */ + public default boolean matchesNone(PREDICATE KEY_GENERIC_TYPE filter) { + for(ITERATOR KEY_GENERIC_TYPE iter = iterator();iter.hasNext();) { +#if TYPE_OBJECT + if(filter.getBoolean(iter.NEXT())) return false; +#else + if(filter.GET_VALUE(iter.NEXT())) return false; +#endif + } + return true; + } + + /** + * Helper function to reduce stream usage that allows to filter for all matches. + * @param filter that should be applied + * @return true if all matches. + */ + public default boolean matchesAll(PREDICATE KEY_GENERIC_TYPE filter) { + for(ITERATOR KEY_GENERIC_TYPE iter = iterator();iter.hasNext();) { +#if TYPE_OBJECT + if(!filter.getBoolean(iter.NEXT())) return false; +#else + if(!filter.GET_VALUE(iter.NEXT())) return false; +#endif + } + return true; + } + + /** + * Helper function to reduce stream usage that allows to filter for the first match. + * @param filter that should be applied + * @return the found value or the null equivalent variant. + */ + public default KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { + for(ITERATOR KEY_GENERIC_TYPE iter = iterator();iter.hasNext();) { + KEY_TYPE entry = iter.NEXT(); +#if TYPE_OBJECT + if(filter.getBoolean(entry)) return entry; +#else + if(filter.GET_VALUE(entry)) return entry; +#endif + } + return EMPTY_VALUE; + } } \ No newline at end of file diff --git a/src/builder/resources/speiger/assets/collections/templates/utils/Iterables.template b/src/builder/resources/speiger/assets/collections/templates/utils/Iterables.template new file mode 100644 index 00000000..5db07621 --- /dev/null +++ b/src/builder/resources/speiger/assets/collections/templates/utils/Iterables.template @@ -0,0 +1,100 @@ +package speiger.src.collections.PACKAGE.utils; + +#if TYPE_OBJECT +import java.util.function.Function; +import speiger.src.collections.PACKAGE.collections.ITERABLE; +import speiger.src.collections.PACKAGE.collections.ITERATOR; +#endif + +/** + * A Helper class for Iterables + */ +public class ITERABLES +{ +#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 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); + } + + 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 ITERATORS.map(iterable.iterator(), mapper); + } + } + + 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 ITERATORS.flatMap(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 ITERATORS.arrayFlatMap(iterable.iterator(), mapper); + } + } +#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 80d3d54f..e085e7eb 100644 --- a/src/builder/resources/speiger/assets/collections/templates/utils/Iterators.template +++ b/src/builder/resources/speiger/assets/collections/templates/utils/Iterators.template @@ -12,9 +12,6 @@ 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 @@ -90,18 +87,6 @@ public class ITERATORS } #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 @@ -114,31 +99,6 @@ public class ITERATORS 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 @@ -682,21 +642,6 @@ public class ITERATORS } #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; @@ -723,38 +668,6 @@ public class ITERATORS } } - 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;