KickJava   Java API By Example, From Geeks To Geeks.

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


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

58 public class LazyMap
59         extends AbstractMapDecorator
60         implements Map JavaDoc, Serializable JavaDoc {
61
62     /** Serialization version */
63     private static final long serialVersionUID = 7990956402564206740L;
64
65     /** The factory to use to construct elements */
66     protected final Transformer factory;
67
68     /**
69      * Factory method to create a lazily instantiated map.
70      *
71      * @param map the map to decorate, must not be null
72      * @param factory the factory to use, must not be null
73      * @throws IllegalArgumentException if map or factory is null
74      */

75     public static Map JavaDoc decorate(Map JavaDoc map, Factory factory) {
76         return new LazyMap(map, factory);
77     }
78
79     /**
80      * Factory method to create a lazily instantiated map.
81      *
82      * @param map the map to decorate, must not be null
83      * @param factory the factory to use, must not be null
84      * @throws IllegalArgumentException if map or factory is null
85      */

86     public static Map JavaDoc decorate(Map JavaDoc map, Transformer factory) {
87         return new LazyMap(map, factory);
88     }
89
90     //-----------------------------------------------------------------------
91
/**
92      * Constructor that wraps (not copies).
93      *
94      * @param map the map to decorate, must not be null
95      * @param factory the factory to use, must not be null
96      * @throws IllegalArgumentException if map or factory is null
97      */

98     protected LazyMap(Map JavaDoc map, Factory factory) {
99         super(map);
100         if (factory == null) {
101             throw new IllegalArgumentException JavaDoc("Factory must not be null");
102         }
103         this.factory = FactoryTransformer.getInstance(factory);
104     }
105
106     /**
107      * Constructor that wraps (not copies).
108      *
109      * @param map the map to decorate, must not be null
110      * @param factory the factory to use, must not be null
111      * @throws IllegalArgumentException if map or factory is null
112      */

113     protected LazyMap(Map JavaDoc map, Transformer factory) {
114         super(map);
115         if (factory == null) {
116             throw new IllegalArgumentException JavaDoc("Factory must not be null");
117         }
118         this.factory = factory;
119     }
120
121     //-----------------------------------------------------------------------
122
/**
123      * Write the map out using a custom routine.
124      *
125      * @param out the output stream
126      * @throws IOException
127      * @since Commons Collections 3.1
128      */

129     private void writeObject(ObjectOutputStream JavaDoc out) throws IOException JavaDoc {
130         out.defaultWriteObject();
131         out.writeObject(map);
132     }
133
134     /**
135      * Read the map in using a custom routine.
136      *
137      * @param in the input stream
138      * @throws IOException
139      * @throws ClassNotFoundException
140      * @since Commons Collections 3.1
141      */

142     private void readObject(ObjectInputStream JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
143         in.defaultReadObject();
144         map = (Map JavaDoc) in.readObject();
145     }
146
147     //-----------------------------------------------------------------------
148
public Object JavaDoc get(Object JavaDoc key) {
149         // create value for key if key is not currently in the map
150
if (map.containsKey(key) == false) {
151             Object JavaDoc value = factory.transform(key);
152             map.put(key, value);
153             return value;
154         }
155         return map.get(key);
156     }
157
158     // no need to wrap keySet, entrySet or values as they are views of
159
// existing map entries - you can't do a map-style get on them.
160
}
161
Popular Tags