2020-11-13 03:01:13 +01:00
package speiger.src.collections.PACKAGE.collections;
2020-11-16 02:55:33 +01:00
import java.util.Collection;
2020-12-06 09:32:22 +01:00
import java.util.Objects;
2020-11-13 03:01:13 +01:00
import java.util.AbstractCollection;
2020-11-26 22:56:15 +01:00
import speiger.src.collections.PACKAGE.collections.COLLECTION;
2020-11-16 02:55:33 +01:00
#if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.utils.ITERATORS;
#endif
2020-11-13 03:01:13 +01:00
public abstract class ABSTRACT_COLLECTION KEY_GENERIC_TYPE extends AbstractCollection<CLASS_TYPE> implements COLLECTION KEY_GENERIC_TYPE
{
@Override
public abstract ITERATOR KEY_GENERIC_TYPE iterator();
2020-11-16 02:55:33 +01:00
#if !TYPE_OBJECT
2020-12-06 09:32:22 +01:00
/** {@inheritDoc}
2020-12-07 08:55:22 +01:00
* <p>This default implementation delegates to the corresponding type-specific function.
* @deprecated Please use the corresponding type-specific function instead.
2020-12-06 09:32:22 +01:00
*/
2020-11-13 03:01:13 +01:00
@Override
@Deprecated
public boolean add(CLASS_TYPE e) { return COLLECTION.super.add(e); }
2020-11-26 22:56:15 +01:00
#endif
2020-11-13 03:01:13 +01:00
@Override
2020-11-26 22:56:15 +01:00
public boolean addAll(COLLECTION KEY_GENERIC_TYPE c) {
2020-11-13 03:01:13 +01:00
boolean modified = false;
2021-01-20 04:29:08 +01:00
for(ITERATOR KEY_GENERIC_TYPE iter = c.iterator();iter.hasNext();modified |= add(iter.NEXT()));
2020-11-13 03:01:13 +01:00
return modified;
}
2020-11-26 22:56:15 +01:00
#if !TYPE_OBJECT
2020-12-06 09:32:22 +01:00
/** {@inheritDoc}
2020-12-07 08:55:22 +01:00
* <p>This default implementation delegates to the corresponding type-specific function.
* @deprecated Please use the corresponding type-specific function instead.
2020-12-06 09:32:22 +01:00
*/
2020-11-13 03:01:13 +01:00
@Override
@Deprecated
public boolean contains(Object e) { return COLLECTION.super.contains(e); }
2020-12-06 09:32:22 +01:00
/**
* A Type-Specific implementation of contains. This implementation iterates over the elements and returns true if the value match.
2021-04-22 23:02:04 +02:00
* @param e the element that should be searched for.
2020-12-06 09:32:22 +01:00
* @return true if the value was found.
*/
2020-11-13 03:01:13 +01:00
@Override
public boolean contains(KEY_TYPE e) {
2021-01-21 21:35:23 +01:00
for(ITERATOR KEY_GENERIC_TYPE iter = iterator();iter.hasNext();) { if(KEY_EQUALS(iter.NEXT(), e)) return true; }
2020-11-13 03:01:13 +01:00
return false;
}
2020-12-01 02:43:13 +01:00
#endif
2020-12-06 09:32:22 +01:00
/** {@inheritDoc}
2020-12-07 08:55:22 +01:00
* <p>This default implementation delegates to the corresponding type-specific function.
* @deprecated Please use the corresponding type-specific function instead.
2020-12-06 09:32:22 +01:00
*/
2020-11-16 02:55:33 +01:00
@Override
2020-11-30 00:11:40 +01:00
@Deprecated
2020-11-16 02:55:33 +01:00
public boolean addAll(Collection<? extends CLASS_TYPE> c)
{
2020-12-01 02:43:13 +01:00
return c instanceof COLLECTION ? addAll((COLLECTION KEY_GENERIC_TYPE)c) : super.addAll(c);
2020-11-16 02:55:33 +01:00
}
2020-12-06 09:32:22 +01:00
/**
* A Type-Specific implementation of containsAll. This implementation iterates over all elements and checks all elements are present in the other collection.
2021-04-22 23:02:04 +02:00
* @param c the collection that should be checked if it contains all elements.
2020-12-06 09:32:22 +01:00
* @return true if all elements were found in the collection
2021-04-22 23:02:04 +02:00
* @throws java.lang.NullPointerException if the collection is null
2020-12-06 09:32:22 +01:00
*/
2020-11-13 03:01:13 +01:00
@Override
2020-12-01 02:43:13 +01:00
public boolean containsAll(COLLECTION KEY_GENERIC_TYPE c) {
2020-12-06 09:32:22 +01:00
Objects.requireNonNull(c);
2021-01-20 04:29:08 +01:00
for(ITERATOR KEY_GENERIC_TYPE iter = c.iterator();iter.hasNext();)
if(!contains(iter.NEXT()))
2020-11-13 03:01:13 +01:00
return false;
return true;
}
2020-12-06 09:32:22 +01:00
/**
* This implementation iterates over the elements of the collection and checks if they are stored in this collection
* @param c the elements that should be checked for
* @return true if any element is in this collection
* @deprecated if this is a primitive collection
2021-04-22 23:02:04 +02:00
* @throws java.lang.NullPointerException if the collection is null
2020-12-06 09:32:22 +01:00
*/
2020-11-30 00:11:40 +01:00
@Override
@Primitive
public boolean containsAny(Collection<?> c) {
2020-12-06 09:32:22 +01:00
Objects.requireNonNull(c);
2021-01-08 21:12:20 +01:00
for(Object e : c)
if(contains(e))
2020-11-30 00:11:40 +01:00
return true;
return false;
}
2020-12-06 09:32:22 +01:00
/**
* This implementation iterates over the elements of the collection and checks if they are stored in this collection.
* @param c the elements that should be checked for
* @return true if any element is in this collection
2021-04-22 23:02:04 +02:00
* @throws java.lang.NullPointerException if the collection is null
2020-12-06 09:32:22 +01:00
*/
2020-12-01 02:43:13 +01:00
@Override
public boolean containsAny(COLLECTION KEY_GENERIC_TYPE c) {
2020-12-06 09:32:22 +01:00
Objects.requireNonNull(c);
2021-01-20 04:29:08 +01:00
for(ITERATOR KEY_GENERIC_TYPE iter = c.iterator();iter.hasNext();)
if(contains(iter.NEXT()))
2020-12-01 02:43:13 +01:00
return true;
return false;
}
2020-11-30 00:11:40 +01:00
#if !TYPE_OBJECT
2020-12-06 09:32:22 +01:00
/** {@inheritDoc}
2020-12-07 08:55:22 +01:00
* <p>This default implementation delegates to the corresponding type-specific function.
* @deprecated Please use the corresponding type-specific function instead.
2020-12-06 09:32:22 +01:00
*/
2020-11-13 03:01:13 +01:00
@Override
@Deprecated
public boolean remove(Object e) { return COLLECTION.super.remove(e); }
2020-12-06 09:32:22 +01:00
/**
* A Type-Specific implementation of remove. This implementation iterates over the elements until it finds the element that is searched for or it runs out of elements.
* It stops after finding the first element
* @param e the element that is searched for
* @return true if the element was found and removed.
*/
2020-11-16 02:55:33 +01:00
@Override
public boolean REMOVE_KEY(KEY_TYPE e) {
2020-12-01 02:43:13 +01:00
for(ITERATOR KEY_GENERIC_TYPE iter = iterator();iter.hasNext();) {
2021-01-21 21:35:23 +01:00
if(KEY_EQUALS(iter.NEXT(), e)) {
2020-11-16 02:55:33 +01:00
iter.remove();
return true;
}
}
return false;
}
2020-12-01 02:43:13 +01:00
#endif
2020-12-06 09:32:22 +01:00
/**
* A Type-Specific implementation of removeAll. This Implementation iterates over all elements and removes them as they were found in the other collection.
* @param c the elements that should be deleted
* @return true if the collection was modified.
2021-04-22 23:02:04 +02:00
* @throws java.lang.NullPointerException if the collection is null
2020-12-06 09:32:22 +01:00
*/
2020-11-13 03:01:13 +01:00
@Override
2020-12-01 02:43:13 +01:00
public boolean removeAll(COLLECTION KEY_GENERIC_TYPE c) {
2020-12-06 09:32:22 +01:00
Objects.requireNonNull(c);
2020-11-13 03:01:13 +01:00
boolean modified = false;
2020-12-01 02:43:13 +01:00
for(ITERATOR KEY_GENERIC_TYPE iter = iterator();iter.hasNext();) {
2020-11-13 03:01:13 +01:00
if(c.contains(iter.NEXT())) {
iter.remove();
modified = true;
}
}
return modified;
}
2020-12-06 09:32:22 +01:00
/**
* A Type-Specific implementation of retainAll. This Implementation iterates over all elements and removes them as they were not found in the other collection.
* @param c the elements that should be kept
* @return true if the collection was modified.
2021-04-22 23:02:04 +02:00
* @throws java.lang.NullPointerException if the collection is null
2020-12-06 09:32:22 +01:00
*/
2020-11-13 03:01:13 +01:00
@Override
2020-12-01 02:43:13 +01:00
public boolean retainAll(COLLECTION KEY_GENERIC_TYPE c) {
2020-12-06 09:32:22 +01:00
Objects.requireNonNull(c);
2021-01-08 21:12:20 +01:00
if(c.isEmpty()) {
boolean modified = !isEmpty();
clear();
return modified;
}
2020-11-13 03:01:13 +01:00
boolean modified = false;
2020-12-01 02:43:13 +01:00
for(ITERATOR KEY_GENERIC_TYPE iter = iterator();iter.hasNext();) {
2020-11-13 03:01:13 +01:00
if(!c.contains(iter.NEXT())) {
iter.remove();
modified = true;
}
}
return modified;
}
2020-11-16 02:55:33 +01:00
2020-12-01 02:43:13 +01:00
#if !TYPE_OBJECT
2020-12-06 09:32:22 +01:00
/**
* A Type-Specific implementation of toArray that links to {@link #TO_ARRAY(KEY_TYPE[])} with a newly created array.
* @return an array containing all of the elements in this collection
*/
2020-11-30 00:11:40 +01:00
@Override
2020-11-16 02:55:33 +01:00
public KEY_TYPE[] TO_ARRAY() {
return TO_ARRAY(new KEY_TYPE[size()]);
}
2020-12-06 09:32:22 +01:00
/**
* A Type-Specific implementation of toArray. This implementation iterates over all elements and unwraps them into primitive type.
* @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
*/
2020-11-30 00:11:40 +01:00
@Override
2020-11-16 02:55:33 +01:00
public KEY_TYPE[] TO_ARRAY(KEY_TYPE[] a) {
if(a == null || a.length < size()) a = new KEY_TYPE[size()];
ITERATORS.unwrap(a, iterator());
return a;
}
2020-11-13 03:01:13 +01:00
#endif
}