001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software 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 GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ha.framework.interfaces;
023:
024: import java.io.ObjectStreamException;
025: import java.util.ArrayList;
026: import java.util.Collection;
027: import java.util.Iterator;
028: import java.util.List;
029: import java.util.ListIterator;
030:
031: /**
032: * Subclasses ArrayList but throws an UnsupportedOperationException from
033: * any method that would change the internal data members. Any iterators
034: * that are returned do the same.
035: *
036: * All other methods are delegated to the ArrayList that is passed to the
037: * constructor, so creating an instance of this class does not result in
038: * making a copy of the internal element array of the source array list.
039: *
040: * @author <a href="brian.stansberry@jboss.com">Brian Stansberry</a>
041: * @version $Revision: 1.1 $
042: */
043: public class ImmutableArrayList extends ArrayList {
044: /** The serialVersionUID */
045: private static final long serialVersionUID = -7080841600873898901L;
046:
047: private ArrayList delegate;
048:
049: ImmutableArrayList(ArrayList source) {
050: this .delegate = source;
051: }
052:
053: // Delegated Methods
054:
055: public Object clone() {
056: return delegate.clone();
057: }
058:
059: public boolean contains(Object elem) {
060: return delegate.contains(elem);
061: }
062:
063: public Object get(int index) {
064: return delegate.get(index);
065: }
066:
067: public int indexOf(Object elem) {
068: return delegate.indexOf(elem);
069: }
070:
071: public boolean isEmpty() {
072: return delegate.isEmpty();
073: }
074:
075: public int lastIndexOf(Object elem) {
076: return delegate.lastIndexOf(elem);
077: }
078:
079: public int size() {
080: return delegate.size();
081: }
082:
083: public Object[] toArray() {
084: return delegate.toArray();
085: }
086:
087: public Object[] toArray(Object[] a) {
088: return delegate.toArray(a);
089: }
090:
091: public boolean equals(Object o) {
092: return delegate.equals(o);
093: }
094:
095: public int hashCode() {
096: return delegate.hashCode();
097: }
098:
099: public List subList(int fromIndex, int toIndex) {
100: return delegate.subList(fromIndex, toIndex);
101: }
102:
103: public boolean containsAll(Collection c) {
104: return delegate.containsAll(c);
105: }
106:
107: public String toString() {
108: return delegate.toString();
109: }
110:
111: // Immutable Methods
112:
113: public void add(int arg0, Object arg1) {
114: throw new UnsupportedOperationException(
115: "Target list is immutable; mutator methods are not supported");
116: }
117:
118: public boolean add(Object arg0) {
119: throw new UnsupportedOperationException(
120: "Target list is immutable; mutator methods are not supported");
121: }
122:
123: public boolean addAll(Collection arg0) {
124: throw new UnsupportedOperationException(
125: "Target list is immutable; mutator methods are not supported");
126: }
127:
128: public boolean addAll(int arg0, Collection arg1) {
129: throw new UnsupportedOperationException(
130: "Target list is immutable; mutator methods are not supported");
131: }
132:
133: public void clear() {
134: throw new UnsupportedOperationException(
135: "Target list is immutable; mutator methods are not supported");
136: }
137:
138: public void ensureCapacity(int arg0) {
139: throw new UnsupportedOperationException(
140: "Target list is immutable; mutator methods are not supported");
141: }
142:
143: public Object remove(int arg0) {
144: throw new UnsupportedOperationException(
145: "Target list is immutable; mutator methods are not supported");
146: }
147:
148: public boolean remove(Object arg0) {
149: throw new UnsupportedOperationException(
150: "Target list is immutable; mutator methods are not supported");
151: }
152:
153: protected void removeRange(int arg0, int arg1) {
154: throw new UnsupportedOperationException(
155: "Target list is immutable; mutator methods are not supported");
156: }
157:
158: public Object set(int arg0, Object arg1) {
159: throw new UnsupportedOperationException(
160: "Target list is immutable; mutator methods are not supported");
161: }
162:
163: public void trimToSize() {
164: throw new UnsupportedOperationException(
165: "Target list is immutable; mutator methods are not supported");
166: }
167:
168: public Iterator iterator() {
169: return new ImmutableArrayListIterator(super .listIterator());
170: }
171:
172: public ListIterator listIterator() {
173: return new ImmutableArrayListIterator(super .listIterator());
174: }
175:
176: public ListIterator listIterator(int index) {
177: return new ImmutableArrayListIterator(super .listIterator(index));
178: }
179:
180: public boolean removeAll(Collection arg0) {
181: throw new UnsupportedOperationException(
182: "Target list is immutable; mutator methods are not supported");
183: }
184:
185: public boolean retainAll(Collection arg0) {
186: throw new UnsupportedOperationException(
187: "Target list is immutable; mutator methods are not supported");
188: }
189:
190: // Serialization
191:
192: private Object writeReplace() throws ObjectStreamException {
193: return delegate;
194: }
195:
196: // Inner Classes
197:
198: private class ImmutableArrayListIterator implements ListIterator {
199:
200: private ListIterator delegate;
201:
202: ImmutableArrayListIterator(ListIterator delegate) {
203: this .delegate = delegate;
204: }
205:
206: public boolean hasNext() {
207: return delegate.hasNext();
208: }
209:
210: public Object next() {
211: return delegate.next();
212: }
213:
214: public void remove() {
215: throw new UnsupportedOperationException(
216: "Target list is immutable; mutator methods are not supported");
217:
218: }
219:
220: public void add(Object o) {
221: throw new UnsupportedOperationException(
222: "Target list is immutable; mutator methods are not supported");
223: }
224:
225: public boolean hasPrevious() {
226: return delegate.hasPrevious();
227: }
228:
229: public int nextIndex() {
230: return delegate.nextIndex();
231: }
232:
233: public Object previous() {
234: return delegate.previous();
235: }
236:
237: public int previousIndex() {
238: return delegate.previousIndex();
239: }
240:
241: public void set(Object o) {
242: throw new UnsupportedOperationException(
243: "Target list is immutable; mutator methods are not supported");
244: }
245:
246: }
247:
248: }
|