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.Bag;
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 Bag} 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: UnmodifiableBag.java 1429905 2013-01-07 17:15:14Z ggregory $
41 */
42 public final class UnmodifiableBag<E>
43 extends AbstractBagDecorator<E> implements Unmodifiable, Serializable {
44
45 /** Serialization version */
46 private static final long serialVersionUID = -1873799975157099624L;
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 Bag
56 * @throws IllegalArgumentException if bag is null
57 */
58 public static <E> Bag<E> unmodifiableBag(final Bag<E> bag) {
59 if (bag instanceof Unmodifiable) {
60 return bag;
61 }
62 return new UnmodifiableBag<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 UnmodifiableBag(final Bag<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.<E> unmodifiableIterator(decorated()
105 .iterator());
106 }
107
108 @Override
109 public boolean add(final E object) {
110 throw new UnsupportedOperationException();
111 }
112
113 @Override
114 public boolean addAll(final Collection<? extends E> coll) {
115 throw new UnsupportedOperationException();
116 }
117
118 @Override
119 public void clear() {
120 throw new UnsupportedOperationException();
121 }
122
123 @Override
124 public boolean remove(final Object object) {
125 throw new UnsupportedOperationException();
126 }
127
128 @Override
129 public boolean removeAll(final Collection<?> coll) {
130 throw new UnsupportedOperationException();
131 }
132
133 @Override
134 public boolean retainAll(final Collection<?> coll) {
135 throw new UnsupportedOperationException();
136 }
137
138 //-----------------------------------------------------------------------
139 @Override
140 public boolean add(final E object, final int count) {
141 throw new UnsupportedOperationException();
142 }
143
144 @Override
145 public boolean remove(final Object object, final int count) {
146 throw new UnsupportedOperationException();
147 }
148
149 @Override
150 public Set<E> uniqueSet() {
151 final Set<E> set = decorated().uniqueSet();
152 return UnmodifiableSet.<E> unmodifiableSet(set);
153 }
154
155 }