FreezableList.java revision 608:7e06bf1dcb09
1248590Smm/* 2248590Smm * Copyright (c) 2000, 2003, Oracle and/or its affiliates. All rights reserved. 3248590Smm * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4248590Smm * 5248590Smm * This code is free software; you can redistribute it and/or modify it 6248590Smm * under the terms of the GNU General Public License version 2 only, as 7248590Smm * published by the Free Software Foundation. Oracle designates this 8248590Smm * particular file as subject to the "Classpath" exception as provided 9248590Smm * by Oracle in the LICENSE file that accompanied this code. 10248590Smm * 11248590Smm * This code is distributed in the hope that it will be useful, but WITHOUT 12248590Smm * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13248590Smm * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14248590Smm * version 2 for more details (a copy is included in the LICENSE file that 15248590Smm * accompanied this code). 16248590Smm * 17248590Smm * You should have received a copy of the GNU General Public License version 18248590Smm * 2 along with this work; if not, write to the Free Software Foundation, 19248590Smm * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20248590Smm * 21248590Smm * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22248590Smm * or visit www.oracle.com if you need additional information or have any 23248590Smm * questions. 24248590Smm */ 25248590Smm 26248590Smmpackage com.sun.corba.se.impl.ior ; 27248590Smm 28248590Smmimport java.util.Collection ; 29248590Smmimport java.util.List ; 30248590Smmimport java.util.AbstractList ; 31248590Smmimport java.util.ListIterator ; 32248590Smmimport java.util.Iterator ; 33248590Smm 34248590Smmimport com.sun.corba.se.spi.ior.MakeImmutable ; 35248590Smm 36248590Smm/** Simple class that delegates all List operations to 37248590Smm* another list. It also can be frozen, which means that 38248590Smm* a number of operations can be performed on the list, 39248590Smm* and then the list can be made immutable, so that no 40248590Smm* further changes are possible. A FreezableList is frozen 41248590Smm* using the makeImmutable method. 42248590Smm*/ 43248590Smmpublic class FreezableList extends AbstractList { 44248590Smm private List delegate = null ; 45248590Smm private boolean immutable = false ; 46248590Smm 47248590Smm public boolean equals( Object obj ) 48248590Smm { 49248590Smm if (obj == null) 50248590Smm return false ; 51248590Smm 52248590Smm if (!(obj instanceof FreezableList)) 53248590Smm return false ; 54248590Smm 55248590Smm FreezableList other = (FreezableList)obj ; 56248590Smm 57248590Smm return delegate.equals( other.delegate ) && 58248590Smm (immutable == other.immutable) ; 59248590Smm } 60248590Smm 61248590Smm public int hashCode() 62248590Smm { 63248590Smm return delegate.hashCode() ; 64248590Smm } 65248590Smm 66248590Smm public FreezableList( List delegate, boolean immutable ) 67248590Smm { 68248590Smm this.delegate = delegate ; 69248590Smm this.immutable = immutable ; 70248590Smm } 71248590Smm 72248590Smm public FreezableList( List delegate ) 73248590Smm { 74248590Smm this( delegate, false ) ; 75248590Smm } 76248590Smm 77248590Smm public void makeImmutable() 78248590Smm { 79248590Smm immutable = true ; 80248590Smm } 81248590Smm 82248590Smm public boolean isImmutable() 83248590Smm { 84248590Smm return immutable ; 85248590Smm } 86248590Smm 87248590Smm public void makeElementsImmutable() 88248590Smm { 89248590Smm Iterator iter = iterator() ; 90248590Smm while (iter.hasNext()) { 91248590Smm Object obj = iter.next() ; 92248590Smm if (obj instanceof MakeImmutable) { 93248590Smm MakeImmutable element = (MakeImmutable)obj ; 94248590Smm element.makeImmutable() ; 95248590Smm } 96248590Smm } 97248590Smm } 98248590Smm 99248590Smm // Methods overridden from AbstractList 100248590Smm 101248590Smm public int size() 102248590Smm { 103324418Smm return delegate.size() ; 104324418Smm } 105248590Smm 106324418Smm public Object get(int index) 107248590Smm { 108248590Smm return delegate.get(index) ; 109248590Smm } 110248590Smm 111248590Smm public Object set(int index, Object element) 112248590Smm { 113248590Smm if (immutable) 114248590Smm throw new UnsupportedOperationException() ; 115248590Smm 116248590Smm return delegate.set(index, element) ; 117248590Smm } 118248590Smm 119248590Smm public void add(int index, Object element) 120248590Smm { 121248590Smm if (immutable) 122248590Smm throw new UnsupportedOperationException() ; 123248590Smm 124248590Smm delegate.add(index, element) ; 125248590Smm } 126248590Smm 127248590Smm public Object remove(int index) 128248590Smm { 129248590Smm if (immutable) 130248590Smm throw new UnsupportedOperationException() ; 131248590Smm 132248590Smm return delegate.remove(index) ; 133248590Smm } 134248590Smm 135248590Smm // We also override subList so that the result is a FreezableList. 136248590Smm public List subList(int fromIndex, int toIndex) 137248590Smm { 138248590Smm List list = delegate.subList(fromIndex, toIndex) ; 139248590Smm List result = new FreezableList( list, immutable ) ; 140248590Smm return result ; 141248590Smm } 142248590Smm} 143248590Smm