01: /*
02: * Spoon - http://spoon.gforge.inria.fr/
03: * Copyright (C) 2006 INRIA Futurs <renaud.pawlak@inria.fr>
04: *
05: * This software is governed by the CeCILL-C License under French law and
06: * abiding by the rules of distribution of free software. You can use, modify
07: * and/or redistribute the software under the terms of the CeCILL-C license as
08: * circulated by CEA, CNRS and INRIA at http://www.cecill.info.
09: *
10: * This program is distributed in the hope that it will be useful, but WITHOUT
11: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12: * FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details.
13: *
14: * The fact that you are presently reading this means that you have had
15: * knowledge of the CeCILL-C license and that you accept its terms.
16: */
17:
18: package spoon.support.util;
19:
20: import java.util.Collection;
21: import java.util.Comparator;
22: import java.util.LinkedList;
23:
24: public class SortedList<E> extends LinkedList<E> {
25:
26: private static final long serialVersionUID = 1L;
27:
28: Comparator<? super E> comparator;
29:
30: public SortedList(Comparator<? super E> comparator) {
31: super ();
32: this .comparator = comparator;
33: }
34:
35: @Override
36: public boolean add(E o) {
37: for (E e : this ) {
38: if (comparator.compare(o, e) < 0) {
39: add(indexOf(e), o);
40: return true;
41: }
42: }
43: return super .add(o);
44: }
45:
46: @Override
47: public boolean addAll(Collection<? extends E> c) {
48: boolean ret = true;
49: for (E e : c)
50: ret &= add(e);
51: return ret;
52: }
53:
54: public Comparator<? super E> getComparator() {
55: return comparator;
56: }
57:
58: public void setComparator(Comparator<? super E> comparator) {
59: this.comparator = comparator;
60: }
61:
62: }
|