1. /*
  2. * @(#)FreezableList.java 1.11 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.corba.se.internal.ior ;
  8. import java.util.Collection ;
  9. import java.util.List ;
  10. import java.util.ListIterator ;
  11. import java.util.Iterator ;
  12. /** Simple class that delegates all List operations to
  13. * another list. It also can be frozen, which means that
  14. * a number of operations can be performed on the list,
  15. * and then the list can be made immutable, so that no
  16. * further changes are possible. A FreezableList is frozen
  17. * using the makeImmutable method.
  18. */
  19. public class FreezableList implements List {
  20. private List delegate = null ;
  21. private boolean immutable = false ;
  22. public boolean equals( Object obj )
  23. {
  24. if (obj == null)
  25. return false ;
  26. if (!(obj instanceof FreezableList))
  27. return false ;
  28. FreezableList other = (FreezableList)obj ;
  29. return delegate.equals( other.delegate ) &&
  30. (immutable == other.immutable) ;
  31. }
  32. public FreezableList( List delegate, boolean immutable )
  33. {
  34. this.delegate = delegate ;
  35. this.immutable = immutable ;
  36. }
  37. public FreezableList( List delegate )
  38. {
  39. this( delegate, false ) ;
  40. }
  41. public void makeImmutable()
  42. {
  43. immutable = true ;
  44. }
  45. public boolean isImmutable()
  46. {
  47. return immutable ;
  48. }
  49. // Methods defined in List
  50. public int size()
  51. {
  52. return delegate.size() ;
  53. }
  54. public boolean isEmpty()
  55. {
  56. return delegate.isEmpty() ;
  57. }
  58. public boolean contains(Object o)
  59. {
  60. return delegate.contains(o) ;
  61. }
  62. public Iterator iterator()
  63. {
  64. return new FreezableIterator( delegate.iterator(), this ) ;
  65. }
  66. public Object[] toArray()
  67. {
  68. return delegate.toArray() ;
  69. }
  70. public Object[] toArray(Object a[])
  71. {
  72. return delegate.toArray(a) ;
  73. }
  74. public boolean add(Object o)
  75. {
  76. if (immutable)
  77. throw new UnsupportedOperationException() ;
  78. return delegate.add(o) ;
  79. }
  80. public boolean remove(Object o)
  81. {
  82. if (immutable)
  83. throw new UnsupportedOperationException() ;
  84. return delegate.remove(o) ;
  85. }
  86. public boolean containsAll(Collection c)
  87. {
  88. return delegate.containsAll(c) ;
  89. }
  90. public boolean addAll(Collection c)
  91. {
  92. if (immutable)
  93. throw new UnsupportedOperationException() ;
  94. return delegate.addAll(c) ;
  95. }
  96. public boolean addAll(int index, Collection c)
  97. {
  98. if (immutable)
  99. throw new UnsupportedOperationException() ;
  100. return delegate.addAll(index, c) ;
  101. }
  102. public boolean removeAll(Collection c)
  103. {
  104. if (immutable)
  105. throw new UnsupportedOperationException() ;
  106. return delegate.removeAll(c) ;
  107. }
  108. public boolean retainAll(Collection c)
  109. {
  110. if (immutable)
  111. throw new UnsupportedOperationException() ;
  112. return delegate.retainAll(c) ;
  113. }
  114. public void clear()
  115. {
  116. if (immutable)
  117. throw new UnsupportedOperationException() ;
  118. delegate.clear() ;
  119. }
  120. public int hashCode()
  121. {
  122. return delegate.hashCode() ;
  123. }
  124. public Object get(int index)
  125. {
  126. return delegate.get(index) ;
  127. }
  128. public Object set(int index, Object element)
  129. {
  130. if (immutable)
  131. throw new UnsupportedOperationException() ;
  132. return delegate.set(index, element) ;
  133. }
  134. public void add(int index, Object element)
  135. {
  136. if (immutable)
  137. throw new UnsupportedOperationException() ;
  138. delegate.add(index, element) ;
  139. }
  140. public Object remove(int index)
  141. {
  142. if (immutable)
  143. throw new UnsupportedOperationException() ;
  144. return delegate.remove(index) ;
  145. }
  146. public int indexOf(Object o)
  147. {
  148. return delegate.indexOf(o) ;
  149. }
  150. public int lastIndexOf(Object o)
  151. {
  152. return delegate.lastIndexOf(o) ;
  153. }
  154. public ListIterator listIterator()
  155. {
  156. return new FreezableListIterator( delegate.listIterator(), this ) ;
  157. }
  158. public ListIterator listIterator(int index)
  159. {
  160. return new FreezableListIterator( delegate.listIterator( index ), this ) ;
  161. }
  162. public List subList(int fromIndex, int toIndex)
  163. {
  164. List list = delegate.subList(fromIndex, toIndex) ;
  165. List result = new FreezableList( list, immutable ) ;
  166. return result ;
  167. }
  168. public String toString()
  169. {
  170. return delegate.toString() ;
  171. }
  172. class FreezableIterator implements Iterator {
  173. protected Iterator iter ;
  174. protected FreezableList list ;
  175. public FreezableIterator( Iterator iter, FreezableList list )
  176. {
  177. this.iter = iter ;
  178. this.list = list ;
  179. }
  180. public boolean hasNext()
  181. {
  182. return iter.hasNext() ;
  183. }
  184. public Object next()
  185. {
  186. return iter.next() ;
  187. }
  188. public void remove()
  189. {
  190. if (list.isImmutable())
  191. throw new UnsupportedOperationException() ;
  192. else
  193. iter.remove() ;
  194. }
  195. }
  196. class FreezableListIterator extends FreezableIterator
  197. implements ListIterator
  198. {
  199. public FreezableListIterator( ListIterator iter, FreezableList list )
  200. {
  201. super( iter, list ) ;
  202. }
  203. public boolean hasPrevious()
  204. {
  205. return ((ListIterator)iter).hasPrevious() ;
  206. }
  207. public Object previous()
  208. {
  209. return ((ListIterator)iter).previous() ;
  210. }
  211. public int nextIndex()
  212. {
  213. return ((ListIterator)iter).nextIndex() ;
  214. }
  215. public int previousIndex()
  216. {
  217. return ((ListIterator)iter).previousIndex() ;
  218. }
  219. public void set(Object o)
  220. {
  221. if (list.isImmutable())
  222. throw new UnsupportedOperationException() ;
  223. else
  224. ((ListIterator)iter).set(o) ;
  225. }
  226. public void add(Object o)
  227. {
  228. if (list.isImmutable())
  229. throw new UnsupportedOperationException() ;
  230. else
  231. ((ListIterator)iter).add(o) ;
  232. }
  233. }
  234. }