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 * https://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 */ 017package org.apache.commons.collections4.bag; 018 019import java.io.IOException; 020import java.io.ObjectInputStream; 021import java.io.ObjectOutputStream; 022import java.util.Collection; 023import java.util.Iterator; 024import java.util.Set; 025import java.util.function.Predicate; 026 027import org.apache.commons.collections4.Bag; 028import org.apache.commons.collections4.Unmodifiable; 029import org.apache.commons.collections4.iterators.UnmodifiableIterator; 030import org.apache.commons.collections4.multiset.UnmodifiableMultiSet; 031import org.apache.commons.collections4.set.UnmodifiableSet; 032 033/** 034 * Decorates another {@link Bag} to ensure it can't be altered. 035 * <p> 036 * This class is Serializable from Commons Collections 3.1. 037 * </p> 038 * <p> 039 * Attempts to modify it will result in an UnsupportedOperationException. 040 * </p> 041 * 042 * @param <E> The type of elements in this bag 043 * @since 3.0 044 * @deprecated Since 4.6.0, use {@link UnmodifiableMultiSet} instead. 045 */ 046@Deprecated 047public final class UnmodifiableBag<E> 048 extends AbstractBagDecorator<E> implements Unmodifiable { 049 050 /** Serialization version */ 051 private static final long serialVersionUID = -1873799975157099624L; 052 053 /** 054 * Factory method to create an unmodifiable bag. 055 * <p> 056 * If the bag passed in is already unmodifiable, it is returned. 057 * 058 * @param <E> The type of the elements in the bag 059 * @param bag The bag to decorate, must not be null 060 * @return An unmodifiable Bag 061 * @throws NullPointerException if bag is null 062 * @since 4.0 063 */ 064 public static <E> Bag<E> unmodifiableBag(final Bag<? extends E> bag) { 065 if (bag instanceof Unmodifiable) { 066 @SuppressWarnings("unchecked") // safe to upcast 067 final Bag<E> tmpBag = (Bag<E>) bag; 068 return tmpBag; 069 } 070 return new UnmodifiableBag<>(bag); 071 } 072 073 /** 074 * Constructor that wraps (not copies). 075 * 076 * @param bag The bag to decorate, must not be null 077 * @throws NullPointerException if bag is null 078 */ 079 @SuppressWarnings("unchecked") // safe to upcast 080 private UnmodifiableBag(final Bag<? extends E> bag) { 081 super((Bag<E>) bag); 082 } 083 084 /** 085 * Always throws {@link UnsupportedOperationException}. 086 * 087 * @param object Ignored. 088 * @throws UnsupportedOperationException Always thrown. 089 */ 090 @Override 091 public boolean add(final E object) { 092 throw new UnsupportedOperationException(); 093 } 094 095 /** 096 * Always throws {@link UnsupportedOperationException}. 097 * 098 * @param object Ignored. 099 * @param count Ignored. 100 * @throws UnsupportedOperationException Always thrown. 101 */ 102 @Override 103 public boolean add(final E object, final int count) { 104 throw new UnsupportedOperationException(); 105 } 106 107 /** 108 * Always throws {@link UnsupportedOperationException}. 109 * 110 * @param coll Ignored. 111 * @throws UnsupportedOperationException Always thrown. 112 */ 113 @Override 114 public boolean addAll(final Collection<? extends E> coll) { 115 throw new UnsupportedOperationException(); 116 } 117 118 /** 119 * Always throws {@link UnsupportedOperationException}. 120 * 121 * @throws UnsupportedOperationException Always thrown. 122 */ 123 @Override 124 public void clear() { 125 throw new UnsupportedOperationException(); 126 } 127 128 @Override 129 public Iterator<E> iterator() { 130 return UnmodifiableIterator.<E>unmodifiableIterator(decorated().iterator()); 131 } 132 133 /** 134 * Deserializes the collection in using a custom routine. 135 * 136 * @param in The input stream 137 * @throws IOException Thrown if an error occurs while reading from the stream 138 * @throws ClassNotFoundException if an object read from the stream cannot be loaded 139 * @throws ClassCastException if deserialized object has wrong type 140 */ 141 @SuppressWarnings("unchecked") // will throw CCE, see Javadoc 142 private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException { 143 in.defaultReadObject(); 144 setCollection((Collection<E>) in.readObject()); 145 } 146 147 /** 148 * Always throws {@link UnsupportedOperationException}. 149 * 150 * @param object Ignored. 151 * @throws UnsupportedOperationException Always thrown. 152 */ 153 @Override 154 public boolean remove(final Object object) { 155 throw new UnsupportedOperationException(); 156 } 157 158 /** 159 * Always throws {@link UnsupportedOperationException}. 160 * 161 * @param object Ignored. 162 * @param count Ignored. 163 * @throws UnsupportedOperationException Always thrown. 164 */ 165 @Override 166 public boolean remove(final Object object, final int count) { 167 throw new UnsupportedOperationException(); 168 } 169 170 /** 171 * Always throws {@link UnsupportedOperationException}. 172 * 173 * @param coll Ignored. 174 * @throws UnsupportedOperationException Always thrown. 175 */ 176 @Override 177 public boolean removeAll(final Collection<?> coll) { 178 throw new UnsupportedOperationException(); 179 } 180 181 /** 182 * Always throws {@link UnsupportedOperationException}. 183 * 184 * @param filter Ignored. 185 * @throws UnsupportedOperationException Always thrown. 186 * @since 4.4 187 */ 188 @Override 189 public boolean removeIf(final Predicate<? super E> filter) { 190 throw new UnsupportedOperationException(); 191 } 192 193 /** 194 * Always throws {@link UnsupportedOperationException}. 195 * 196 * @param coll Ignored. 197 * @throws UnsupportedOperationException Always thrown. 198 */ 199 @Override 200 public boolean retainAll(final Collection<?> coll) { 201 throw new UnsupportedOperationException(); 202 } 203 204 @Override 205 public Set<E> uniqueSet() { 206 return UnmodifiableSet.<E>unmodifiableSet(decorated().uniqueSet()); 207 } 208 209 /** 210 * Serializes this object to an ObjectOutputStream. 211 * 212 * @param out The target ObjectOutputStream. 213 * @throws IOException thrown when an I/O errors occur writing to the target stream. 214 */ 215 private void writeObject(final ObjectOutputStream out) throws IOException { 216 out.defaultWriteObject(); 217 out.writeObject(decorated()); 218 } 219 220}