Primitive-Collections/src/builder/resources/speiger/assets/collections/templates/collections/Iterable.template

100 lines
2.9 KiB
Plaintext

package speiger.src.collections.PACKAGE.collections;
#if !TYPE_OBJECT
import java.util.Objects;
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;
#endif
import speiger.src.collections.PACKAGE.collections.SPLIT_ITERATOR;
import speiger.src.collections.PACKAGE.utils.SPLIT_ITERATORS;
/**
* A Type-Specific {@link Iterable} that reduces (un)boxing
* @Type(T)
*/
public interface ITERABLE KEY_GENERIC_TYPE extends Iterable<CLASS_TYPE>
{
/**
* Returns an iterator over elements of type {@code T}.
*
* @return an Iterator.
*/
@Override
ITERATOR KEY_GENERIC_TYPE iterator();
#if !TYPE_OBJECT
/**
* A Type Specific foreach function that reduces (un)boxing
*
* @implSpec
* <p>The default implementation behaves as if:
* <pre>{@code
* iterator().forEachRemaining(action);
* }</pre>
*
* @param action The action to be performed for each element
* @throws NullPointerException if the specified action is null
* @see Iterable#forEach(Consumer)
*/
default void forEach(CONSUMER action) {
Objects.requireNonNull(action);
iterator().forEachRemaining(action);
}
/** {@inheritDoc}
* <p>This default implementation delegates to the corresponding type-specific function.
* @deprecated Please use the corresponding type-specific function instead.
*/
@Deprecated
@Override
default void forEach(Consumer<? super CLASS_TYPE> action) {
Objects.requireNonNull(action);
iterator().forEachRemaining(action);
}
#endif
/**
* A Type Specific Type Splititerator to reduce boxing/unboxing
* @return type specific splititerator
*/
@Override
default SPLIT_ITERATOR KEY_GENERIC_TYPE spliterator() { return SPLIT_ITERATORS.createUnknownSplititerator(iterator(), 0); }
#if TYPE_OBJECT
/**
* A Helper function to reduce the usage of Streams and allows to convert a Iterable to something else.
* @param map the mapping function
* @Type(E)
* @return a new Iterable that returns the desired result
*/
public default <E> ObjectIterable<E> map(Function<T, E> map) {
return ObjectIterators.map(this, map);
}
/**
* A Helper function to reduce the usage of Streams and allows to convert a Iterable to something else.
* @param map the flatMapping function
* @Type(E)
* @Type(V)
* @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);
}
/**
* A Helper function to reduce the usage of Streams and allows to convert a Iterable to something else.
* @param map the flatMapping function
* @Type(E)
* @return a new Iterable that returns the desired result
*/
public default <E> ObjectIterable<E> arrayflatMap(Function<T, E[]> map) {
return ObjectIterators.arrayFlatMap(this, map);
}
#endif
}