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 */ 035public final class UnmodifiableCollection<E> 036 extends AbstractCollectionDecorator<E> 037 implements Unmodifiable { 038 039 /** Serialization version */ 040 private static final long serialVersionUID = -239892006883819945L; 041 042 /** 043 * Factory method to create an unmodifiable collection. 044 * <p> 045 * If the collection passed in is already unmodifiable, it is returned. 046 * 047 * @param <T> the type of the elements in the collection 048 * @param coll the collection to decorate, must not be null 049 * @return an unmodifiable collection 050 * @throws NullPointerException if collection is null 051 * @since 4.0 052 */ 053 public static <T> Collection<T> unmodifiableCollection(final Collection<? extends T> coll) { 054 if (coll instanceof Unmodifiable) { 055 @SuppressWarnings("unchecked") // safe to upcast 056 final Collection<T> tmpColl = (Collection<T>) coll; 057 return tmpColl; 058 } 059 return new UnmodifiableCollection<>(coll); 060 } 061 062 //----------------------------------------------------------------------- 063 /** 064 * Constructor that wraps (not copies). 065 * 066 * @param coll the collection to decorate, must not be null 067 * @throws NullPointerException if collection is null 068 */ 069 @SuppressWarnings("unchecked") // safe to upcast 070 private UnmodifiableCollection(final Collection<? extends E> coll) { 071 super((Collection<E>) coll); 072 } 073 074 //----------------------------------------------------------------------- 075 @Override 076 public Iterator<E> iterator() { 077 return UnmodifiableIterator.unmodifiableIterator(decorated().iterator()); 078 } 079 080 @Override 081 public boolean add(final E object) { 082 throw new UnsupportedOperationException(); 083 } 084 085 @Override 086 public boolean addAll(final Collection<? extends E> coll) { 087 throw new UnsupportedOperationException(); 088 } 089 090 @Override 091 public void clear() { 092 throw new UnsupportedOperationException(); 093 } 094 095 @Override 096 public boolean remove(final Object object) { 097 throw new UnsupportedOperationException(); 098 } 099 100 @Override 101 public boolean removeAll(final Collection<?> coll) { 102 throw new UnsupportedOperationException(); 103 } 104 105 @Override 106 public boolean retainAll(final Collection<?> coll) { 107 throw new UnsupportedOperationException(); 108 } 109 110}