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.Transformer;
  20. /**
  21. * Decorates another <code>SortedMap </code> to transform objects that are added.
  22. * <p>
  23. * The Map put methods and Map.Entry setValue method are affected by this class.
  24. * Thus objects must be removed or searched for using their transformed form.
  25. * For example, if the transformation converts Strings to Integers, you must
  26. * use the Integer form to remove objects.
  27. * <p>
  28. * This class is Serializable from Commons Collections 3.1.
  29. *
  30. * @since Commons Collections 3.0
  31. * @version $Revision: 1.6 $ $Date: 2004/04/09 10:36:01 $
  32. *
  33. * @author Stephen Colebourne
  34. */
  35. public class TransformedSortedMap
  36. extends TransformedMap
  37. implements SortedMap {
  38. /** Serialization version */
  39. private static final long serialVersionUID = -8751771676410385778L;
  40. /**
  41. * Factory method to create a transforming sorted map.
  42. * <p>
  43. * If there are any elements already in the map being decorated, they
  44. * are NOT transformed.
  45. *
  46. * @param map the map to decorate, must not be null
  47. * @param keyTransformer the predicate to validate the keys, null means no transformation
  48. * @param valueTransformer the predicate to validate to values, null means no transformation
  49. * @throws IllegalArgumentException if the map is null
  50. */
  51. public static SortedMap decorate(SortedMap map, Transformer keyTransformer, Transformer valueTransformer) {
  52. return new TransformedSortedMap(map, keyTransformer, valueTransformer);
  53. }
  54. //-----------------------------------------------------------------------
  55. /**
  56. * Constructor that wraps (not copies).
  57. * <p>
  58. * If there are any elements already in the collection being decorated, they
  59. * are NOT transformed.</p>
  60. *
  61. * @param map the map to decorate, must not be null
  62. * @param keyTransformer the predicate to validate the keys, null means no transformation
  63. * @param valueTransformer the predicate to validate to values, null means no transformation
  64. * @throws IllegalArgumentException if the map is null
  65. */
  66. protected TransformedSortedMap(SortedMap map, Transformer keyTransformer, Transformer valueTransformer) {
  67. super(map, keyTransformer, valueTransformer);
  68. }
  69. //-----------------------------------------------------------------------
  70. /**
  71. * Gets the map being decorated.
  72. *
  73. * @return the decorated map
  74. */
  75. protected SortedMap getSortedMap() {
  76. return (SortedMap) map;
  77. }
  78. //-----------------------------------------------------------------------
  79. public Object firstKey() {
  80. return getSortedMap().firstKey();
  81. }
  82. public Object lastKey() {
  83. return getSortedMap().lastKey();
  84. }
  85. public Comparator comparator() {
  86. return getSortedMap().comparator();
  87. }
  88. public SortedMap subMap(Object fromKey, Object toKey) {
  89. SortedMap map = getSortedMap().subMap(fromKey, toKey);
  90. return new TransformedSortedMap(map, keyTransformer, valueTransformer);
  91. }
  92. public SortedMap headMap(Object toKey) {
  93. SortedMap map = getSortedMap().headMap(toKey);
  94. return new TransformedSortedMap(map, keyTransformer, valueTransformer);
  95. }
  96. public SortedMap tailMap(Object fromKey) {
  97. SortedMap map = getSortedMap().tailMap(fromKey);
  98. return new TransformedSortedMap(map, keyTransformer, valueTransformer);
  99. }
  100. }