001: /*
002: * Copyright (C) 2004 NNL Technology AB
003: * Visit www.infonode.net for information about InfoNode(R)
004: * products and how to contact NNL Technology AB.
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
019: * MA 02111-1307, USA.
020: */
021:
022: // $Id: ChangeNotifyList.java,v 1.4 2005/02/16 11:28:14 jesper Exp $
023: package net.infonode.util;
024:
025: import java.util.*;
026:
027: /**
028: * @author $Author: jesper $
029: * @version $Revision: 1.4 $
030: */
031: abstract public class ChangeNotifyList implements List {
032: private List list;
033:
034: abstract protected void changed();
035:
036: protected ChangeNotifyList() {
037: this (new ArrayList(2));
038: }
039:
040: protected ChangeNotifyList(List list) {
041: this .list = list;
042: }
043:
044: protected List getList() {
045: return list;
046: }
047:
048: public int size() {
049: return list.size();
050: }
051:
052: public boolean isEmpty() {
053: return list.isEmpty();
054: }
055:
056: public Object[] toArray() {
057: return list.toArray();
058: }
059:
060: public Object get(int index) {
061: return list.get(index);
062: }
063:
064: public int indexOf(Object o) {
065: return list.indexOf(o);
066: }
067:
068: public int lastIndexOf(Object o) {
069: return list.lastIndexOf(o);
070: }
071:
072: public boolean contains(Object o) {
073: return list.contains(o);
074: }
075:
076: public boolean containsAll(Collection c) {
077: return list.containsAll(c);
078: }
079:
080: public Iterator iterator() {
081: return listIterator();
082: }
083:
084: public ListIterator listIterator() {
085: return listIterator(0);
086: }
087:
088: public ListIterator listIterator(int index) {
089: return new ChangeIterator(list.listIterator(index));
090: }
091:
092: public Object[] toArray(Object[] a) {
093: return list.toArray(a);
094: }
095:
096: public void clear() {
097: list.clear();
098: changed();
099: }
100:
101: public Object remove(int index) {
102: Object result = list.remove(index);
103: changed();
104: return result;
105: }
106:
107: public void add(int index, Object element) {
108: list.add(index, element);
109: changed();
110: }
111:
112: public boolean add(Object o) {
113: if (list.add(o)) {
114: changed();
115: return true;
116: } else
117: return false;
118: }
119:
120: public boolean remove(Object o) {
121: if (list.remove(o)) {
122: changed();
123: return true;
124: } else
125: return false;
126: }
127:
128: public boolean addAll(int index, Collection c) {
129: if (list.addAll(index, c)) {
130: changed();
131: return true;
132: } else
133: return false;
134: }
135:
136: public boolean addAll(Collection c) {
137: if (list.addAll(c)) {
138: changed();
139: return true;
140: } else
141: return false;
142: }
143:
144: public boolean removeAll(Collection c) {
145: if (list.removeAll(c)) {
146: changed();
147: return true;
148: } else
149: return false;
150: }
151:
152: public boolean retainAll(Collection c) {
153: if (list.retainAll(c)) {
154: changed();
155: return true;
156: } else
157: return false;
158: }
159:
160: public List subList(int fromIndex, int toIndex) {
161: return new ChangeNotifyList(list.subList(fromIndex, toIndex)) {
162: protected void changed() {
163: ChangeNotifyList.this .changed();
164: }
165: };
166: }
167:
168: public Object set(int index, Object element) {
169: Object result = list.set(index, element);
170: changed();
171: return result;
172: }
173:
174: private class ChangeIterator implements ListIterator {
175: private ListIterator iterator;
176:
177: ChangeIterator(ListIterator iterator) {
178: this .iterator = iterator;
179: }
180:
181: public int nextIndex() {
182: return iterator.nextIndex();
183: }
184:
185: public int previousIndex() {
186: return iterator.previousIndex();
187: }
188:
189: public boolean hasPrevious() {
190: return iterator.hasPrevious();
191: }
192:
193: public Object previous() {
194: return iterator.previous();
195: }
196:
197: public void add(Object o) {
198: iterator.add(o);
199: changed();
200: }
201:
202: public void set(Object o) {
203: iterator.set(o);
204: changed();
205: }
206:
207: public void remove() {
208: iterator.remove();
209: changed();
210: }
211:
212: public boolean hasNext() {
213: return iterator.hasNext();
214: }
215:
216: public Object next() {
217: return iterator.next();
218: }
219: }
220: }
|