KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > ior > FreezableList


1 /*
2  * @(#)FreezableList.java 1.13 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.corba.se.impl.ior ;
9
10 import java.util.Collection JavaDoc ;
11 import java.util.List JavaDoc ;
12 import java.util.AbstractList JavaDoc ;
13 import java.util.ListIterator JavaDoc ;
14 import java.util.Iterator JavaDoc ;
15
16 import com.sun.corba.se.spi.ior.MakeImmutable ;
17
18 /** Simple class that delegates all List operations to
19 * another list. It also can be frozen, which means that
20 * a number of operations can be performed on the list,
21 * and then the list can be made immutable, so that no
22 * further changes are possible. A FreezableList is frozen
23 * using the makeImmutable method.
24 */

25 public class FreezableList extends AbstractList JavaDoc {
26     private List JavaDoc delegate = null ;
27     private boolean immutable = false ;
28
29     public boolean equals( Object JavaDoc obj )
30     {
31     if (obj == null)
32         return false ;
33
34     if (!(obj instanceof FreezableList))
35         return false ;
36
37     FreezableList other = (FreezableList)obj ;
38
39     return delegate.equals( other.delegate ) &&
40         (immutable == other.immutable) ;
41     }
42
43     public int hashCode()
44     {
45     return delegate.hashCode() ;
46     }
47
48     public FreezableList( List JavaDoc delegate, boolean immutable )
49     {
50     this.delegate = delegate ;
51     this.immutable = immutable ;
52     }
53
54     public FreezableList( List JavaDoc delegate )
55     {
56     this( delegate, false ) ;
57     }
58
59     public void makeImmutable()
60     {
61     immutable = true ;
62     }
63
64     public boolean isImmutable()
65     {
66     return immutable ;
67     }
68
69     public void makeElementsImmutable()
70     {
71     Iterator JavaDoc iter = iterator() ;
72     while (iter.hasNext()) {
73         Object JavaDoc obj = iter.next() ;
74         if (obj instanceof MakeImmutable) {
75         MakeImmutable element = (MakeImmutable)obj ;
76         element.makeImmutable() ;
77         }
78     }
79     }
80
81     // Methods overridden from AbstractList
82

83     public int size()
84     {
85     return delegate.size() ;
86     }
87
88     public Object JavaDoc get(int index)
89     {
90     return delegate.get(index) ;
91     }
92
93     public Object JavaDoc set(int index, Object JavaDoc element)
94     {
95     if (immutable)
96         throw new UnsupportedOperationException JavaDoc() ;
97
98     return delegate.set(index, element) ;
99     }
100
101     public void add(int index, Object JavaDoc element)
102     {
103     if (immutable)
104         throw new UnsupportedOperationException JavaDoc() ;
105
106     delegate.add(index, element) ;
107     }
108
109     public Object JavaDoc remove(int index)
110     {
111     if (immutable)
112         throw new UnsupportedOperationException JavaDoc() ;
113
114     return delegate.remove(index) ;
115     }
116
117     // We also override subList so that the result is a FreezableList.
118
public List JavaDoc subList(int fromIndex, int toIndex)
119     {
120     List JavaDoc list = delegate.subList(fromIndex, toIndex) ;
121     List JavaDoc result = new FreezableList( list, immutable ) ;
122     return result ;
123     }
124 }
125
Popular Tags