001: /*
002: * Copyright 2000-2003 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.corba.se.impl.ior;
027:
028: import java.util.Collection;
029: import java.util.List;
030: import java.util.AbstractList;
031: import java.util.ListIterator;
032: import java.util.Iterator;
033:
034: import com.sun.corba.se.spi.ior.MakeImmutable;
035:
036: /** Simple class that delegates all List operations to
037: * another list. It also can be frozen, which means that
038: * a number of operations can be performed on the list,
039: * and then the list can be made immutable, so that no
040: * further changes are possible. A FreezableList is frozen
041: * using the makeImmutable method.
042: */
043: public class FreezableList extends AbstractList {
044: private List delegate = null;
045: private boolean immutable = false;
046:
047: public boolean equals(Object obj) {
048: if (obj == null)
049: return false;
050:
051: if (!(obj instanceof FreezableList))
052: return false;
053:
054: FreezableList other = (FreezableList) obj;
055:
056: return delegate.equals(other.delegate)
057: && (immutable == other.immutable);
058: }
059:
060: public int hashCode() {
061: return delegate.hashCode();
062: }
063:
064: public FreezableList(List delegate, boolean immutable) {
065: this .delegate = delegate;
066: this .immutable = immutable;
067: }
068:
069: public FreezableList(List delegate) {
070: this (delegate, false);
071: }
072:
073: public void makeImmutable() {
074: immutable = true;
075: }
076:
077: public boolean isImmutable() {
078: return immutable;
079: }
080:
081: public void makeElementsImmutable() {
082: Iterator iter = iterator();
083: while (iter.hasNext()) {
084: Object obj = iter.next();
085: if (obj instanceof MakeImmutable) {
086: MakeImmutable element = (MakeImmutable) obj;
087: element.makeImmutable();
088: }
089: }
090: }
091:
092: // Methods overridden from AbstractList
093:
094: public int size() {
095: return delegate.size();
096: }
097:
098: public Object get(int index) {
099: return delegate.get(index);
100: }
101:
102: public Object set(int index, Object element) {
103: if (immutable)
104: throw new UnsupportedOperationException();
105:
106: return delegate.set(index, element);
107: }
108:
109: public void add(int index, Object element) {
110: if (immutable)
111: throw new UnsupportedOperationException();
112:
113: delegate.add(index, element);
114: }
115:
116: public Object remove(int index) {
117: if (immutable)
118: throw new UnsupportedOperationException();
119:
120: return delegate.remove(index);
121: }
122:
123: // We also override subList so that the result is a FreezableList.
124: public List subList(int fromIndex, int toIndex) {
125: List list = delegate.subList(fromIndex, toIndex);
126: List result = new FreezableList(list, immutable);
127: return result;
128: }
129: }
|