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
This commit is contained in:
2021-09-15 05:09:14 +02:00
parent 854678fd3c
commit fc1a738625
5 changed files with 175 additions and 91 deletions
@@ -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<CLASS_TYPE>
* @return a new Iterable that returns the desired result
*/
public default <E> ObjectIterable<E> map(Function<T, E> map) {
return ObjectIterators.map(this, map);
return ObjectIterables.map(this, map);
}
/**
@@ -83,7 +84,7 @@ public interface ITERABLE KEY_GENERIC_TYPE extends Iterable<CLASS_TYPE>
* @return a new Iterable that returns the desired result
*/
public default <E, V extends Iterable<E>> ObjectIterable<E> flatMap(Function<T, V> map) {
return ObjectIterators.flatMap(this, map);
return ObjectIterables.flatMap(this, map);
}
/**
@@ -93,8 +94,72 @@ public interface ITERABLE KEY_GENERIC_TYPE extends Iterable<CLASS_TYPE>
* @return a new Iterable that returns the desired result
*/
public default <E> ObjectIterable<E> arrayflatMap(Function<T, E[]> 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;
}
}
@@ -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 <T, E> ITERABLE<T> map(ITERABLE<E> iterable, Function<E, T> 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 <T, E, V extends Iterable<T>> ITERABLE<T> flatMap(ITERABLE<E> iterable, Function<E, V> 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 <T, E> ITERABLE<T> arrayFlatMap(ITERABLE<E> iterable, Function<E, T[]> mapper) {
return new FlatMappedArrayIterableBRACES(iterable, mapper);
}
private static class MappedIterable<E, T> implements ITERABLE<T>
{
ITERABLE<E> iterable;
Function<E, T> mapper;
MappedIterable(ITERABLE<E> iterable, Function<E, T> mapper) {
this.iterable = iterable;
this.mapper = mapper;
}
public ITERATOR<T> iterator() {
return ITERATORS.map(iterable.iterator(), mapper);
}
}
private static class FlatMappedIterable<E, T, V extends Iterable<T>> implements ITERABLE<T>
{
ITERABLE<E> iterable;
Function<E, V> mapper;
FlatMappedIterable(ITERABLE<E> iterable, Function<E, V> mapper) {
this.iterable = iterable;
this.mapper = mapper;
}
@Override
public ITERATOR<T> iterator() {
return ITERATORS.flatMap(iterable.iterator(), mapper);
}
}
private static class FlatMappedArrayIterable<E, T> implements ITERABLE<T>
{
ITERABLE<E> iterable;
Function<E, T[]> mapper;
FlatMappedArrayIterable(ITERABLE<E> iterable, Function<E, T[]> mapper) {
this.iterable = iterable;
this.mapper = mapper;
}
@Override
public ITERATOR<T> iterator() {
return ITERATORS.arrayFlatMap(iterable.iterator(), mapper);
}
}
#endif
}
@@ -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 <T, E> ITERABLE<T> map(ITERABLE<E> iterable, Function<E, T> 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 <T, E, V extends Iterable<T>> ITERABLE<T> flatMap(ITERABLE<E> iterable, Function<E, V> 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 <T, E> ITERABLE<T> arrayFlatMap(ITERABLE<E> iterable, Function<E, T[]> 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<E, T> implements ITERABLE<T>
{
ITERABLE<E> iterable;
Function<E, T> mapper;
MappedIterable(ITERABLE<E> iterable, Function<E, T> mapper) {
this.iterable = iterable;
this.mapper = mapper;
}
public ITERATOR<T> iterator() {
return new MappedIterator<>(iterable.iterator(), mapper);
}
}
private static class MappedIterator<E, T> implements ITERATOR<T>
{
ITERATOR<E> iterator;
@@ -723,38 +668,6 @@ public class ITERATORS
}
}
private static class FlatMappedIterable<E, T, V extends Iterable<T>> implements ITERABLE<T>
{
ITERABLE<E> iterable;
Function<E, V> mapper;
FlatMappedIterable(ITERABLE<E> iterable, Function<E, V> mapper) {
this.iterable = iterable;
this.mapper = mapper;
}
@Override
public ITERATOR<T> iterator() {
return new FlatMappedIterator<>(iterable.iterator(), mapper);
}
}
private static class FlatMappedArrayIterable<E, T> implements ITERABLE<T>
{
ITERABLE<E> iterable;
Function<E, T[]> mapper;
FlatMappedArrayIterable(ITERABLE<E> iterable, Function<E, T[]> mapper) {
this.iterable = iterable;
this.mapper = mapper;
}
@Override
public ITERATOR<T> iterator() {
return new FlatMappedArrayIterator<>(iterable.iterator(), mapper);
}
}
private static class FlatMappedIterator<E, T, V extends Iterable<T>> implements ITERATOR<T>
{
ITERATOR<E> iterator;