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.bag;
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: import java.util.Set;
025:
026: import org.apache.commons.collections.Bag;
027: import org.apache.commons.collections.Unmodifiable;
028: import org.apache.commons.collections.iterators.UnmodifiableIterator;
029: import org.apache.commons.collections.set.UnmodifiableSet;
030:
031: /**
032: * Decorates another <code>Bag</code> to ensure it can't be altered.
033: * <p>
034: * This class is Serializable from Commons Collections 3.1.
035: *
036: * @since Commons Collections 3.0
037: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
038: *
039: * @author Stephen Colebourne
040: */
041: public final class UnmodifiableBag extends AbstractBagDecorator
042: implements Unmodifiable, Serializable {
043:
044: /** Serialization version */
045: private static final long serialVersionUID = -1873799975157099624L;
046:
047: /**
048: * Factory method to create an unmodifiable bag.
049: * <p>
050: * If the bag passed in is already unmodifiable, it is returned.
051: *
052: * @param bag the bag to decorate, must not be null
053: * @return an unmodifiable Bag
054: * @throws IllegalArgumentException if bag is null
055: */
056: public static Bag decorate(Bag bag) {
057: if (bag instanceof Unmodifiable) {
058: return bag;
059: }
060: return new UnmodifiableBag(bag);
061: }
062:
063: //-----------------------------------------------------------------------
064: /**
065: * Constructor that wraps (not copies).
066: *
067: * @param bag the bag to decorate, must not be null
068: * @throws IllegalArgumentException if bag is null
069: */
070: private UnmodifiableBag(Bag bag) {
071: super (bag);
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,
094: ClassNotFoundException {
095: in.defaultReadObject();
096: collection = (Collection) in.readObject();
097: }
098:
099: //-----------------------------------------------------------------------
100: public Iterator iterator() {
101: return UnmodifiableIterator
102: .decorate(getCollection().iterator());
103: }
104:
105: public boolean add(Object object) {
106: throw new UnsupportedOperationException();
107: }
108:
109: public boolean addAll(Collection coll) {
110: throw new UnsupportedOperationException();
111: }
112:
113: public void clear() {
114: throw new UnsupportedOperationException();
115: }
116:
117: public boolean remove(Object object) {
118: throw new UnsupportedOperationException();
119: }
120:
121: public boolean removeAll(Collection coll) {
122: throw new UnsupportedOperationException();
123: }
124:
125: public boolean retainAll(Collection coll) {
126: throw new UnsupportedOperationException();
127: }
128:
129: //-----------------------------------------------------------------------
130: public boolean add(Object object, int count) {
131: throw new UnsupportedOperationException();
132: }
133:
134: public boolean remove(Object object, int count) {
135: throw new UnsupportedOperationException();
136: }
137:
138: public Set uniqueSet() {
139: Set set = getBag().uniqueSet();
140: return UnmodifiableSet.decorate(set);
141: }
142:
143: }
|