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.Comparator;
  18. import org.apache.commons.collections.Predicate;
  19. import org.apache.commons.collections.SortedBag;
  20. /**
  21. * Decorates another <code>SortedBag</code> to validate that additions
  22. * match a specified predicate.
  23. * <p>
  24. * This bag exists to provide validation for the decorated bag.
  25. * It is normally created to decorate an empty bag.
  26. * If an object cannot be added to the bag, an IllegalArgumentException is thrown.
  27. * <p>
  28. * One usage would be to ensure that no null entries are added to the bag.
  29. * <pre>SortedBag bag = PredicatedSortedBag.decorate(new TreeBag(), 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.8 $ $Date: 2004/06/03 22:02:12 $
  35. *
  36. * @author Stephen Colebourne
  37. * @author Paul Jack
  38. */
  39. public class PredicatedSortedBag
  40. extends PredicatedBag implements SortedBag {
  41. /** Serialization version */
  42. private static final long serialVersionUID = 3448581314086406616L;
  43. /**
  44. * Factory method to create a predicated (validating) bag.
  45. * <p>
  46. * If there are any elements already in the bag being decorated, they
  47. * are validated.
  48. *
  49. * @param bag the bag to decorate, must not be null
  50. * @param predicate the predicate to use for validation, must not be null
  51. * @return a new predicated SortedBag
  52. * @throws IllegalArgumentException if bag or predicate is null
  53. * @throws IllegalArgumentException if the bag contains invalid elements
  54. */
  55. public static SortedBag decorate(SortedBag bag, Predicate predicate) {
  56. return new PredicatedSortedBag(bag, predicate);
  57. }
  58. //-----------------------------------------------------------------------
  59. /**
  60. * Constructor that wraps (not copies).
  61. * <p>
  62. * If there are any elements already in the bag being decorated, they
  63. * are validated.
  64. *
  65. * @param bag the bag to decorate, must not be null
  66. * @param predicate the predicate to use for validation, must not be null
  67. * @throws IllegalArgumentException if bag or predicate is null
  68. * @throws IllegalArgumentException if the bag contains invalid elements
  69. */
  70. protected PredicatedSortedBag(SortedBag bag, Predicate predicate) {
  71. super(bag, predicate);
  72. }
  73. /**
  74. * Gets the decorated sorted bag.
  75. *
  76. * @return the decorated bag
  77. */
  78. protected SortedBag getSortedBag() {
  79. return (SortedBag) getCollection();
  80. }
  81. //-----------------------------------------------------------------------
  82. public Object first() {
  83. return getSortedBag().first();
  84. }
  85. public Object last() {
  86. return getSortedBag().last();
  87. }
  88. public Comparator comparator() {
  89. return getSortedBag().comparator();
  90. }
  91. }