KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > bag > UnmodifiableBag


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.bag;
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 import java.util.Set JavaDoc;
25
26 import org.apache.commons.collections.Bag;
27 import org.apache.commons.collections.Unmodifiable;
28 import org.apache.commons.collections.iterators.UnmodifiableIterator;
29 import org.apache.commons.collections.set.UnmodifiableSet;
30
31 /**
32  * Decorates another <code>Bag</code> to ensure it can't be altered.
33  * <p>
34  * This class is Serializable from Commons Collections 3.1.
35  *
36  * @since Commons Collections 3.0
37  * @version $Revision: 1.8 $ $Date: 2004/06/02 21:56:19 $
38  *
39  * @author Stephen Colebourne
40  */

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

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

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

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

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