001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * Copyright (C) 2003 Vivid Solutions
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * For more information, contact:
022: *
023: * Vivid Solutions
024: * Suite #1A
025: * 2328 Government Street
026: * Victoria BC V8T 5G5
027: * Canada
028: *
029: * (250)385-6040
030: * www.vividsolutions.com
031: */
032: package com.vividsolutions.jump.util;
033:
034: import java.util.ArrayList;
035: import java.util.Collection;
036: import java.util.Collections;
037: import java.util.Iterator;
038: import java.util.List;
039: import java.util.ListIterator;
040:
041: public class SortedList implements List {
042:
043: private List list;
044:
045: /**
046: * Creates a UniqueList.
047: */
048: public SortedList() {
049: this (new ArrayList());
050: }
051:
052: /**
053: * Creates a SortedList backed by the given List.
054: * @param list a List that will be this SortedList's underlying List
055: */
056: public SortedList(List list) {
057: this .list = list;
058: }
059:
060: private void sort() {
061: Collections.sort(list);
062: }
063:
064: public int size() {
065: return list.size();
066: }
067:
068: public boolean isEmpty() {
069: return list.isEmpty();
070: }
071:
072: public boolean contains(Object o) {
073: return list.contains(o);
074: }
075:
076: public Iterator iterator() {
077: return list.iterator();
078: }
079:
080: public Object[] toArray() {
081: return list.toArray();
082: }
083:
084: public Object[] toArray(Object[] a) {
085: return list.toArray(a);
086: }
087:
088: public boolean add(Object o) {
089: try {
090: return list.add(o);
091: } finally {
092: sort();
093: }
094: }
095:
096: public boolean remove(Object o) {
097: try {
098: return list.remove(o);
099: } finally {
100: sort();
101: }
102: }
103:
104: public boolean containsAll(Collection c) {
105: return list.containsAll(c);
106: }
107:
108: public boolean addAll(Collection c) {
109: try {
110: return list.addAll(c);
111: } finally {
112: sort();
113: }
114: }
115:
116: public boolean addAll(int index, Collection c) {
117: try {
118: return list.addAll(index, c);
119: } finally {
120: sort();
121: }
122: }
123:
124: public boolean removeAll(Collection c) {
125: try {
126: return list.removeAll(c);
127: } finally {
128: sort();
129: }
130: }
131:
132: public boolean retainAll(Collection c) {
133: try {
134: return list.retainAll(c);
135: } finally {
136: sort();
137: }
138: }
139:
140: public void clear() {
141: list.clear();
142: }
143:
144: public Object get(int index) {
145: return list.get(index);
146: }
147:
148: public Object set(int index, Object element) {
149: try {
150: return list.set(index, element);
151: } finally {
152: sort();
153: }
154: }
155:
156: public void add(int index, Object element) {
157: try {
158: list.add(index, element);
159: } finally {
160: sort();
161: }
162: }
163:
164: public Object remove(int index) {
165: try {
166: return list.remove(index);
167: } finally {
168: sort();
169: }
170: }
171:
172: public int indexOf(Object o) {
173: return list.indexOf(o);
174: }
175:
176: public int lastIndexOf(Object o) {
177: return list.lastIndexOf(o);
178: }
179:
180: public ListIterator listIterator() {
181: return list.listIterator();
182: }
183:
184: public ListIterator listIterator(int index) {
185: return list.listIterator(index);
186: }
187:
188: public List subList(int fromIndex, int toIndex) {
189: //Not yet supported because we would need to track changes to the
190: //sublist because those changes modify the underlying list. [Jon Aquino]
191: throw new UnsupportedOperationException();
192: }
193:
194: }
|