001: package com.sun.portal.desktop.util;
002:
003: import java.util.List;
004: import java.util.ArrayList;
005: import java.util.Iterator;
006: import java.util.Collection;
007: import java.util.ListIterator;
008:
009: public class SmartList implements List {
010: private List originalList = null;
011: private ThreadLocal listCopies = new ThreadLocal();
012:
013: private SmartList() {
014: // nothing
015: }
016:
017: public SmartList(List l) {
018: originalList = l;
019: }
020:
021: protected void copyIfNecessary() {
022: List l = getCopiedList();
023: if (l == null) {
024: l = new ArrayList(originalList);
025: listCopies.set(l);
026: }
027: }
028:
029: protected List getCopiedList() {
030: List l = (List) listCopies.get();
031: return l;
032: }
033:
034: public List getList() {
035: List l = getCopiedList();
036: if (l == null) {
037: l = originalList;
038: }
039:
040: return l;
041: }
042:
043: public void revert() {
044: listCopies.set(null);
045: }
046:
047: public void evolve() {
048: originalList = getList();
049: }
050:
051: public boolean isCopied() {
052: if (getCopiedList() == null) {
053: return false;
054: } else {
055: return true;
056: }
057: }
058:
059: public List cloneOriginalList() {
060: return new ArrayList(originalList);
061: }
062:
063: public int size() {
064: return getList().size();
065: }
066:
067: public boolean isEmpty() {
068: return getList().isEmpty();
069: }
070:
071: public boolean contains(Object o) {
072: return getList().contains(o);
073: }
074:
075: public Iterator iterator() {
076: return getList().iterator();
077: }
078:
079: public Object[] toArray() {
080: return getList().toArray();
081: }
082:
083: public Object[] toArray(Object a[]) {
084: return getList().toArray(a);
085: }
086:
087: public boolean add(Object o) {
088: copyIfNecessary();
089: return getList().add(o);
090: }
091:
092: public boolean remove(Object o) {
093: copyIfNecessary();
094: return getList().remove(o);
095: }
096:
097: public boolean containsAll(Collection c) {
098: copyIfNecessary();
099: return getList().containsAll(c);
100: }
101:
102: public boolean addAll(Collection c) {
103: copyIfNecessary();
104: return getList().addAll(c);
105: }
106:
107: public boolean addAll(int index, Collection c) {
108: copyIfNecessary();
109: return getList().addAll(index, c);
110: }
111:
112: public boolean removeAll(Collection c) {
113: copyIfNecessary();
114: return getList().removeAll(c);
115: }
116:
117: public boolean retainAll(Collection c) {
118: copyIfNecessary();
119: return getList().retainAll(c);
120: }
121:
122: public void clear() {
123: copyIfNecessary();
124: getList().clear();
125: }
126:
127: public int hashCode() {
128: return getList().hashCode();
129: }
130:
131: public Object get(int index) {
132: return getList().get(index);
133: }
134:
135: public Object set(int index, Object element) {
136: copyIfNecessary();
137: return getList().set(index, element);
138: }
139:
140: public void add(int index, Object element) {
141: copyIfNecessary();
142: getList().add(index, element);
143: }
144:
145: public Object remove(int index) {
146: copyIfNecessary();
147: return getList().remove(index);
148: }
149:
150: public int indexOf(Object o) {
151: return getList().indexOf(o);
152: }
153:
154: public int lastIndexOf(Object o) {
155: return getList().lastIndexOf(o);
156: }
157:
158: public ListIterator listIterator() {
159: return getList().listIterator();
160: }
161:
162: public ListIterator listIterator(int index) {
163: return getList().listIterator(index);
164: }
165:
166: public List subList(int fromIndex, int toIndex) {
167: return getList().subList(fromIndex, toIndex);
168: }
169:
170: public String toString() {
171: String s = getList().toString();
172: if (getCopiedList() != null) {
173: String id = Thread.currentThread().getName();
174: s += " (copied, id=" + id + ")";
175: } else {
176: s += " (original)";
177: }
178:
179: return s;
180: }
181: }
|