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.BoundedCollection;
  20. import org.apache.commons.collections.iterators.UnmodifiableIterator;
  21. /**
  22. * <code>UnmodifiableBoundedCollection</code> decorates another
  23. * <code>BoundedCollection</code> to ensure it can't be altered.
  24. * <p>
  25. * If a BoundedCollection is first wrapped in some other collection decorator,
  26. * such as synchronized or predicated, the BoundedCollection methods are no
  27. * longer accessible.
  28. * The factory on this class will attempt to retrieve the bounded nature by
  29. * examining the package scope variables.
  30. * <p>
  31. * This class is Serializable from Commons Collections 3.1.
  32. *
  33. * @since Commons Collections 3.0
  34. * @version $Revision: 1.10 $ $Date: 2004/06/03 22:02:13 $
  35. *
  36. * @author Stephen Colebourne
  37. */
  38. public final class UnmodifiableBoundedCollection
  39. extends AbstractSerializableCollectionDecorator
  40. implements BoundedCollection {
  41. /** Serialization version */
  42. private static final long serialVersionUID = -7112672385450340330L;
  43. /**
  44. * Factory method to create an unmodifiable bounded collection.
  45. *
  46. * @param coll the <code>BoundedCollection</code> to decorate, must not be null
  47. * @return a new unmodifiable bounded collection
  48. * @throws IllegalArgumentException if bag is null
  49. */
  50. public static BoundedCollection decorate(BoundedCollection coll) {
  51. return new UnmodifiableBoundedCollection(coll);
  52. }
  53. /**
  54. * Factory method to create an unmodifiable bounded collection.
  55. * <p>
  56. * This method is capable of drilling down through up to 1000 other decorators
  57. * to find a suitable BoundedCollection.
  58. *
  59. * @param coll the <code>BoundedCollection</code> to decorate, must not be null
  60. * @return a new unmodifiable bounded collection
  61. * @throws IllegalArgumentException if bag is null
  62. */
  63. public static BoundedCollection decorateUsing(Collection coll) {
  64. if (coll == null) {
  65. throw new IllegalArgumentException("The collection must not be null");
  66. }
  67. // handle decorators
  68. for (int i = 0; i < 1000; i++) { // counter to prevent infinite looping
  69. if (coll instanceof BoundedCollection) {
  70. break; // normal loop exit
  71. } else if (coll instanceof AbstractCollectionDecorator) {
  72. coll = ((AbstractCollectionDecorator) coll).collection;
  73. } else if (coll instanceof SynchronizedCollection) {
  74. coll = ((SynchronizedCollection) coll).collection;
  75. } else {
  76. break; // normal loop exit
  77. }
  78. }
  79. if (coll instanceof BoundedCollection == false) {
  80. throw new IllegalArgumentException("The collection is not a bounded collection");
  81. }
  82. return new UnmodifiableBoundedCollection((BoundedCollection) coll);
  83. }
  84. /**
  85. * Constructor that wraps (not copies).
  86. *
  87. * @param coll the collection to decorate, must not be null
  88. * @throws IllegalArgumentException if coll is null
  89. */
  90. private UnmodifiableBoundedCollection(BoundedCollection coll) {
  91. super(coll);
  92. }
  93. //-----------------------------------------------------------------------
  94. public Iterator iterator() {
  95. return UnmodifiableIterator.decorate(getCollection().iterator());
  96. }
  97. public boolean add(Object object) {
  98. throw new UnsupportedOperationException();
  99. }
  100. public boolean addAll(Collection coll) {
  101. throw new UnsupportedOperationException();
  102. }
  103. public void clear() {
  104. throw new UnsupportedOperationException();
  105. }
  106. public boolean remove(Object object) {
  107. throw new UnsupportedOperationException();
  108. }
  109. public boolean removeAll(Collection coll) {
  110. throw new UnsupportedOperationException();
  111. }
  112. public boolean retainAll(Collection coll) {
  113. throw new UnsupportedOperationException();
  114. }
  115. //-----------------------------------------------------------------------
  116. public boolean isFull() {
  117. return ((BoundedCollection) collection).isFull();
  118. }
  119. public int maxSize() {
  120. return ((BoundedCollection) collection).maxSize();
  121. }
  122. }