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.Factory;
  20. import org.apache.commons.collections.Transformer;
  21. /**
  22. * Decorates another <code>SortedMap</code> to create objects in the map on demand.
  23. * <p>
  24. * When the {@link #get(Object)} method is called with a key that does not
  25. * exist in the map, the factory is used to create the object. The created
  26. * object will be added to the map using the requested key.
  27. * <p>
  28. * For instance:
  29. * <pre>
  30. * Factory factory = new Factory() {
  31. * public Object create() {
  32. * return new Date();
  33. * }
  34. * }
  35. * SortedMap lazy = Lazy.sortedMap(new HashMap(), factory);
  36. * Object obj = lazy.get("NOW");
  37. * </pre>
  38. *
  39. * After the above code is executed, <code>obj</code> will contain
  40. * a new <code>Date</code> instance. Furthermore, that <code>Date</code>
  41. * instance is mapped to the "NOW" key in the map.
  42. * <p>
  43. * This class is Serializable from Commons Collections 3.1.
  44. *
  45. * @since Commons Collections 3.0
  46. * @version $Revision: 1.6 $ $Date: 2004/04/09 10:36:01 $
  47. *
  48. * @author Stephen Colebourne
  49. * @author Paul Jack
  50. */
  51. public class LazySortedMap
  52. extends LazyMap
  53. implements SortedMap {
  54. /** Serialization version */
  55. private static final long serialVersionUID = 2715322183617658933L;
  56. /**
  57. * Factory method to create a lazily instantiated sorted map.
  58. *
  59. * @param map the map to decorate, must not be null
  60. * @param factory the factory to use, must not be null
  61. * @throws IllegalArgumentException if map or factory is null
  62. */
  63. public static SortedMap decorate(SortedMap map, Factory factory) {
  64. return new LazySortedMap(map, factory);
  65. }
  66. /**
  67. * Factory method to create a lazily instantiated sorted map.
  68. *
  69. * @param map the map to decorate, must not be null
  70. * @param factory the factory to use, must not be null
  71. * @throws IllegalArgumentException if map or factory is null
  72. */
  73. public static SortedMap decorate(SortedMap map, Transformer factory) {
  74. return new LazySortedMap(map, factory);
  75. }
  76. //-----------------------------------------------------------------------
  77. /**
  78. * Constructor that wraps (not copies).
  79. *
  80. * @param map the map to decorate, must not be null
  81. * @param factory the factory to use, must not be null
  82. * @throws IllegalArgumentException if map or factory is null
  83. */
  84. protected LazySortedMap(SortedMap map, Factory factory) {
  85. super(map, factory);
  86. }
  87. /**
  88. * Constructor that wraps (not copies).
  89. *
  90. * @param map the map to decorate, must not be null
  91. * @param factory the factory to use, must not be null
  92. * @throws IllegalArgumentException if map or factory is null
  93. */
  94. protected LazySortedMap(SortedMap map, Transformer factory) {
  95. super(map, factory);
  96. }
  97. //-----------------------------------------------------------------------
  98. /**
  99. * Gets the map being decorated.
  100. *
  101. * @return the decorated map
  102. */
  103. protected SortedMap getSortedMap() {
  104. return (SortedMap) map;
  105. }
  106. //-----------------------------------------------------------------------
  107. public Object firstKey() {
  108. return getSortedMap().firstKey();
  109. }
  110. public Object lastKey() {
  111. return getSortedMap().lastKey();
  112. }
  113. public Comparator comparator() {
  114. return getSortedMap().comparator();
  115. }
  116. public SortedMap subMap(Object fromKey, Object toKey) {
  117. SortedMap map = getSortedMap().subMap(fromKey, toKey);
  118. return new LazySortedMap(map, factory);
  119. }
  120. public SortedMap headMap(Object toKey) {
  121. SortedMap map = getSortedMap().headMap(toKey);
  122. return new LazySortedMap(map, factory);
  123. }
  124. public SortedMap tailMap(Object fromKey) {
  125. SortedMap map = getSortedMap().tailMap(fromKey);
  126. return new LazySortedMap(map, factory);
  127. }
  128. }