View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.collections.bag;
18  
19  import java.io.IOException;
20  import java.io.ObjectInputStream;
21  import java.io.ObjectOutputStream;
22  import java.io.Serializable;
23  import java.util.Collection;
24  import java.util.Iterator;
25  import java.util.Set;
26  
27  import org.apache.commons.collections.SortedBag;
28  import org.apache.commons.collections.Unmodifiable;
29  import org.apache.commons.collections.iterators.UnmodifiableIterator;
30  import org.apache.commons.collections.set.UnmodifiableSet;
31  
32  /**
33   * Decorates another {@link SortedBag} to ensure it can't be altered.
34   * <p>
35   * This class is Serializable from Commons Collections 3.1.
36   * <p>
37   * Attempts to modify it will result in an UnsupportedOperationException. 
38   *
39   * @since 3.0
40   * @version $Id: UnmodifiableSortedBag.java 1429905 2013-01-07 17:15:14Z ggregory $
41   */
42  public final class UnmodifiableSortedBag<E>
43          extends AbstractSortedBagDecorator<E> implements Unmodifiable, Serializable {
44  
45      /** Serialization version */
46      private static final long serialVersionUID = -3190437252665717841L;
47  
48      /**
49       * Factory method to create an unmodifiable bag.
50       * <p>
51       * If the bag passed in is already unmodifiable, it is returned.
52       * 
53       * @param <E> the type of the elements in the bag
54       * @param bag  the bag to decorate, must not be null
55       * @return an unmodifiable SortedBag
56       * @throws IllegalArgumentException if bag is null
57       */
58      public static <E> SortedBag<E> unmodifiableSortedBag(final SortedBag<E> bag) {
59          if (bag instanceof Unmodifiable) {
60              return bag;
61          }
62          return new UnmodifiableSortedBag<E>(bag);
63      }
64  
65      //-----------------------------------------------------------------------
66      /**
67       * Constructor that wraps (not copies).
68       * 
69       * @param bag  the bag to decorate, must not be null
70       * @throws IllegalArgumentException if bag is null
71       */
72      private UnmodifiableSortedBag(final SortedBag<E> bag) {
73          super(bag);
74      }
75  
76      //-----------------------------------------------------------------------
77      /**
78       * Write the collection out using a custom routine.
79       * 
80       * @param out  the output stream
81       * @throws IOException
82       */
83      private void writeObject(final ObjectOutputStream out) throws IOException {
84          out.defaultWriteObject();
85          out.writeObject(collection);
86      }
87  
88      /**
89       * Read the collection in using a custom routine.
90       * 
91       * @param in  the input stream
92       * @throws IOException
93       * @throws ClassNotFoundException
94       */
95      @SuppressWarnings("unchecked")
96      private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
97          in.defaultReadObject();
98          collection = (Collection<E>) in.readObject();
99      }
100 
101     //-----------------------------------------------------------------------
102     @Override
103     public Iterator<E> iterator() {
104         return UnmodifiableIterator.unmodifiableIterator(decorated().iterator());
105     }
106 
107     @Override
108     public boolean add(final E object) {
109         throw new UnsupportedOperationException();
110     }
111 
112     @Override
113     public boolean addAll(final Collection<? extends E> coll) {
114         throw new UnsupportedOperationException();
115     }
116 
117     @Override
118     public void clear() {
119         throw new UnsupportedOperationException();
120     }
121 
122     @Override
123     public boolean remove(final Object object) {
124         throw new UnsupportedOperationException();
125     }
126 
127     @Override
128     public boolean removeAll(final Collection<?> coll) {
129         throw new UnsupportedOperationException();
130     }
131 
132     @Override
133     public boolean retainAll(final Collection<?> coll) {
134         throw new UnsupportedOperationException();
135     }
136 
137     //-----------------------------------------------------------------------
138     @Override
139     public boolean add(final E object, final int count) {
140         throw new UnsupportedOperationException();
141     }
142 
143     @Override
144     public boolean remove(final Object object, final int count) {
145         throw new UnsupportedOperationException();
146     }
147 
148     @Override
149     public Set<E> uniqueSet() {
150         final Set<E> set = decorated().uniqueSet();
151         return UnmodifiableSet.unmodifiableSet(set);
152     }
153 
154 }