KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > map > LazySortedMap


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
18 import java.util.Comparator JavaDoc;
19 import java.util.SortedMap JavaDoc;
20
21 import org.apache.commons.collections.Factory;
22 import org.apache.commons.collections.Transformer;
23
24 /**
25  * Decorates another <code>SortedMap</code> to create objects in the map on demand.
26  * <p>
27  * When the {@link #get(Object)} method is called with a key that does not
28  * exist in the map, the factory is used to create the object. The created
29  * object will be added to the map using the requested key.
30  * <p>
31  * For instance:
32  * <pre>
33  * Factory factory = new Factory() {
34  * public Object create() {
35  * return new Date();
36  * }
37  * }
38  * SortedMap lazy = Lazy.sortedMap(new HashMap(), factory);
39  * Object obj = lazy.get("NOW");
40  * </pre>
41  *
42  * After the above code is executed, <code>obj</code> will contain
43  * a new <code>Date</code> instance. Furthermore, that <code>Date</code>
44  * instance is mapped to the "NOW" key in the map.
45  * <p>
46  * This class is Serializable from Commons Collections 3.1.
47  *
48  * @since Commons Collections 3.0
49  * @version $Revision: 1.6 $ $Date: 2004/04/09 10:36:01 $
50  *
51  * @author Stephen Colebourne
52  * @author Paul Jack
53  */

54 public class LazySortedMap
55         extends LazyMap
56         implements SortedMap JavaDoc {
57
58     /** Serialization version */
59     private static final long serialVersionUID = 2715322183617658933L;
60
61     /**
62      * Factory method to create a lazily instantiated sorted map.
63      *
64      * @param map the map to decorate, must not be null
65      * @param factory the factory to use, must not be null
66      * @throws IllegalArgumentException if map or factory is null
67      */

68     public static SortedMap JavaDoc decorate(SortedMap JavaDoc map, Factory factory) {
69         return new LazySortedMap(map, factory);
70     }
71
72     /**
73      * Factory method to create a lazily instantiated sorted map.
74      *
75      * @param map the map to decorate, must not be null
76      * @param factory the factory to use, must not be null
77      * @throws IllegalArgumentException if map or factory is null
78      */

79     public static SortedMap JavaDoc decorate(SortedMap JavaDoc map, Transformer factory) {
80         return new LazySortedMap(map, factory);
81     }
82
83     //-----------------------------------------------------------------------
84
/**
85      * Constructor that wraps (not copies).
86      *
87      * @param map the map to decorate, must not be null
88      * @param factory the factory to use, must not be null
89      * @throws IllegalArgumentException if map or factory is null
90      */

91     protected LazySortedMap(SortedMap JavaDoc map, Factory factory) {
92         super(map, factory);
93     }
94
95     /**
96      * Constructor that wraps (not copies).
97      *
98      * @param map the map to decorate, must not be null
99      * @param factory the factory to use, must not be null
100      * @throws IllegalArgumentException if map or factory is null
101      */

102     protected LazySortedMap(SortedMap JavaDoc map, Transformer factory) {
103         super(map, factory);
104     }
105
106     //-----------------------------------------------------------------------
107
/**
108      * Gets the map being decorated.
109      *
110      * @return the decorated map
111      */

112     protected SortedMap JavaDoc getSortedMap() {
113         return (SortedMap JavaDoc) map;
114     }
115
116     //-----------------------------------------------------------------------
117
public Object JavaDoc firstKey() {
118         return getSortedMap().firstKey();
119     }
120
121     public Object JavaDoc lastKey() {
122         return getSortedMap().lastKey();
123     }
124
125     public Comparator JavaDoc comparator() {
126         return getSortedMap().comparator();
127     }
128
129     public SortedMap JavaDoc subMap(Object JavaDoc fromKey, Object JavaDoc toKey) {
130         SortedMap JavaDoc map = getSortedMap().subMap(fromKey, toKey);
131         return new LazySortedMap(map, factory);
132     }
133
134     public SortedMap JavaDoc headMap(Object JavaDoc toKey) {
135         SortedMap JavaDoc map = getSortedMap().headMap(toKey);
136         return new LazySortedMap(map, factory);
137     }
138
139     public SortedMap JavaDoc tailMap(Object JavaDoc fromKey) {
140         SortedMap JavaDoc map = getSortedMap().tailMap(fromKey);
141         return new LazySortedMap(map, factory);
142     }
143
144 }
145
Popular Tags