323 lines
11 KiB
Plaintext
323 lines
11 KiB
Plaintext
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;
|
|
#if SPLIT_ITERATOR_FEATURE && STREAM_FEATURE
|
|
import java.util.stream.JAVA_STREAM;
|
|
import java.util.stream.StreamSupport;
|
|
#endif
|
|
#endif
|
|
#if TYPE_OBJECT
|
|
import java.util.function.Consumer;
|
|
import java.util.function.IntFunction;
|
|
#else
|
|
import speiger.src.collections.PACKAGE.functions.CONSUMER;
|
|
#endif
|
|
#if SPLIT_ITERATOR_FEATURE
|
|
import speiger.src.collections.PACKAGE.utils.SPLIT_ITERATORS;
|
|
#endif
|
|
import speiger.src.collections.PACKAGE.utils.COLLECTIONS;
|
|
import speiger.src.collections.utils.ISizeProvider;
|
|
import speiger.src.collections.utils.SanityChecks;
|
|
|
|
/**
|
|
* A Type-Specific {@link Collection} that reduces (un)boxing
|
|
* @Type(T)
|
|
*/
|
|
public interface COLLECTION KEY_GENERIC_TYPE extends Collection<CLASS_TYPE>, ITERABLE KEY_GENERIC_TYPE, ISizeProvider
|
|
{
|
|
#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);
|
|
|
|
/**
|
|
* A Type-Specific Array based addAll method to reduce the amount of Wrapping
|
|
* @param e the elements that should be added
|
|
* @return if the collection was modified
|
|
*/
|
|
public default boolean addAll(KEY_TYPE... e) { return addAll(e, 0, e.length); }
|
|
|
|
/**
|
|
* A Type-Specific Array based addAll method to reduce the amount of Wrapping
|
|
* @param e the elements that should be added
|
|
* @param length how many elements of the array should be added
|
|
* @return if the collection was modified
|
|
*/
|
|
public default boolean addAll(KEY_TYPE[] e, int length) { return addAll(e, 0, length); }
|
|
|
|
/**
|
|
* A Type-Specific Array based addAll method to reduce the amount of Wrapping
|
|
* @param e the elements that should be added
|
|
* @param offset where to start within the array
|
|
* @param length how many elements of the array should be added
|
|
* @return if the collection was modified
|
|
*/
|
|
public default boolean addAll(KEY_TYPE[] e, int offset, int length) {
|
|
if(length <= 0) return false;
|
|
SanityChecks.checkArrayCapacity(e.length, offset, length);
|
|
boolean added = false;
|
|
for(int i = 0;i<length;i++) {
|
|
if(add(e[offset+i])) added = true;
|
|
}
|
|
return added;
|
|
}
|
|
|
|
#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 removeAll function that reduces (un)boxing.
|
|
* It also notifies the remover of which exact element is going to be removed.
|
|
* @param c the collection of elements that should be removed
|
|
* @param r elements that got removed
|
|
* @return true if any element was removed
|
|
* @see Collection#removeAll(Collection)
|
|
*/
|
|
public boolean removeAll(COLLECTION KEY_GENERIC_TYPE c, CONSUMER KEY_GENERIC_TYPE r);
|
|
|
|
/**
|
|
* 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);
|
|
|
|
/**
|
|
* A Type-Specific retainAll function that reduces (un)boxing.
|
|
* It also notifies the remover of which exact element is going to be removed.
|
|
* @param c the collection of elements that should be kept
|
|
* @param r elements that got removed
|
|
* @return true if any element was removed
|
|
* @see Collection#retainAll(Collection)
|
|
*/
|
|
public boolean retainAll(COLLECTION KEY_GENERIC_TYPE c, CONSUMER KEY_GENERIC_TYPE r);
|
|
|
|
/**
|
|
* A Helper function to reduce the usage of Streams and allows to collect all elements
|
|
* @param collection that the elements should be inserted to
|
|
* @param <E> the collection type
|
|
* @return the input with the desired elements
|
|
*/
|
|
default <E extends COLLECTION KEY_GENERIC_TYPE> E pour(E collection) {
|
|
collection.addAll(this);
|
|
return collection;
|
|
}
|
|
|
|
/**
|
|
* A Function that does a shallow clone of the Collection itself.
|
|
* This function is more optimized then a copy constructor since the Collection does not have to be unsorted/resorted.
|
|
* It can be compared to Cloneable but with less exception risk
|
|
* @return a Shallow Copy of the collection
|
|
* @note Wrappers and view collections will not support this feature
|
|
*/
|
|
public COLLECTION KEY_GENERIC_TYPE copy();
|
|
|
|
#if TYPE_OBJECT
|
|
/**
|
|
* A Helper function that simplifies the process of creating a new Array.
|
|
* @param action the array creation function
|
|
* @param <E> the returning arrayType
|
|
* @return an array containing all of the elements in this collection
|
|
* @see Collection#toArray(Object[])
|
|
*/
|
|
default <E> E[] TO_ARRAY(IntFunction<E[]> action) {
|
|
return TO_ARRAY(action.apply(size()));
|
|
}
|
|
|
|
#else
|
|
/**
|
|
* 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}
|
|
* <p>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<? super CLASS_TYPE> 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.
|
|
* <p>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}
|
|
* <p>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}
|
|
* <p>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}
|
|
* <p>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();
|
|
|
|
/**
|
|
* Creates a Wrapped Collection that is Synchronized
|
|
* @return a new Collection that is synchronized
|
|
* @see COLLECTIONS#synchronize
|
|
*/
|
|
public default COLLECTION KEY_GENERIC_TYPE synchronize() { return COLLECTIONS.synchronize(this); }
|
|
|
|
/**
|
|
* Creates a Wrapped Collection that is Synchronized
|
|
* @param mutex is the controller of the synchronization block
|
|
* @return a new Collection Wrapper that is synchronized
|
|
* @see COLLECTIONS#synchronize
|
|
*/
|
|
public default COLLECTION KEY_GENERIC_TYPE synchronize(Object mutex) { return COLLECTIONS.synchronize(this, mutex); }
|
|
|
|
/**
|
|
* Creates a Wrapped Collection that is unmodifiable
|
|
* @return a new Collection Wrapper that is unmodifiable
|
|
* @see COLLECTIONS#unmodifiable
|
|
*/
|
|
public default COLLECTION KEY_GENERIC_TYPE unmodifiable() { return COLLECTIONS.unmodifiable(this); }
|
|
|
|
#if SPLIT_ITERATOR_FEATURE
|
|
#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
|
|
#if STREAM_FEATURE
|
|
/**
|
|
* 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); }
|
|
#endif
|
|
#endif
|
|
} |