001: /*
002: * Copyright 2003-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections.buffer;
017:
018: import java.io.IOException;
019: import java.io.ObjectInputStream;
020: import java.io.ObjectOutputStream;
021: import java.io.Serializable;
022: import java.util.Collection;
023: import java.util.Iterator;
024:
025: import org.apache.commons.collections.Buffer;
026: import org.apache.commons.collections.Unmodifiable;
027: import org.apache.commons.collections.iterators.UnmodifiableIterator;
028:
029: /**
030: * Decorates another <code>Buffer</code> to ensure it can't be altered.
031: * <p>
032: * This class is Serializable from Commons Collections 3.1.
033: *
034: * @since Commons Collections 3.0
035: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
036: *
037: * @author Stephen Colebourne
038: */
039: public final class UnmodifiableBuffer extends AbstractBufferDecorator
040: implements Unmodifiable, Serializable {
041:
042: /** Serialization version */
043: private static final long serialVersionUID = 1832948656215393357L;
044:
045: /**
046: * Factory method to create an unmodifiable buffer.
047: * <p>
048: * If the buffer passed in is already unmodifiable, it is returned.
049: *
050: * @param buffer the buffer to decorate, must not be null
051: * @return an unmodifiable Buffer
052: * @throws IllegalArgumentException if buffer is null
053: */
054: public static Buffer decorate(Buffer buffer) {
055: if (buffer instanceof Unmodifiable) {
056: return buffer;
057: }
058: return new UnmodifiableBuffer(buffer);
059: }
060:
061: //-----------------------------------------------------------------------
062: /**
063: * Constructor that wraps (not copies).
064: *
065: * @param buffer the buffer to decorate, must not be null
066: * @throws IllegalArgumentException if buffer is null
067: */
068: private UnmodifiableBuffer(Buffer buffer) {
069: super (buffer);
070: }
071:
072: //-----------------------------------------------------------------------
073: /**
074: * Write the collection out using a custom routine.
075: *
076: * @param out the output stream
077: * @throws IOException
078: */
079: private void writeObject(ObjectOutputStream out) throws IOException {
080: out.defaultWriteObject();
081: out.writeObject(collection);
082: }
083:
084: /**
085: * Read the collection in using a custom routine.
086: *
087: * @param in the input stream
088: * @throws IOException
089: * @throws ClassNotFoundException
090: */
091: private void readObject(ObjectInputStream in) throws IOException,
092: ClassNotFoundException {
093: in.defaultReadObject();
094: collection = (Collection) in.readObject();
095: }
096:
097: //-----------------------------------------------------------------------
098: public Iterator iterator() {
099: return UnmodifiableIterator
100: .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: }
|