package speiger.src.collections.PACKAGE.collections; import java.util.Collection; #if PRIMITIVES import java.util.Objects; import java.util.function.JAVA_PREDICATE; import java.util.function.Predicate; import java.util.stream.JAVA_STREAM; import java.util.stream.StreamSupport; #endif import speiger.src.collections.PACKAGE.utils.SPLIT_ITERATORS; #if TYPE_BYTE || TYPE_SHORT || TYPE_CHAR || TYPE_FLOAT import speiger.src.collections.utils.SanityChecks; #endif /** * A Type-Specific {@link Collection} that reduces (un)boxing * @Type(T) */ public interface COLLECTION KEY_GENERIC_TYPE extends Collection, ITERABLE KEY_GENERIC_TYPE { #if !TYPE_OBJECT /** * A Type-Specific add function to reduce (un)boxing * @param o the element that should be added * @return true if the element was added to the collection */ public boolean add(KEY_TYPE o); #endif /** * A Type-Specific addAll function to reduce (un)boxing * @param c the collection of elements that should be added * @return true if elements were added into the collection */ public boolean addAll(COLLECTION KEY_GENERIC_TYPE c); #if !TYPE_OBJECT /** * A Type-Specific contains function to reduce (un)boxing * @param o the element that is checked for * @return true if the element is found in the collection */ public boolean contains(KEY_TYPE o); #endif /** * A Type-Specific containsAll function to reduce (un)boxing * @param c the collection of elements that should be tested for * @return true if all the element is found in the collection */ public boolean containsAll(COLLECTION KEY_GENERIC_TYPE c); /** * A Type-Specific containsAny function to reduce (un)boxing * @param c the collection of elements that should be tested for * @return true if any element was found */ public boolean containsAny(COLLECTION KEY_GENERIC_TYPE c); /** * Returns true if any element of the Collection is found in the provided collection. * A Small Optimization function to find out of any element is present when comparing collections and not all of them. * @param c the collection of elements that should be tested for * @return true if any element was found. */ @Primitive public boolean containsAny(Collection c); #if !TYPE_OBJECT /** * A Type-Specific remove function that reduces (un)boxing. * @param o the element that should be removed * @return true if the element was removed * @see Collection#remove(Object) */ public boolean REMOVE_KEY(KEY_TYPE o); #endif /** * A Type-Specific removeAll function that reduces (un)boxing. * @param c the collection of elements that should be removed * @return true if any element was removed * @see Collection#removeAll(Collection) */ public boolean removeAll(COLLECTION KEY_GENERIC_TYPE c); /** * A Type-Specific retainAll function that reduces (un)boxing. * @param c the collection of elements that should be kept * @return true if any element was removed * @see Collection#retainAll(Collection) */ public boolean retainAll(COLLECTION KEY_GENERIC_TYPE c); #if !TYPE_OBJECT /** * A Type-Specific toArray function that delegates to {@link #TO_ARRAY(KEY_TYPE[])} with a newly created array. * @return an array containing all of the elements in this collection * @see Collection#toArray() */ public KEY_TYPE[] TO_ARRAY(); /** * A Type-Specific toArray function that reduces (un)boxing. * @param a array that the elements should be injected to. If null or to small a new array with the right size is created * @return an array containing all of the elements in this collection * @see Collection#toArray(Object[]) */ public KEY_TYPE[] TO_ARRAY(KEY_TYPE[] a); #if PRIMITIVES /** {@inheritDoc} *

This default implementation delegates to the corresponding type-specific function. * @deprecated Please use the corresponding type-specific function instead. */ @Override @Deprecated public default boolean removeIf(Predicate filter) { Objects.requireNonNull(filter); #if TYPE_BYTE || TYPE_SHORT || TYPE_CHAR || TYPE_FLOAT return remIf(v -> filter.test(KEY_TO_OBJ(SanityChecks.SANITY_CAST(v)))); #else return remIf(v -> filter.test(KEY_TO_OBJ(v))); #endif } /** * A Type-Specific removeIf function to reduce (un)boxing. *

Removes elements that were selected by the filter * @see Collection#removeIf(Predicate) * @param filter Filters the elements that should be removed * @return true if the collection was modified * @throws java.lang.NullPointerException if filter is null */ public default boolean remIf(JAVA_PREDICATE filter) { Objects.requireNonNull(filter); boolean removed = false; final ITERATOR each = iterator(); while (each.hasNext()) { if (filter.test(each.NEXT())) { each.remove(); removed = true; } } return removed; } #endif /** {@inheritDoc} *

This default implementation delegates to the corresponding type-specific function. * @deprecated Please use the corresponding type-specific function instead. */ @Override @Deprecated public default boolean add(CLASS_TYPE o) { return add(OBJ_TO_KEY(o)); } /** {@inheritDoc} *

This default implementation delegates to the corresponding type-specific function. * @deprecated Please use the corresponding type-specific function instead. */ @Override @Deprecated public default boolean contains(Object o) { return o != null && contains(CLASS_TO_KEY(o)); } /** {@inheritDoc} *

This default implementation delegates to the corresponding type-specific function. * @deprecated Please use the corresponding type-specific function instead. */ @Override @Deprecated public default boolean remove(Object o) { return o != null && REMOVE_KEY(CLASS_TO_KEY(o)); } #endif /** * Returns a Type-Specific Iterator to reduce (un)boxing * @return a iterator of the collection * @see Collection#iterator() */ @Override public ITERATOR KEY_GENERIC_TYPE iterator(); #if PRIMITIVES /** * Returns a Java-Type-Specific Stream to reduce boxing/unboxing. * @return a Stream of the closest java type */ default JAVA_STREAM primitiveStream() { return StreamSupport.NEW_STREAM(SPLIT_ITERATORS.createJavaSplititerator(this, 0), false); } /** * Returns a Java-Type-Specific Parallel Stream to reduce boxing/unboxing. * @return a Stream of the closest java type */ default JAVA_STREAM parallelPrimitiveStream() { return StreamSupport.NEW_STREAM(SPLIT_ITERATORS.createJavaSplititerator(this, 0), true); } #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.createSplititerator(this, 0); } }