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 }