1. /*
  2. * Copyright 2003-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.collections.buffer;
  17. import java.io.IOException;
  18. import java.io.ObjectInputStream;
  19. import java.io.ObjectOutputStream;
  20. import java.io.Serializable;
  21. import java.util.Collection;
  22. import java.util.Iterator;
  23. import org.apache.commons.collections.Buffer;
  24. import org.apache.commons.collections.Unmodifiable;
  25. import org.apache.commons.collections.iterators.UnmodifiableIterator;
  26. /**
  27. * Decorates another <code>Buffer</code> to ensure it can't be altered.
  28. * <p>
  29. * This class is Serializable from Commons Collections 3.1.
  30. *
  31. * @since Commons Collections 3.0
  32. * @version $Revision: 1.7 $ $Date: 2004/06/02 21:57:03 $
  33. *
  34. * @author Stephen Colebourne
  35. */
  36. public final class UnmodifiableBuffer
  37. extends AbstractBufferDecorator
  38. implements Unmodifiable, Serializable {
  39. /** Serialization version */
  40. private static final long serialVersionUID = 1832948656215393357L;
  41. /**
  42. * Factory method to create an unmodifiable buffer.
  43. * <p>
  44. * If the buffer passed in is already unmodifiable, it is returned.
  45. *
  46. * @param buffer the buffer to decorate, must not be null
  47. * @return an unmodifiable Buffer
  48. * @throws IllegalArgumentException if buffer is null
  49. */
  50. public static Buffer decorate(Buffer buffer) {
  51. if (buffer instanceof Unmodifiable) {
  52. return buffer;
  53. }
  54. return new UnmodifiableBuffer(buffer);
  55. }
  56. //-----------------------------------------------------------------------
  57. /**
  58. * Constructor that wraps (not copies).
  59. *
  60. * @param buffer the buffer to decorate, must not be null
  61. * @throws IllegalArgumentException if buffer is null
  62. */
  63. private UnmodifiableBuffer(Buffer buffer) {
  64. super(buffer);
  65. }
  66. //-----------------------------------------------------------------------
  67. /**
  68. * Write the collection out using a custom routine.
  69. *
  70. * @param out the output stream
  71. * @throws IOException
  72. */
  73. private void writeObject(ObjectOutputStream out) throws IOException {
  74. out.defaultWriteObject();
  75. out.writeObject(collection);
  76. }
  77. /**
  78. * Read the collection in using a custom routine.
  79. *
  80. * @param in the input stream
  81. * @throws IOException
  82. * @throws ClassNotFoundException
  83. */
  84. private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
  85. in.defaultReadObject();
  86. collection = (Collection) in.readObject();
  87. }
  88. //-----------------------------------------------------------------------
  89. public Iterator iterator() {
  90. return UnmodifiableIterator.decorate(getCollection().iterator());
  91. }
  92. public boolean add(Object object) {
  93. throw new UnsupportedOperationException();
  94. }
  95. public boolean addAll(Collection coll) {
  96. throw new UnsupportedOperationException();
  97. }
  98. public void clear() {
  99. throw new UnsupportedOperationException();
  100. }
  101. public boolean remove(Object object) {
  102. throw new UnsupportedOperationException();
  103. }
  104. public boolean removeAll(Collection coll) {
  105. throw new UnsupportedOperationException();
  106. }
  107. public boolean retainAll(Collection coll) {
  108. throw new UnsupportedOperationException();
  109. }
  110. //-----------------------------------------------------------------------
  111. public Object remove() {
  112. throw new UnsupportedOperationException();
  113. }
  114. }