001: /*
002: * @(#)SortedList.java 1/10/2007
003: *
004: * Copyright 2002 - 2007 JIDE Software Inc. All rights reserved.
005: */
006:
007: package com.jidesoft.utils;
008:
009: import java.util.*;
010:
011: /**
012: * @author Patrick Gotthardt
013: */
014: public class SortedList<E> implements List<E> {
015: private Comparator<E> comparator;
016: private List<E> delegate;
017:
018: public SortedList(List<E> delegate, Comparator comparator) {
019: this .delegate = delegate;
020: this .comparator = comparator;
021: }
022:
023: public void add(int index, E element) {
024: // no indexed insertion supported
025: add(element);
026: }
027:
028: public boolean add(E o) {
029: int size = delegate.size();
030: for (int i = 0; i < size; i++) {
031: if (comparator.compare(o, delegate.get(i)) > 0) {
032: delegate.add(i, o);
033: return true;
034: }
035: }
036: delegate.add(o);
037: return true;
038: }
039:
040: public boolean addAll(Collection<? extends E> c) {
041: return delegate.addAll(c);
042: }
043:
044: public boolean addAll(int index, Collection<? extends E> c) {
045: return delegate.addAll(index, c);
046: }
047:
048: public void clear() {
049: delegate.clear();
050: }
051:
052: public boolean contains(Object o) {
053: return delegate.contains(o);
054: }
055:
056: public boolean containsAll(Collection<?> c) {
057: return delegate.containsAll(c);
058: }
059:
060: @Override
061: public boolean equals(Object o) {
062: return delegate.equals(o);
063: }
064:
065: public E get(int index) {
066: return delegate.get(index);
067: }
068:
069: @Override
070: public int hashCode() {
071: return delegate.hashCode();
072: }
073:
074: public int indexOf(Object o) {
075: return delegate.indexOf(o);
076: }
077:
078: public boolean isEmpty() {
079: return delegate.isEmpty();
080: }
081:
082: public Iterator<E> iterator() {
083: return delegate.iterator();
084: }
085:
086: public int lastIndexOf(Object o) {
087: return delegate.lastIndexOf(o);
088: }
089:
090: public ListIterator<E> listIterator() {
091: return delegate.listIterator();
092: }
093:
094: public ListIterator<E> listIterator(int index) {
095: return delegate.listIterator(index);
096: }
097:
098: public E remove(int index) {
099: return delegate.remove(index);
100: }
101:
102: public boolean remove(Object o) {
103: return delegate.remove(o);
104: }
105:
106: public boolean removeAll(Collection<?> c) {
107: return delegate.removeAll(c);
108: }
109:
110: public boolean retainAll(Collection<?> c) {
111: return delegate.retainAll(c);
112: }
113:
114: public E set(int index, E element) {
115: return delegate.set(index, element);
116: }
117:
118: public int size() {
119: return delegate.size();
120: }
121:
122: public List<E> subList(int fromIndex, int toIndex) {
123: return delegate.subList(fromIndex, toIndex);
124: }
125:
126: public Object[] toArray() {
127: return delegate.toArray();
128: }
129:
130: public Object[] toArray(Object[] a) {
131: return delegate.toArray(a);
132: }
133:
134: public static void main(String[] args) {
135: List<String> sortedList = new SortedList(new ArrayList(),
136: new Comparator<String>() {
137: public int compare(String o1, String o2) {
138: return o2.compareTo(o1);
139: }
140: });
141: sortedList.add("test");
142: sortedList.add("aaa");
143: sortedList.add("ddd");
144: sortedList.add("ccc");
145: for (String aSortedList : sortedList) {
146: String s = (String) aSortedList;
147: System.out.println(s);
148: }
149: }
150: }
|