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 */ 017package org.apache.commons.collections4.multiset; 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.MultiSet; 028import org.apache.commons.collections4.Unmodifiable; 029import org.apache.commons.collections4.iterators.UnmodifiableIterator; 030import org.apache.commons.collections4.set.UnmodifiableSet; 031 032/** 033 * Decorates another {@link MultiSet} to ensure it can't be altered. 034 * <p> 035 * Attempts to modify it will result in an UnsupportedOperationException. 036 * </p> 037 * 038 * @param <E> the type held in the multiset 039 * @since 4.1 040 */ 041public final class UnmodifiableMultiSet<E> 042 extends AbstractMultiSetDecorator<E> implements Unmodifiable { 043 044 /** Serialization version */ 045 private static final long serialVersionUID = 20150611L; 046 047 /** 048 * Factory method to create an unmodifiable multiset. 049 * <p> 050 * If the multiset passed in is already unmodifiable, it is returned. 051 * 052 * @param <E> the type of the elements in the multiset 053 * @param multiset the multiset to decorate, may not be null 054 * @return an unmodifiable MultiSet 055 * @throws NullPointerException if multiset is null 056 */ 057 public static <E> MultiSet<E> unmodifiableMultiSet(final MultiSet<? extends E> multiset) { 058 if (multiset instanceof Unmodifiable) { 059 @SuppressWarnings("unchecked") // safe to upcast 060 final MultiSet<E> tmpMultiSet = (MultiSet<E>) multiset; 061 return tmpMultiSet; 062 } 063 return new UnmodifiableMultiSet<>(multiset); 064 } 065 066 /** 067 * Constructor that wraps (not copies). 068 * 069 * @param multiset the multiset to decorate, may not be null 070 * @throws NullPointerException if multiset is null 071 */ 072 @SuppressWarnings("unchecked") // safe to upcast 073 private UnmodifiableMultiSet(final MultiSet<? extends E> multiset) { 074 super((MultiSet<E>) multiset); 075 } 076 077 @Override 078 public boolean add(final E object) { 079 throw new UnsupportedOperationException(); 080 } 081 082 @Override 083 public int add(final E object, final int count) { 084 throw new UnsupportedOperationException(); 085 } 086 087 @Override 088 public boolean addAll(final Collection<? extends E> coll) { 089 throw new UnsupportedOperationException(); 090 } 091 092 @Override 093 public void clear() { 094 throw new UnsupportedOperationException(); 095 } 096 097 @Override 098 public Set<MultiSet.Entry<E>> entrySet() { 099 final Set<MultiSet.Entry<E>> set = decorated().entrySet(); 100 return UnmodifiableSet.unmodifiableSet(set); 101 } 102 103 @Override 104 public Iterator<E> iterator() { 105 return UnmodifiableIterator.<E>unmodifiableIterator(decorated().iterator()); 106 } 107 108 /** 109 * Read the collection in using a custom routine. 110 * 111 * @param in the input stream 112 * @throws IOException if an error occurs while reading from the stream 113 * @throws ClassNotFoundException if an object read from the stream can not be loaded 114 * @throws ClassCastException if deserialized object has wrong type 115 */ 116 @SuppressWarnings("unchecked") // will throw CCE, see Javadoc 117 private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException { 118 in.defaultReadObject(); 119 setCollection((Collection<E>) in.readObject()); 120 } 121 122 @Override 123 public boolean remove(final Object object) { 124 throw new UnsupportedOperationException(); 125 } 126 127 @Override 128 public int remove(final Object object, final int count) { 129 throw new UnsupportedOperationException(); 130 } 131 132 @Override 133 public boolean removeAll(final Collection<?> coll) { 134 throw new UnsupportedOperationException(); 135 } 136 137 /** 138 * @since 4.4 139 */ 140 @Override 141 public boolean removeIf(final Predicate<? super E> filter) { 142 throw new UnsupportedOperationException(); 143 } 144 145 @Override 146 public boolean retainAll(final Collection<?> coll) { 147 throw new UnsupportedOperationException(); 148 } 149 150 @Override 151 public int setCount(final E object, final int count) { 152 throw new UnsupportedOperationException(); 153 } 154 155 @Override 156 public Set<E> uniqueSet() { 157 final Set<E> set = decorated().uniqueSet(); 158 return UnmodifiableSet.unmodifiableSet(set); 159 } 160 161 /** 162 * Write the collection out using a custom routine. 163 * 164 * @param out the output stream 165 * @throws IOException if an error occurs while writing to the stream 166 */ 167 private void writeObject(final ObjectOutputStream out) throws IOException { 168 out.defaultWriteObject(); 169 out.writeObject(decorated()); 170 } 171 172}