KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > buffer > UnmodifiableBuffer


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
18 import java.io.IOException JavaDoc;
19 import java.io.ObjectInputStream JavaDoc;
20 import java.io.ObjectOutputStream JavaDoc;
21 import java.io.Serializable JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.Iterator JavaDoc;
24
25 import org.apache.commons.collections.Buffer;
26 import org.apache.commons.collections.Unmodifiable;
27 import org.apache.commons.collections.iterators.UnmodifiableIterator;
28
29 /**
30  * Decorates another <code>Buffer</code> to ensure it can't be altered.
31  * <p>
32  * This class is Serializable from Commons Collections 3.1.
33  *
34  * @since Commons Collections 3.0
35  * @version $Revision: 1.7 $ $Date: 2004/06/02 21:57:03 $
36  *
37  * @author Stephen Colebourne
38  */

39 public final class UnmodifiableBuffer
40         extends AbstractBufferDecorator
41         implements Unmodifiable, Serializable JavaDoc {
42
43     /** Serialization version */
44     private static final long serialVersionUID = 1832948656215393357L;
45
46     /**
47      * Factory method to create an unmodifiable buffer.
48      * <p>
49      * If the buffer passed in is already unmodifiable, it is returned.
50      *
51      * @param buffer the buffer to decorate, must not be null
52      * @return an unmodifiable Buffer
53      * @throws IllegalArgumentException if buffer is null
54      */

55     public static Buffer decorate(Buffer buffer) {
56         if (buffer instanceof Unmodifiable) {
57             return buffer;
58         }
59         return new UnmodifiableBuffer(buffer);
60     }
61
62     //-----------------------------------------------------------------------
63
/**
64      * Constructor that wraps (not copies).
65      *
66      * @param buffer the buffer to decorate, must not be null
67      * @throws IllegalArgumentException if buffer is null
68      */

69     private UnmodifiableBuffer(Buffer buffer) {
70         super(buffer);
71     }
72
73     //-----------------------------------------------------------------------
74
/**
75      * Write the collection out using a custom routine.
76      *
77      * @param out the output stream
78      * @throws IOException
79      */

80     private void writeObject(ObjectOutputStream JavaDoc out) throws IOException JavaDoc {
81         out.defaultWriteObject();
82         out.writeObject(collection);
83     }
84
85     /**
86      * Read the collection in using a custom routine.
87      *
88      * @param in the input stream
89      * @throws IOException
90      * @throws ClassNotFoundException
91      */

92     private void readObject(ObjectInputStream JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
93         in.defaultReadObject();
94         collection = (Collection JavaDoc) in.readObject();
95     }
96
97     //-----------------------------------------------------------------------
98
public Iterator JavaDoc iterator() {
99         return UnmodifiableIterator.decorate(getCollection().iterator());
100     }
101
102     public boolean add(Object JavaDoc object) {
103         throw new UnsupportedOperationException JavaDoc();
104     }
105
106     public boolean addAll(Collection JavaDoc coll) {
107         throw new UnsupportedOperationException JavaDoc();
108     }
109
110     public void clear() {
111         throw new UnsupportedOperationException JavaDoc();
112     }
113
114     public boolean remove(Object JavaDoc object) {
115         throw new UnsupportedOperationException JavaDoc();
116     }
117
118     public boolean removeAll(Collection JavaDoc coll) {
119         throw new UnsupportedOperationException JavaDoc();
120     }
121
122     public boolean retainAll(Collection JavaDoc coll) {
123         throw new UnsupportedOperationException JavaDoc();
124     }
125
126     //-----------------------------------------------------------------------
127
public Object JavaDoc remove() {
128         throw new UnsupportedOperationException JavaDoc();
129     }
130
131 }
132
Popular Tags