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.SortedBag;
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>SortedBag</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 UnmodifiableSortedBag extends
042: AbstractSortedBagDecorator implements Unmodifiable,
043: Serializable {
044:
045: /** Serialization version */
046: private static final long serialVersionUID = -3190437252665717841L;
047:
048: /**
049: * Factory method to create an unmodifiable bag.
050: * <p>
051: * If the bag passed in is already unmodifiable, it is returned.
052: *
053: * @param bag the bag to decorate, must not be null
054: * @return an unmodifiable SortedBag
055: * @throws IllegalArgumentException if bag is null
056: */
057: public static SortedBag decorate(SortedBag bag) {
058: if (bag instanceof Unmodifiable) {
059: return bag;
060: }
061: return new UnmodifiableSortedBag(bag);
062: }
063:
064: //-----------------------------------------------------------------------
065: /**
066: * Constructor that wraps (not copies).
067: *
068: * @param bag the bag to decorate, must not be null
069: * @throws IllegalArgumentException if bag is null
070: */
071: private UnmodifiableSortedBag(SortedBag bag) {
072: super (bag);
073: }
074:
075: //-----------------------------------------------------------------------
076: /**
077: * Write the collection out using a custom routine.
078: *
079: * @param out the output stream
080: * @throws IOException
081: */
082: private void writeObject(ObjectOutputStream out) throws IOException {
083: out.defaultWriteObject();
084: out.writeObject(collection);
085: }
086:
087: /**
088: * Read the collection in using a custom routine.
089: *
090: * @param in the input stream
091: * @throws IOException
092: * @throws ClassNotFoundException
093: */
094: private void readObject(ObjectInputStream in) throws IOException,
095: ClassNotFoundException {
096: in.defaultReadObject();
097: collection = (Collection) in.readObject();
098: }
099:
100: //-----------------------------------------------------------------------
101: public Iterator iterator() {
102: return UnmodifiableIterator
103: .decorate(getCollection().iterator());
104: }
105:
106: public boolean add(Object object) {
107: throw new UnsupportedOperationException();
108: }
109:
110: public boolean addAll(Collection coll) {
111: throw new UnsupportedOperationException();
112: }
113:
114: public void clear() {
115: throw new UnsupportedOperationException();
116: }
117:
118: public boolean remove(Object object) {
119: throw new UnsupportedOperationException();
120: }
121:
122: public boolean removeAll(Collection coll) {
123: throw new UnsupportedOperationException();
124: }
125:
126: public boolean retainAll(Collection coll) {
127: throw new UnsupportedOperationException();
128: }
129:
130: //-----------------------------------------------------------------------
131: public boolean add(Object object, int count) {
132: throw new UnsupportedOperationException();
133: }
134:
135: public boolean remove(Object object, int count) {
136: throw new UnsupportedOperationException();
137: }
138:
139: public Set uniqueSet() {
140: Set set = getBag().uniqueSet();
141: return UnmodifiableSet.decorate(set);
142: }
143:
144: }
|