blob: 974d3e5ba5a33e1bcd8c27b592fea9c70447614a (
plain) (
tree)
|
|
package de.animux.android.andmal.util;
import java.util.Collection;
import java.util.LinkedList;
import java.util.ListIterator;
public class SortedLinkedList<T extends Comparable<T>> extends LinkedList<T> {
private static final long serialVersionUID = 8263372892230475461L;
@Override
public boolean add(T object) {
ListIterator<T> it = listIterator();
while (it.hasNext()) {
if (object.compareTo(it.next()) > 0) {
break;
}
}
if (it.hasPrevious()) {
it.previous();
it.add(object);
}
else {
// insert at first position
listIterator().add(object);
}
return true;
}
@Override
public boolean addAll(Collection<? extends T> collection) {
for (T object : collection) {
add(object);
}
return true;
}
}
|