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. package com.sun.corba.se.impl.ior ;
  8. import java.util.Collection ;
  9. import java.util.List ;
  10. import java.util.AbstractList ;
  11. import java.util.ListIterator ;
  12. import java.util.Iterator ;
  13. import com.sun.corba.se.spi.ior.MakeImmutable ;
  14. /** Simple class that delegates all List operations to
  15. * another list. It also can be frozen, which means that
  16. * a number of operations can be performed on the list,
  17. * and then the list can be made immutable, so that no
  18. * further changes are possible. A FreezableList is frozen
  19. * using the makeImmutable method.
  20. */
  21. public class FreezableList extends AbstractList {
  22. private List delegate = null ;
  23. private boolean immutable = false ;
  24. public boolean equals( Object obj )
  25. {
  26. if (obj == null)
  27. return false ;
  28. if (!(obj instanceof FreezableList))
  29. return false ;
  30. FreezableList other = (FreezableList)obj ;
  31. return delegate.equals( other.delegate ) &&
  32. (immutable == other.immutable) ;
  33. }
  34. public int hashCode()
  35. {
  36. return delegate.hashCode() ;
  37. }
  38. public FreezableList( List delegate, boolean immutable )
  39. {
  40. this.delegate = delegate ;
  41. this.immutable = immutable ;
  42. }
  43. public FreezableList( List delegate )
  44. {
  45. this( delegate, false ) ;
  46. }
  47. public void makeImmutable()
  48. {
  49. immutable = true ;
  50. }
  51. public boolean isImmutable()
  52. {
  53. return immutable ;
  54. }
  55. public void makeElementsImmutable()
  56. {
  57. Iterator iter = iterator() ;
  58. while (iter.hasNext()) {
  59. Object obj = iter.next() ;
  60. if (obj instanceof MakeImmutable) {
  61. MakeImmutable element = (MakeImmutable)obj ;
  62. element.makeImmutable() ;
  63. }
  64. }
  65. }
  66. // Methods overridden from AbstractList
  67. public int size()
  68. {
  69. return delegate.size() ;
  70. }
  71. public Object get(int index)
  72. {
  73. return delegate.get(index) ;
  74. }
  75. public Object set(int index, Object element)
  76. {
  77. if (immutable)
  78. throw new UnsupportedOperationException() ;
  79. return delegate.set(index, element) ;
  80. }
  81. public void add(int index, Object element)
  82. {
  83. if (immutable)
  84. throw new UnsupportedOperationException() ;
  85. delegate.add(index, element) ;
  86. }
  87. public Object remove(int index)
  88. {
  89. if (immutable)
  90. throw new UnsupportedOperationException() ;
  91. return delegate.remove(index) ;
  92. }
  93. // We also override subList so that the result is a FreezableList.
  94. public List subList(int fromIndex, int toIndex)
  95. {
  96. List list = delegate.subList(fromIndex, toIndex) ;
  97. List result = new FreezableList( list, immutable ) ;
  98. return result ;
  99. }
  100. }