package speiger.src.collections.PACKAGE.collections; import java.util.Collection; import java.util.Objects; import java.util.AbstractCollection; import speiger.src.collections.PACKAGE.collections.COLLECTION; #if !TYPE_OBJECT import speiger.src.collections.PACKAGE.utils.ITERATORS; #endif public abstract class ABSTRACT_COLLECTION KEY_GENERIC_TYPE extends AbstractCollection implements COLLECTION KEY_GENERIC_TYPE { @Override public abstract ITERATOR KEY_GENERIC_TYPE iterator(); #if !TYPE_OBJECT /** {@inheritDoc} *

This default implementation delegates to the corresponding type-specific function. * @deprecated Please use the corresponding type-specific function instead. */ @Override @Deprecated public boolean add(CLASS_TYPE e) { return COLLECTION.super.add(e); } #endif @Override public boolean addAll(COLLECTION KEY_GENERIC_TYPE c) { boolean modified = false; for(ITERATOR KEY_GENERIC_TYPE iter = c.iterator();iter.hasNext();modified |= add(iter.NEXT())); return modified; } #if !TYPE_OBJECT /** {@inheritDoc} *

This default implementation delegates to the corresponding type-specific function. * @deprecated Please use the corresponding type-specific function instead. */ @Override @Deprecated public boolean contains(Object e) { return COLLECTION.super.contains(e); } /** * A Type-Specific implementation of contains. This implementation iterates over the elements and returns true if the value match. * @param e the element that should be searched for. * @return true if the value was found. */ @Override public boolean contains(KEY_TYPE e) { for(ITERATOR KEY_GENERIC_TYPE iter = iterator();iter.hasNext();) { if(KEY_EQUALS(iter.NEXT(), e)) return true; } return false; } #endif /** {@inheritDoc} *

This default implementation delegates to the corresponding type-specific function. * @deprecated Please use the corresponding type-specific function instead. */ @Override @Deprecated public boolean addAll(Collection c) { return c instanceof COLLECTION ? addAll((COLLECTION KEY_GENERIC_TYPE)c) : super.addAll(c); } /** * A Type-Specific implementation of containsAll. This implementation iterates over all elements and checks all elements are present in the other collection. * @param c the collection that should be checked if it contains all elements. * @return true if all elements were found in the collection * @throws java.lang.NullPointerException if the collection is null */ @Override public boolean containsAll(COLLECTION KEY_GENERIC_TYPE c) { Objects.requireNonNull(c); for(ITERATOR KEY_GENERIC_TYPE iter = c.iterator();iter.hasNext();) if(!contains(iter.NEXT())) return false; return true; } /** * 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 * @throws java.lang.NullPointerException if the collection is null */ @Override @Primitive public boolean containsAny(Collection c) { Objects.requireNonNull(c); for(Object e : c) if(contains(e)) return true; return false; } /** * 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 * @throws java.lang.NullPointerException if the collection is null */ @Override public boolean containsAny(COLLECTION KEY_GENERIC_TYPE c) { Objects.requireNonNull(c); for(ITERATOR KEY_GENERIC_TYPE iter = c.iterator();iter.hasNext();) if(contains(iter.NEXT())) return true; return false; } #if !TYPE_OBJECT /** {@inheritDoc} *

This default implementation delegates to the corresponding type-specific function. * @deprecated Please use the corresponding type-specific function instead. */ @Override @Deprecated public boolean remove(Object e) { return COLLECTION.super.remove(e); } /** * 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. */ @Override public boolean REMOVE_KEY(KEY_TYPE e) { for(ITERATOR KEY_GENERIC_TYPE iter = iterator();iter.hasNext();) { if(KEY_EQUALS(iter.NEXT(), e)) { iter.remove(); return true; } } return false; } #endif /** * 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. * @throws java.lang.NullPointerException if the collection is null */ @Override public boolean removeAll(COLLECTION KEY_GENERIC_TYPE c) { Objects.requireNonNull(c); boolean modified = false; for(ITERATOR KEY_GENERIC_TYPE iter = iterator();iter.hasNext();) { if(c.contains(iter.NEXT())) { iter.remove(); modified = true; } } return modified; } /** * 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. * @throws java.lang.NullPointerException if the collection is null */ @Override public boolean retainAll(COLLECTION KEY_GENERIC_TYPE c) { Objects.requireNonNull(c); if(c.isEmpty()) { boolean modified = !isEmpty(); clear(); return modified; } boolean modified = false; for(ITERATOR KEY_GENERIC_TYPE iter = iterator();iter.hasNext();) { if(!c.contains(iter.NEXT())) { iter.remove(); modified = true; } } return modified; } #if !TYPE_OBJECT /** * 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 */ @Override public KEY_TYPE[] TO_ARRAY() { return TO_ARRAY(new KEY_TYPE[size()]); } /** * 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 */ @Override 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; } #endif }