KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > spoon > support > util > SortedList


1 package spoon.support.util;
2
3 import java.util.Collection JavaDoc;
4 import java.util.Comparator JavaDoc;
5 import java.util.LinkedList JavaDoc;
6
7 public class SortedList<E> extends LinkedList JavaDoc<E> {
8
9     private static final long serialVersionUID = 1L;
10
11     Comparator JavaDoc<? super E> comparator;
12
13     public SortedList(Comparator JavaDoc<? super E> comparator) {
14         super();
15         this.comparator = comparator;
16     }
17
18     @Override JavaDoc
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 JavaDoc
30     public boolean addAll(Collection JavaDoc<? extends E> c) {
31         boolean ret = true;
32         for (E e : c)
33             ret &= add(e);
34         return ret;
35     }
36
37     public Comparator JavaDoc<? super E> getComparator() {
38         return comparator;
39     }
40
41     public void setComparator(Comparator JavaDoc<? super E> comparator) {
42         this.comparator = comparator;
43     }
44
45 }
46
Popular Tags