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.collection;
  17. import java.util.Collection;
  18. import java.util.Iterator;
  19. import org.apache.commons.collections.Unmodifiable;
  20. import org.apache.commons.collections.iterators.UnmodifiableIterator;
  21. /**
  22. * Decorates another <code>Collection</code> to ensure it can't be altered.
  23. * <p>
  24. * This class is Serializable from Commons Collections 3.1.
  25. *
  26. * @since Commons Collections 3.0
  27. * @version $Revision: 1.8 $ $Date: 2004/06/03 22:02:13 $
  28. *
  29. * @author Stephen Colebourne
  30. */
  31. public final class UnmodifiableCollection
  32. extends AbstractSerializableCollectionDecorator
  33. implements Unmodifiable {
  34. /** Serialization version */
  35. private static final long serialVersionUID = -239892006883819945L;
  36. /**
  37. * Factory method to create an unmodifiable collection.
  38. * <p>
  39. * If the collection passed in is already unmodifiable, it is returned.
  40. *
  41. * @param coll the collection to decorate, must not be null
  42. * @return an unmodifiable collection
  43. * @throws IllegalArgumentException if collection is null
  44. */
  45. public static Collection decorate(Collection coll) {
  46. if (coll instanceof Unmodifiable) {
  47. return coll;
  48. }
  49. return new UnmodifiableCollection(coll);
  50. }
  51. //-----------------------------------------------------------------------
  52. /**
  53. * Constructor that wraps (not copies).
  54. *
  55. * @param coll the collection to decorate, must not be null
  56. * @throws IllegalArgumentException if collection is null
  57. */
  58. private UnmodifiableCollection(Collection coll) {
  59. super(coll);
  60. }
  61. //-----------------------------------------------------------------------
  62. public Iterator iterator() {
  63. return UnmodifiableIterator.decorate(getCollection().iterator());
  64. }
  65. public boolean add(Object object) {
  66. throw new UnsupportedOperationException();
  67. }
  68. public boolean addAll(Collection coll) {
  69. throw new UnsupportedOperationException();
  70. }
  71. public void clear() {
  72. throw new UnsupportedOperationException();
  73. }
  74. public boolean remove(Object object) {
  75. throw new UnsupportedOperationException();
  76. }
  77. public boolean removeAll(Collection coll) {
  78. throw new UnsupportedOperationException();
  79. }
  80. public boolean retainAll(Collection coll) {
  81. throw new UnsupportedOperationException();
  82. }
  83. }