1/*
2 * Copyright (c) 2000, 2003, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.corba.se.impl.ior ;
27
28import java.util.Collection ;
29import java.util.List ;
30import java.util.AbstractList ;
31import java.util.ListIterator ;
32import java.util.Iterator ;
33
34import com.sun.corba.se.spi.ior.MakeImmutable ;
35
36/** Simple class that delegates all List operations to
37* another list.  It also can be frozen, which means that
38* a number of operations can be performed on the list,
39* and then the list can be made immutable, so that no
40* further changes are possible.  A FreezableList is frozen
41* using the makeImmutable method.
42*/
43public class FreezableList extends AbstractList {
44    private List delegate = null ;
45    private boolean immutable = false ;
46
47    public boolean equals( Object obj )
48    {
49        if (obj == null)
50            return false ;
51
52        if (!(obj instanceof FreezableList))
53            return false ;
54
55        FreezableList other = (FreezableList)obj ;
56
57        return delegate.equals( other.delegate ) &&
58            (immutable == other.immutable) ;
59    }
60
61    public int hashCode()
62    {
63        return delegate.hashCode() ;
64    }
65
66    public FreezableList( List delegate, boolean immutable  )
67    {
68        this.delegate = delegate ;
69        this.immutable = immutable ;
70    }
71
72    public FreezableList( List delegate )
73    {
74        this( delegate, false ) ;
75    }
76
77    public void makeImmutable()
78    {
79        immutable = true ;
80    }
81
82    public boolean isImmutable()
83    {
84        return immutable ;
85    }
86
87    public void makeElementsImmutable()
88    {
89        Iterator iter = iterator() ;
90        while (iter.hasNext()) {
91            Object obj = iter.next() ;
92            if (obj instanceof MakeImmutable) {
93                MakeImmutable element = (MakeImmutable)obj ;
94                element.makeImmutable() ;
95            }
96        }
97    }
98
99    // Methods overridden from AbstractList
100
101    public int size()
102    {
103        return delegate.size() ;
104    }
105
106    public Object get(int index)
107    {
108        return delegate.get(index) ;
109    }
110
111    public Object set(int index, Object element)
112    {
113        if (immutable)
114            throw new UnsupportedOperationException() ;
115
116        return delegate.set(index, element) ;
117    }
118
119    public void add(int index, Object element)
120    {
121        if (immutable)
122            throw new UnsupportedOperationException() ;
123
124        delegate.add(index, element) ;
125    }
126
127    public Object remove(int index)
128    {
129        if (immutable)
130            throw new UnsupportedOperationException() ;
131
132        return delegate.remove(index) ;
133    }
134
135    // We also override subList so that the result is a FreezableList.
136    public List subList(int fromIndex, int toIndex)
137    {
138        List list = delegate.subList(fromIndex, toIndex) ;
139        List result = new FreezableList( list, immutable ) ;
140        return result ;
141    }
142}
143