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: /**
042: * Can't add, replace, or remove the first element in the list.
043: */
044: public class ImmutableFirstElementList implements List {
045:
046: private List list = new ArrayList();
047:
048: public ImmutableFirstElementList(Object firstElement) {
049: list.add(firstElement);
050: }
051:
052: public int size() {
053: return list.size();
054: }
055:
056: public boolean isEmpty() {
057: return list.isEmpty();
058: }
059:
060: public boolean contains(Object o) {
061: return list.contains(o);
062: }
063:
064: public Iterator iterator() {
065: //Prevent Iterator#remove. [Jon Aquino]
066: return Collections.unmodifiableList(list).iterator();
067: }
068:
069: public Object[] toArray() {
070: return list.toArray();
071: }
072:
073: public Object[] toArray(Object[] a) {
074: return list.toArray(a);
075: }
076:
077: public boolean add(Object o) {
078: return list.add(o);
079: }
080:
081: public boolean remove(Object o) {
082: //An element equal to the first element may exist later in the list. [Jon Aquino]
083: return list.subList(1, list.size()).remove(o);
084: }
085:
086: public boolean containsAll(Collection c) {
087: return list.containsAll(c);
088: }
089:
090: public boolean addAll(Collection c) {
091: return list.addAll(c);
092: }
093:
094: public boolean addAll(int index, Collection c) {
095: return list.addAll(index == 0 ? 1 : index, c);
096: }
097:
098: public boolean removeAll(Collection c) {
099: return list.subList(1, list.size()).remove(c);
100: }
101:
102: public boolean retainAll(Collection c) {
103: return list.subList(1, list.size()).retainAll(c);
104: }
105:
106: public void clear() {
107: list.subList(1, list.size()).clear();
108: }
109:
110: public Object get(int index) {
111: return list.get(index);
112: }
113:
114: public Object set(int index, Object element) {
115: if (index == 0) {
116: return get(0);
117: }
118: return list.set(index, element);
119: }
120:
121: public void add(int index, Object element) {
122: list.add(index == 0 ? 1 : index, element);
123: }
124:
125: public Object remove(int index) {
126: if (index == 0) {
127: return get(0);
128: }
129: return list.remove(index);
130: }
131:
132: public int indexOf(Object o) {
133: return list.indexOf(o);
134: }
135:
136: public int lastIndexOf(Object o) {
137: return list.lastIndexOf(o);
138: }
139:
140: public ListIterator listIterator() {
141: //Prevent Iterator#remove. [Jon Aquino]
142: return Collections.unmodifiableList(list).listIterator();
143: }
144:
145: public ListIterator listIterator(int index) {
146: //Prevent Iterator#remove. [Jon Aquino]
147: return Collections.unmodifiableList(list).listIterator(index);
148: }
149:
150: public List subList(int fromIndex, int toIndex) {
151: if (fromIndex > 0) {
152: return list.subList(fromIndex, toIndex);
153: }
154: //A bit heavy-handed, but we don't want the first element to be
155: //modified, moved, or removed. Future: allow the other elements
156: //to be modified, moved, or removed. [Jon Aquino]
157: return Collections.unmodifiableList(list).subList(fromIndex,
158: toIndex);
159: }
160:
161: }
|