001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.collections.buffer;
018
019 import java.io.IOException;
020 import java.io.ObjectInputStream;
021 import java.io.ObjectOutputStream;
022 import java.io.Serializable;
023 import java.util.Collection;
024 import java.util.Iterator;
025
026 import org.apache.commons.collections.Buffer;
027 import org.apache.commons.collections.Unmodifiable;
028 import org.apache.commons.collections.iterators.UnmodifiableIterator;
029
030 /**
031 * Decorates another <code>Buffer</code> to ensure it can't be altered.
032 * <p>
033 * This class is Serializable from Commons Collections 3.1.
034 *
035 * @since Commons Collections 3.0
036 * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $
037 *
038 * @author Stephen Colebourne
039 */
040 public final class UnmodifiableBuffer
041 extends AbstractBufferDecorator
042 implements Unmodifiable, Serializable {
043
044 /** Serialization version */
045 private static final long serialVersionUID = 1832948656215393357L;
046
047 /**
048 * Factory method to create an unmodifiable buffer.
049 * <p>
050 * If the buffer passed in is already unmodifiable, it is returned.
051 *
052 * @param buffer the buffer to decorate, must not be null
053 * @return an unmodifiable Buffer
054 * @throws IllegalArgumentException if buffer is null
055 */
056 public static Buffer decorate(Buffer buffer) {
057 if (buffer instanceof Unmodifiable) {
058 return buffer;
059 }
060 return new UnmodifiableBuffer(buffer);
061 }
062
063 //-----------------------------------------------------------------------
064 /**
065 * Constructor that wraps (not copies).
066 *
067 * @param buffer the buffer to decorate, must not be null
068 * @throws IllegalArgumentException if buffer is null
069 */
070 private UnmodifiableBuffer(Buffer buffer) {
071 super(buffer);
072 }
073
074 //-----------------------------------------------------------------------
075 /**
076 * Write the collection out using a custom routine.
077 *
078 * @param out the output stream
079 * @throws IOException
080 */
081 private void writeObject(ObjectOutputStream out) throws IOException {
082 out.defaultWriteObject();
083 out.writeObject(collection);
084 }
085
086 /**
087 * Read the collection in using a custom routine.
088 *
089 * @param in the input stream
090 * @throws IOException
091 * @throws ClassNotFoundException
092 */
093 private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
094 in.defaultReadObject();
095 collection = (Collection) in.readObject();
096 }
097
098 //-----------------------------------------------------------------------
099 public Iterator iterator() {
100 return UnmodifiableIterator.decorate(getCollection().iterator());
101 }
102
103 public boolean add(Object object) {
104 throw new UnsupportedOperationException();
105 }
106
107 public boolean addAll(Collection coll) {
108 throw new UnsupportedOperationException();
109 }
110
111 public void clear() {
112 throw new UnsupportedOperationException();
113 }
114
115 public boolean remove(Object object) {
116 throw new UnsupportedOperationException();
117 }
118
119 public boolean removeAll(Collection coll) {
120 throw new UnsupportedOperationException();
121 }
122
123 public boolean retainAll(Collection coll) {
124 throw new UnsupportedOperationException();
125 }
126
127 //-----------------------------------------------------------------------
128 public Object remove() {
129 throw new UnsupportedOperationException();
130 }
131
132 }