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.map;
  17. import java.util.Comparator;
  18. import java.util.SortedMap;
  19. import org.apache.commons.collections.Predicate;
  20. /**
  21. * Decorates another <code>SortedMap </code> to validate that additions
  22. * match a specified predicate.
  23. * <p>
  24. * This map exists to provide validation for the decorated map.
  25. * It is normally created to decorate an empty map.
  26. * If an object cannot be added to the map, an IllegalArgumentException is thrown.
  27. * <p>
  28. * One usage would be to ensure that no null keys are added to the map.
  29. * <pre>SortedMap map = PredicatedSortedSet.decorate(new TreeMap(), NotNullPredicate.INSTANCE, null);</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/05/21 21:38:49 $
  35. *
  36. * @author Stephen Colebourne
  37. * @author Paul Jack
  38. */
  39. public class PredicatedSortedMap
  40. extends PredicatedMap
  41. implements SortedMap {
  42. /** Serialization version */
  43. private static final long serialVersionUID = 3359846175935304332L;
  44. /**
  45. * Factory method to create a predicated (validating) sorted map.
  46. * <p>
  47. * If there are any elements already in the list being decorated, they
  48. * are validated.
  49. *
  50. * @param map the map to decorate, must not be null
  51. * @param keyPredicate the predicate to validate the keys, null means no check
  52. * @param valuePredicate the predicate to validate to values, null means no check
  53. * @throws IllegalArgumentException if the map is null
  54. */
  55. public static SortedMap decorate(SortedMap map, Predicate keyPredicate, Predicate valuePredicate) {
  56. return new PredicatedSortedMap(map, keyPredicate, valuePredicate);
  57. }
  58. //-----------------------------------------------------------------------
  59. /**
  60. * Constructor that wraps (not copies).
  61. *
  62. * @param map the map to decorate, must not be null
  63. * @param keyPredicate the predicate to validate the keys, null means no check
  64. * @param valuePredicate the predicate to validate to values, null means no check
  65. * @throws IllegalArgumentException if the map is null
  66. */
  67. protected PredicatedSortedMap(SortedMap map, Predicate keyPredicate, Predicate valuePredicate) {
  68. super(map, keyPredicate, valuePredicate);
  69. }
  70. //-----------------------------------------------------------------------
  71. /**
  72. * Gets the map being decorated.
  73. *
  74. * @return the decorated map
  75. */
  76. protected SortedMap getSortedMap() {
  77. return (SortedMap) map;
  78. }
  79. //-----------------------------------------------------------------------
  80. public Object firstKey() {
  81. return getSortedMap().firstKey();
  82. }
  83. public Object lastKey() {
  84. return getSortedMap().lastKey();
  85. }
  86. public Comparator comparator() {
  87. return getSortedMap().comparator();
  88. }
  89. public SortedMap subMap(Object fromKey, Object toKey) {
  90. SortedMap map = getSortedMap().subMap(fromKey, toKey);
  91. return new PredicatedSortedMap(map, keyPredicate, valuePredicate);
  92. }
  93. public SortedMap headMap(Object toKey) {
  94. SortedMap map = getSortedMap().headMap(toKey);
  95. return new PredicatedSortedMap(map, keyPredicate, valuePredicate);
  96. }
  97. public SortedMap tailMap(Object fromKey) {
  98. SortedMap map = getSortedMap().tailMap(fromKey);
  99. return new PredicatedSortedMap(map, keyPredicate, valuePredicate);
  100. }
  101. }