1. /*
  2. * Copyright 2003-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.collections.set;
  17. import java.util.Collection;
  18. import java.util.Iterator;
  19. import java.util.Set;
  20. import org.apache.commons.collections.Unmodifiable;
  21. import org.apache.commons.collections.iterators.UnmodifiableIterator;
  22. /**
  23. * Decorates another <code>Set</code> to ensure it can't be altered.
  24. * <p>
  25. * This class is Serializable from Commons Collections 3.1.
  26. *
  27. * @since Commons Collections 3.0
  28. * @version $Revision: 1.7 $ $Date: 2004/06/03 22:02:13 $
  29. *
  30. * @author Stephen Colebourne
  31. */
  32. public final class UnmodifiableSet
  33. extends AbstractSerializableSetDecorator
  34. implements Unmodifiable {
  35. /** Serialization version */
  36. private static final long serialVersionUID = 6499119872185240161L;
  37. /**
  38. * Factory method to create an unmodifiable set.
  39. *
  40. * @param set the set to decorate, must not be null
  41. * @throws IllegalArgumentException if set is null
  42. */
  43. public static Set decorate(Set set) {
  44. if (set instanceof Unmodifiable) {
  45. return set;
  46. }
  47. return new UnmodifiableSet(set);
  48. }
  49. //-----------------------------------------------------------------------
  50. /**
  51. * Constructor that wraps (not copies).
  52. *
  53. * @param set the set to decorate, must not be null
  54. * @throws IllegalArgumentException if set is null
  55. */
  56. private UnmodifiableSet(Set set) {
  57. super(set);
  58. }
  59. //-----------------------------------------------------------------------
  60. public Iterator iterator() {
  61. return UnmodifiableIterator.decorate(getCollection().iterator());
  62. }
  63. public boolean add(Object object) {
  64. throw new UnsupportedOperationException();
  65. }
  66. public boolean addAll(Collection coll) {
  67. throw new UnsupportedOperationException();
  68. }
  69. public void clear() {
  70. throw new UnsupportedOperationException();
  71. }
  72. public boolean remove(Object object) {
  73. throw new UnsupportedOperationException();
  74. }
  75. public boolean removeAll(Collection coll) {
  76. throw new UnsupportedOperationException();
  77. }
  78. public boolean retainAll(Collection coll) {
  79. throw new UnsupportedOperationException();
  80. }
  81. }