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