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.collection; 018 019import java.util.Collection; 020import java.util.Iterator; 021 022import org.apache.commons.collections4.Unmodifiable; 023import org.apache.commons.collections4.iterators.UnmodifiableIterator; 024 025/** 026 * Decorates another {@link Collection} to ensure it can't be altered. 027 * <p> 028 * This class is Serializable from Commons Collections 3.1. 029 * <p> 030 * Attempts to modify it will result in an UnsupportedOperationException. 031 * 032 * @param <E> the type of the elements in the collection 033 * @since 3.0 034 * @version $Id: UnmodifiableCollection.html 972421 2015-11-14 20:00:04Z tn $ 035 */ 036public final class UnmodifiableCollection<E> 037 extends AbstractCollectionDecorator<E> 038 implements Unmodifiable { 039 040 /** Serialization version */ 041 private static final long serialVersionUID = -239892006883819945L; 042 043 /** 044 * Factory method to create an unmodifiable collection. 045 * <p> 046 * If the collection passed in is already unmodifiable, it is returned. 047 * 048 * @param <T> the type of the elements in the collection 049 * @param coll the collection to decorate, must not be null 050 * @return an unmodifiable collection 051 * @throws IllegalArgumentException if collection is null 052 * @since 4.0 053 */ 054 public static <T> Collection<T> unmodifiableCollection(final Collection<? extends T> coll) { 055 if (coll instanceof Unmodifiable) { 056 @SuppressWarnings("unchecked") // safe to upcast 057 final Collection<T> tmpColl = (Collection<T>) coll; 058 return tmpColl; 059 } 060 return new UnmodifiableCollection<T>(coll); 061 } 062 063 //----------------------------------------------------------------------- 064 /** 065 * Constructor that wraps (not copies). 066 * 067 * @param coll the collection to decorate, must not be null 068 * @throws IllegalArgumentException if collection is null 069 */ 070 @SuppressWarnings("unchecked") // safe to upcast 071 private UnmodifiableCollection(final Collection<? extends E> coll) { 072 super((Collection<E>) coll); 073 } 074 075 //----------------------------------------------------------------------- 076 @Override 077 public Iterator<E> iterator() { 078 return UnmodifiableIterator.unmodifiableIterator(decorated().iterator()); 079 } 080 081 @Override 082 public boolean add(final E object) { 083 throw new UnsupportedOperationException(); 084 } 085 086 @Override 087 public boolean addAll(final Collection<? extends E> coll) { 088 throw new UnsupportedOperationException(); 089 } 090 091 @Override 092 public void clear() { 093 throw new UnsupportedOperationException(); 094 } 095 096 @Override 097 public boolean remove(final Object object) { 098 throw new UnsupportedOperationException(); 099 } 100 101 @Override 102 public boolean removeAll(final Collection<?> coll) { 103 throw new UnsupportedOperationException(); 104 } 105 106 @Override 107 public boolean retainAll(final Collection<?> coll) { 108 throw new UnsupportedOperationException(); 109 } 110 111}