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.bag;
  17. import java.util.Set;
  18. import org.apache.commons.collections.Bag;
  19. import org.apache.commons.collections.Predicate;
  20. import org.apache.commons.collections.collection.PredicatedCollection;
  21. /**
  22. * Decorates another <code>Bag</code> to validate that additions
  23. * match a specified predicate.
  24. * <p>
  25. * This bag exists to provide validation for the decorated bag.
  26. * It is normally created to decorate an empty bag.
  27. * If an object cannot be added to the bag, an IllegalArgumentException is thrown.
  28. * <p>
  29. * One usage would be to ensure that no null entries are added to the bag.
  30. * <pre>Bag bag = PredicatedBag.decorate(new HashBag(), NotNullPredicate.INSTANCE);</pre>
  31. * <p>
  32. * This class is Serializable from Commons Collections 3.1.
  33. *
  34. * @since Commons Collections 3.0
  35. * @version $Revision: 1.8 $ $Date: 2004/06/03 22:02:12 $
  36. *
  37. * @author Stephen Colebourne
  38. * @author Paul Jack
  39. */
  40. public class PredicatedBag
  41. extends PredicatedCollection implements Bag {
  42. /** Serialization version */
  43. private static final long serialVersionUID = -2575833140344736876L;
  44. /**
  45. * Factory method to create a predicated (validating) bag.
  46. * <p>
  47. * If there are any elements already in the bag being decorated, they
  48. * are validated.
  49. *
  50. * @param bag the bag to decorate, must not be null
  51. * @param predicate the predicate to use for validation, must not be null
  52. * @return a new predicated Bag
  53. * @throws IllegalArgumentException if bag or predicate is null
  54. * @throws IllegalArgumentException if the bag contains invalid elements
  55. */
  56. public static Bag decorate(Bag bag, Predicate predicate) {
  57. return new PredicatedBag(bag, predicate);
  58. }
  59. //-----------------------------------------------------------------------
  60. /**
  61. * Constructor that wraps (not copies).
  62. * <p>
  63. * If there are any elements already in the bag being decorated, they
  64. * are validated.
  65. *
  66. * @param bag the bag to decorate, must not be null
  67. * @param predicate the predicate to use for validation, must not be null
  68. * @throws IllegalArgumentException if bag or predicate is null
  69. * @throws IllegalArgumentException if the bag contains invalid elements
  70. */
  71. protected PredicatedBag(Bag bag, Predicate predicate) {
  72. super(bag, predicate);
  73. }
  74. /**
  75. * Gets the decorated bag.
  76. *
  77. * @return the decorated bag
  78. */
  79. protected Bag getBag() {
  80. return (Bag) getCollection();
  81. }
  82. //-----------------------------------------------------------------------
  83. public boolean add(Object object, int count) {
  84. validate(object);
  85. return getBag().add(object, count);
  86. }
  87. public boolean remove(Object object, int count) {
  88. return getBag().remove(object, count);
  89. }
  90. public Set uniqueSet() {
  91. return getBag().uniqueSet();
  92. }
  93. public int getCount(Object object) {
  94. return getBag().getCount(object);
  95. }
  96. }