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. /**
  20. * Decorates another <code>Collection</code> to provide additional behaviour.
  21. * <p>
  22. * Each method call made on this <code>Collection</code> is forwarded to the
  23. * decorated <code>Collection</code>. This class is used as a framework on which
  24. * to build to extensions such as synchronized and unmodifiable behaviour. The
  25. * main advantage of decoration is that one decorator can wrap any implementation
  26. * of <code>Collection</code>, whereas sub-classing requires a new class to be
  27. * written for each implementation.
  28. * <p>
  29. * This implementation does not perform any special processing with
  30. * {@link #iterator()}. Instead it simply returns the value from the
  31. * wrapped collection. This may be undesirable, for example if you are trying
  32. * to write an unmodifiable implementation it might provide a loophole.
  33. *
  34. * @since Commons Collections 3.0
  35. * @version $Revision: 1.4 $ $Date: 2004/06/02 21:53:03 $
  36. *
  37. * @author Stephen Colebourne
  38. * @author Paul Jack
  39. */
  40. public abstract class AbstractCollectionDecorator implements Collection {
  41. /** The collection being decorated */
  42. protected Collection collection;
  43. /**
  44. * Constructor only used in deserialization, do not use otherwise.
  45. * @since Commons Collections 3.1
  46. */
  47. protected AbstractCollectionDecorator() {
  48. super();
  49. }
  50. /**
  51. * Constructor that wraps (not copies).
  52. *
  53. * @param coll the collection to decorate, must not be null
  54. * @throws IllegalArgumentException if the collection is null
  55. */
  56. protected AbstractCollectionDecorator(Collection coll) {
  57. if (coll == null) {
  58. throw new IllegalArgumentException("Collection must not be null");
  59. }
  60. this.collection = coll;
  61. }
  62. /**
  63. * Gets the collection being decorated.
  64. *
  65. * @return the decorated collection
  66. */
  67. protected Collection getCollection() {
  68. return collection;
  69. }
  70. //-----------------------------------------------------------------------
  71. public boolean add(Object object) {
  72. return collection.add(object);
  73. }
  74. public boolean addAll(Collection coll) {
  75. return collection.addAll(coll);
  76. }
  77. public void clear() {
  78. collection.clear();
  79. }
  80. public boolean contains(Object object) {
  81. return collection.contains(object);
  82. }
  83. public boolean isEmpty() {
  84. return collection.isEmpty();
  85. }
  86. public Iterator iterator() {
  87. return collection.iterator();
  88. }
  89. public boolean remove(Object object) {
  90. return collection.remove(object);
  91. }
  92. public int size() {
  93. return collection.size();
  94. }
  95. public Object[] toArray() {
  96. return collection.toArray();
  97. }
  98. public Object[] toArray(Object[] object) {
  99. return collection.toArray(object);
  100. }
  101. public boolean containsAll(Collection coll) {
  102. return collection.containsAll(coll);
  103. }
  104. public boolean removeAll(Collection coll) {
  105. return collection.removeAll(coll);
  106. }
  107. public boolean retainAll(Collection coll) {
  108. return collection.retainAll(coll);
  109. }
  110. public boolean equals(Object object) {
  111. if (object == this) {
  112. return true;
  113. }
  114. return collection.equals(object);
  115. }
  116. public int hashCode() {
  117. return collection.hashCode();
  118. }
  119. public String toString() {
  120. return collection.toString();
  121. }
  122. }