package speiger.src.collections.PACKAGE.lists; import java.util.Collection; import java.util.List; import java.util.ListIterator; import java.util.Objects; import speiger.src.collections.PACKAGE.collections.ABSTRACT_COLLECTION; import speiger.src.collections.PACKAGE.collections.COLLECTION; import speiger.src.collections.PACKAGE.collections.ITERATOR; /** * Abstract implementation of the {@link LIST} interface. */ public abstract class ABSTRACT_LIST KEY_GENERIC_TYPE extends ABSTRACT_COLLECTION KEY_GENERIC_TYPE implements LIST KEY_GENERIC_TYPE { #if !TYPE_OBJECT /** * A Type-Specific implementation of add function that delegates to {@link #add(int, KEY_TYPE)} */ @Override public boolean add(KEY_TYPE e) { add(size(), e); return true; } /** {@inheritDoc} *
This default implementation delegates to the corresponding type-specific function.
* @deprecated Please use the corresponding type-specific function instead.
*/
@Override
@Deprecated
public void add(int index, CLASS_TYPE element) {
add(index, OBJ_TO_KEY(element));
}
#endif
/**
* A Type-Specific implementation that iterates over the elements and adds them.
* @param c the elements that wants to be added
* @return true if the list was modified
*/
@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;
}
/**
* A Type-Specific implementation that iterates over the elements and adds them.
* @param c the elements that wants to be added
* @return true if the list was modified
*/
@Override
public boolean addAll(LIST KEY_GENERIC_TYPE c) {
boolean modified = false;
for(ITERATOR KEY_GENERIC_TYPE iter = c.iterator();iter.hasNext();modified |= add(iter.NEXT()));
return modified;
}
/**
* The IndexOf implementation iterates over all elements and compares them to the search value.
* @param o the value that the index is searched for.
* @return index of the value that was searched for. -1 if not found
* @deprecated it is highly suggested not to use this with Primitives because of boxing. But it is still supported because of ObjectComparason that are custom objects and allow to find the contents.
*/
@Override
@Primitive
public int indexOf(Object o) {
LIST_ITERATOR KEY_GENERIC_TYPE iter = listIterator();
#if TYPE_OBJECT
if(o == null) {
while(iter.hasNext()) {
if(iter.NEXT() == null)
return iter.previousIndex();
}
return -1;
}
#else
if(o == null) return -1;
#endif
while(iter.hasNext()) {
if(EQUALS_KEY_TYPE(iter.NEXT(), o))
return iter.previousIndex();
}
return -1;
}
/**
* The lastIndexOf implementation iterates over all elements and compares them to the search value.
* @param o the value that the index is searched for.
* @return the last index of the value that was searched for. -1 if not found
* @deprecated it is highly suggested not to use this with Primitives because of boxing. But it is still supported because of ObjectComparason that are custom objects and allow to find the contents.
*/
@Override
@Primitive
public int lastIndexOf(Object o) {
LIST_ITERATOR KEY_GENERIC_TYPE iter = listIterator(size());
#if TYPE_OBJECT
if(o == null) {
while(iter.hasPrevious()) {
if(iter.PREVIOUS() == null)
return iter.nextIndex();
}
return -1;
}
#else
if(o == null) return -1;
#endif
while(iter.hasPrevious()) {
if(EQUALS_KEY_TYPE(iter.PREVIOUS(), o))
return iter.nextIndex();
}
return -1;
}
#if !TYPE_OBJECT
/**
* The indexOf implementation iterates over all elements and compares them to the search value.
* @param e the value that the index is searched for.
* @return index of the value that was searched for. -1 if not found
*/
@Override
public int indexOf(KEY_TYPE e) {
LIST_ITERATOR KEY_GENERIC_TYPE iter = listIterator();
while(iter.hasNext()) {
if(KEY_EQUALS(iter.NEXT(), e))
return iter.previousIndex();
}
return -1;
}
/**
* The lastIndexOf implementation iterates over all elements and compares them to the search value.
* @param e the value that the index is searched for.
* @return the last index of the value that was searched for. -1 if not found
*/
@Override
public int lastIndexOf(KEY_TYPE e) {
LIST_ITERATOR KEY_GENERIC_TYPE iter = listIterator(size());
while(iter.hasPrevious()) {
if(KEY_EQUALS(iter.PREVIOUS(), e))
return iter.nextIndex();
}
return -1;
}
#endif
/**
* Compares if the list are the same.
*/
@Override
public boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof List))
return false;
List> l = (List>)o;
if(l.size() != size()) return false;
#if !TYPE_OBJECT
if(l instanceof LIST)
{
LIST_ITERATOR e1 = listIterator();
LIST_ITERATOR e2 = ((LIST)l).listIterator();
while (e1.hasNext() && e2.hasNext()) {
if(!(KEY_EQUALS(e1.NEXT(), e2.NEXT())))
return false;
}
return !(e1.hasNext() || e2.hasNext());
}
#endif
ListIterator