package speiger.src.collections.PACKAGE.utils; import speiger.src.collections.PACKAGE.collections.ITERABLE; #if !TYPE_OBJECT import speiger.src.collections.objects.collections.ObjectIterable; import speiger.src.collections.objects.collections.ObjectIterator; #endif import speiger.src.collections.PACKAGE.collections.ITERATOR; import speiger.src.collections.PACKAGE.functions.function.TO_OBJECT_FUNCTION; import speiger.src.collections.PACKAGE.functions.function.PREDICATE; /** * A Helper class for Iterables */ public class ITERABLES { /** * 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) * @param The return type. * @return a iterable that is mapped to a new result */ public static GENERIC_KEY_SPECIAL_BRACES ObjectIterable map(ITERABLE KEY_GENERIC_TYPE iterable, TO_OBJECT_FUNCTION KKS_GENERIC_TYPE mapper) { return new MappedIterable<>(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) * @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 KEY_GENERIC_TYPE iterable, TO_OBJECT_FUNCTION KKS_GENERIC_TYPE mapper) { return new FlatMappedIterable<>(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) * @param The return type. * @return a iterable that is flatMapped to a new result */ public static GENERIC_KEY_SPECIAL_BRACES ObjectIterable arrayFlatMap(ITERABLE KEY_GENERIC_TYPE iterable, TO_OBJECT_FUNCTION KKS_GENERIC_TYPE mapper) { return new FlatMappedArrayIterable<>(iterable, mapper); } /** * A Helper function that filters out all desired elements * @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 KEY_GENERIC_TYPE iterable, PREDICATE KEY_GENERIC_TYPE filter) { return new FilteredIterableBRACES(iterable, filter); } private static class MappedIterable KSS_GENERIC_TYPE implements ObjectIterable { ITERABLE KEY_SPECIAL_GENERIC_TYPE iterable; TO_OBJECT_FUNCTION KSS_GENERIC_TYPE mapper; MappedIterable(ITERABLE KEY_SPECIAL_GENERIC_TYPE iterable, TO_OBJECT_FUNCTION KSS_GENERIC_TYPE mapper) { this.iterable = iterable; this.mapper = mapper; } public ObjectIterator iterator() { return ITERATORS.map(iterable.iterator(), mapper); } } private static class FlatMappedIterable KSS_GENERIC_TYPE> implements ObjectIterable { ITERABLE KEY_SPECIAL_GENERIC_TYPE iterable; TO_OBJECT_FUNCTION KSS_GENERIC_TYPE mapper; FlatMappedIterable(ITERABLE KEY_SPECIAL_GENERIC_TYPE iterable, TO_OBJECT_FUNCTION KSS_GENERIC_TYPE mapper) { this.iterable = iterable; this.mapper = mapper; } @Override public ObjectIterator iterator() { return ITERATORS.flatMap(iterable.iterator(), mapper); } } private static class FlatMappedArrayIterable KSS_GENERIC_TYPE implements ObjectIterable { ITERABLE KEY_SPECIAL_GENERIC_TYPE iterable; TO_OBJECT_FUNCTION KSS_GENERIC_TYPE mapper; FlatMappedArrayIterable(ITERABLE KEY_SPECIAL_GENERIC_TYPE iterable, TO_OBJECT_FUNCTION KSS_GENERIC_TYPE mapper) { this.iterable = iterable; this.mapper = mapper; } @Override public ObjectIterator iterator() { return ITERATORS.arrayFlatMap(iterable.iterator(), mapper); } } private static class FilteredIterable KEY_GENERIC_TYPE implements ITERABLE KEY_GENERIC_TYPE { ITERABLE KEY_GENERIC_TYPE iterable; PREDICATE KEY_GENERIC_TYPE filter; public FilteredIterable(ITERABLE KEY_GENERIC_TYPE iterable, PREDICATE KEY_GENERIC_TYPE filter) { this.iterable = iterable; this.filter = filter; } @Override public ITERATOR KEY_GENERIC_TYPE iterator() { return ITERATORS.filter(iterable.iterator(), filter); } } }