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