001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.commons.collections.bag;
018    
019    import java.io.IOException;
020    import java.io.ObjectInputStream;
021    import java.io.ObjectOutputStream;
022    import java.io.Serializable;
023    import java.util.Collection;
024    import java.util.Iterator;
025    import java.util.Set;
026    
027    import org.apache.commons.collections.SortedBag;
028    import org.apache.commons.collections.Unmodifiable;
029    import org.apache.commons.collections.iterators.UnmodifiableIterator;
030    import org.apache.commons.collections.set.UnmodifiableSet;
031    
032    /**
033     * Decorates another <code>SortedBag</code> to ensure it can't be altered.
034     * <p>
035     * This class is Serializable from Commons Collections 3.1.
036     *
037     * @since Commons Collections 3.0
038     * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $
039     * 
040     * @author Stephen Colebourne
041     */
042    public final class UnmodifiableSortedBag
043            extends AbstractSortedBagDecorator implements Unmodifiable, 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, ClassNotFoundException {
095            in.defaultReadObject();
096            collection = (Collection) in.readObject();
097        }
098    
099        //-----------------------------------------------------------------------
100        public Iterator iterator() {
101            return UnmodifiableIterator.decorate(getCollection().iterator());
102        }
103    
104        public boolean add(Object object) {
105            throw new UnsupportedOperationException();
106        }
107    
108        public boolean addAll(Collection coll) {
109            throw new UnsupportedOperationException();
110        }
111    
112        public void clear() {
113            throw new UnsupportedOperationException();
114        }
115    
116        public boolean remove(Object object) {
117            throw new UnsupportedOperationException();
118        }
119    
120        public boolean removeAll(Collection coll) {
121            throw new UnsupportedOperationException();
122        }
123    
124        public boolean retainAll(Collection coll) {
125            throw new UnsupportedOperationException();
126        }
127    
128        //-----------------------------------------------------------------------
129        public boolean add(Object object, int count) {
130            throw new UnsupportedOperationException();
131        }
132    
133        public boolean remove(Object object, int count) {
134            throw new UnsupportedOperationException();
135        }
136    
137        public Set uniqueSet() {
138            Set set = getBag().uniqueSet();
139            return UnmodifiableSet.decorate(set);
140        }
141    
142    }