1 package spoon.support.util; 2 3 import java.util.Collection ; 4 import java.util.Comparator ; 5 import java.util.LinkedList ; 6 7 public class SortedList<E> extends LinkedList <E> { 8 9 private static final long serialVersionUID = 1L; 10 11 Comparator <? super E> comparator; 12 13 public SortedList(Comparator <? super E> comparator) { 14 super(); 15 this.comparator = comparator; 16 } 17 18 @Override 19 public boolean add(E o) { 20 for (E e : this) { 21 if (comparator.compare(o, e) < 0) { 22 add(indexOf(e), o); 23 return true; 24 } 25 } 26 return super.add(o); 27 } 28 29 @Override 30 public boolean addAll(Collection <? extends E> c) { 31 boolean ret = true; 32 for (E e : c) 33 ret &= add(e); 34 return ret; 35 } 36 37 public Comparator <? super E> getComparator() { 38 return comparator; 39 } 40 41 public void setComparator(Comparator <? super E> comparator) { 42 this.comparator = comparator; 43 } 44 45 } 46 | Popular Tags |