View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.util;
21  
22  import java.util.Collection;
23  import java.util.Map;
24  import java.util.Set;
25  import java.util.concurrent.ConcurrentHashMap;
26  import java.util.concurrent.ConcurrentMap;
27  
28  import org.apache.mina.core.buffer.IoBuffer;
29  
30  /**
31   * This map is specially useful when reads are much more frequent than writes and 
32   * if the cost of instantiating the values is high like allocating an 
33   * {@link IoBuffer} for example.
34   *  
35   * Based on the final implementation of Memoizer written by Brian Goetz and Tim
36   * Peierls. This implementation will return an
37   * {@link UnsupportedOperationException} on each method that is not intended to
38   * be called by user code for performance reasons.
39   * 
40   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
41   * @since MINA 2.0.0-M2
42   */
43  public class LazyInitializedCacheMap<K, V> implements Map<K, V> {
44      private ConcurrentMap<K, LazyInitializer<V>> cache;
45  
46      /**
47       * This class provides a noop {@link LazyInitializer} meaning it 
48       * will return the same object it received when instantiated.
49       */
50      public class NoopInitializer extends LazyInitializer<V> {
51          private V value;
52  
53          public NoopInitializer(V value) {
54              this.value = value;
55          }
56  
57          public V init() {
58              return value;
59          }
60      }
61  
62      /**
63       * Default constructor. Uses the default parameters to initialize its internal
64       * {@link ConcurrentHashMap}.
65       */
66      public LazyInitializedCacheMap() {
67          this.cache = new ConcurrentHashMap<K, LazyInitializer<V>>();
68      }
69  
70      /**
71       * This constructor allows to provide a fine tuned {@link ConcurrentHashMap}
72       * to stick with each special case the user needs.
73       * 
74       * @param map The map to use as a cache
75       */
76      public LazyInitializedCacheMap(final ConcurrentHashMap<K, LazyInitializer<V>> map) {
77          this.cache = map;
78      }
79  
80      /**
81       * {@inheritDoc}
82       */
83      public V get(Object key) {
84          LazyInitializer<V> c = cache.get(key);
85          if (c != null) {
86              return c.get();
87          }
88  
89          return null;
90      }
91  
92      /**
93       * {@inheritDoc}
94       */
95      public V remove(Object key) {
96          LazyInitializer<V> c = cache.remove(key);
97          if (c != null) {
98              return c.get();
99          }
100 
101         return null;
102     }
103 
104     /**
105      * If the specified key is not already associated
106      * with a value, associate it with the given value.
107      * This is equivalent to
108      * <pre>
109      *   if (!map.containsKey(key))
110      *       return map.put(key, value);
111      *   else
112      *       return map.get(key);</pre>
113      * except that the action is performed atomically.
114      *
115      * @param key key with which the specified value is to be associated
116      * @param value a lazy initialized value object.
117      * 
118      * @return the previous value associated with the specified key,
119      *         or <tt>null</tt> if there was no mapping for the key
120      */
121     public V putIfAbsent(K key, LazyInitializer<V> value) {
122         LazyInitializer<V> v = cache.get(key);
123         if (v == null) {
124             v = cache.putIfAbsent(key, value);
125             if (v == null) {
126                 return value.get();
127             }
128         }
129 
130         return v.get();
131     }
132 
133     /**
134      * {@inheritDoc}
135      */
136     public V put(K key, V value) {
137         LazyInitializer<V> c = cache.put(key, new NoopInitializer(value));
138         if (c != null) {
139             return c.get();
140         }
141 
142         return null;
143     }
144 
145     /**
146      * Throws {@link UnsupportedOperationException} as this method would imply
147      *         performance drops.
148      */
149     public boolean containsValue(Object value) {
150         throw new UnsupportedOperationException();
151     }
152 
153     /**
154      * Throws {@link UnsupportedOperationException} as this method would imply
155      *         performance drops.
156      */
157     public Collection<V> values() {
158         throw new UnsupportedOperationException();
159     }
160 
161     /**
162      * Throws {@link UnsupportedOperationException} as this method would imply
163      *         performance drops.
164      */
165     public Set<java.util.Map.Entry<K, V>> entrySet() {
166         throw new UnsupportedOperationException();
167     }
168 
169     /**
170      * {@inheritDoc}
171      */
172     public void putAll(Map<? extends K, ? extends V> m) {
173         for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) {
174             cache.put(e.getKey(), new NoopInitializer(e.getValue()));
175         }
176     }
177 
178     /**
179      * @return return the values from the cache
180      */
181     public Collection<LazyInitializer<V>> getValues() {
182         return cache.values();
183     }
184 
185     /**
186      * {@inheritDoc}
187      */
188     public void clear() {
189         cache.clear();
190     }
191 
192     /**
193      * {@inheritDoc}
194      */
195     public boolean containsKey(Object key) {
196         return cache.containsKey(key);
197     }
198 
199     /**
200      * {@inheritDoc}
201      */
202     public boolean isEmpty() {
203         return cache.isEmpty();
204     }
205 
206     /**
207      * {@inheritDoc}
208      */
209     public Set<K> keySet() {
210         return cache.keySet();
211     }
212 
213     /**
214      * {@inheritDoc}
215      */
216     public int size() {
217         return cache.size();
218     }
219 }