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.Predicate;
  20. /**
  21. * Decorates another <code>Collection</code> to validate that additions
  22. * match a specified predicate.
  23. * <p>
  24. * This collection exists to provide validation for the decorated collection.
  25. * It is normally created to decorate an empty collection.
  26. * If an object cannot be added to the collection, an IllegalArgumentException is thrown.
  27. * <p>
  28. * One usage would be to ensure that no null entries are added to the collection.
  29. * <pre>Collection coll = PredicatedCollection.decorate(new ArrayList(), NotNullPredicate.INSTANCE);</pre>
  30. * <p>
  31. * This class is Serializable from Commons Collections 3.1.
  32. *
  33. * @since Commons Collections 3.0
  34. * @version $Revision: 1.7 $ $Date: 2004/06/03 22:02:13 $
  35. *
  36. * @author Stephen Colebourne
  37. * @author Paul Jack
  38. */
  39. public class PredicatedCollection extends AbstractSerializableCollectionDecorator {
  40. /** Serialization version */
  41. private static final long serialVersionUID = -5259182142076705162L;
  42. /** The predicate to use */
  43. protected final Predicate predicate;
  44. /**
  45. * Factory method to create a predicated (validating) collection.
  46. * <p>
  47. * If there are any elements already in the collection being decorated, they
  48. * are validated.
  49. *
  50. * @param coll the collection to decorate, must not be null
  51. * @param predicate the predicate to use for validation, must not be null
  52. * @return a new predicated collection
  53. * @throws IllegalArgumentException if collection or predicate is null
  54. * @throws IllegalArgumentException if the collection contains invalid elements
  55. */
  56. public static Collection decorate(Collection coll, Predicate predicate) {
  57. return new PredicatedCollection(coll, predicate);
  58. }
  59. //-----------------------------------------------------------------------
  60. /**
  61. * Constructor that wraps (not copies).
  62. * <p>
  63. * If there are any elements already in the collection being decorated, they
  64. * are validated.
  65. *
  66. * @param coll the collection to decorate, must not be null
  67. * @param predicate the predicate to use for validation, must not be null
  68. * @throws IllegalArgumentException if collection or predicate is null
  69. * @throws IllegalArgumentException if the collection contains invalid elements
  70. */
  71. protected PredicatedCollection(Collection coll, Predicate predicate) {
  72. super(coll);
  73. if (predicate == null) {
  74. throw new IllegalArgumentException("Predicate must not be null");
  75. }
  76. this.predicate = predicate;
  77. for (Iterator it = coll.iterator(); it.hasNext(); ) {
  78. validate(it.next());
  79. }
  80. }
  81. /**
  82. * Validates the object being added to ensure it matches the predicate.
  83. * <p>
  84. * The predicate itself should not throw an exception, but return false to
  85. * indicate that the object cannot be added.
  86. *
  87. * @param object the object being added
  88. * @throws IllegalArgumentException if the add is invalid
  89. */
  90. protected void validate(Object object) {
  91. if (predicate.evaluate(object) == false) {
  92. throw new IllegalArgumentException("Cannot add Object '" + object + "' - Predicate rejected it");
  93. }
  94. }
  95. //-----------------------------------------------------------------------
  96. /**
  97. * Override to validate the object being added to ensure it matches
  98. * the predicate.
  99. *
  100. * @param object the object being added
  101. * @return the result of adding to the underlying collection
  102. * @throws IllegalArgumentException if the add is invalid
  103. */
  104. public boolean add(Object object) {
  105. validate(object);
  106. return getCollection().add(object);
  107. }
  108. /**
  109. * Override to validate the objects being added to ensure they match
  110. * the predicate. If any one fails, no update is made to the underlying
  111. * collection.
  112. *
  113. * @param coll the collection being added
  114. * @return the result of adding to the underlying collection
  115. * @throws IllegalArgumentException if the add is invalid
  116. */
  117. public boolean addAll(Collection coll) {
  118. for (Iterator it = coll.iterator(); it.hasNext(); ) {
  119. validate(it.next());
  120. }
  121. return getCollection().addAll(coll);
  122. }
  123. }