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   * @param <K> The key type
41   * @param <V> The value type
42   * 
43   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
44   * @since MINA 2.0.0-M2
45   */
46  public class LazyInitializedCacheMap<K, V> implements Map<K, V> {
47      private ConcurrentMap<K, LazyInitializer<V>> cache;
48  
49      /**
50       * This class provides a noop {@link LazyInitializer} meaning it 
51       * will return the same object it received when instantiated.
52       */
53      public class NoopInitializer extends LazyInitializer<V> {
54          private V value;
55  
56          /**
57           * Create a new NoopInitializer instance
58           * 
59           * @param value The value stored in this initializer
60           */
61          public NoopInitializer(V value) {
62              this.value = value;
63          }
64  
65          /**
66           * {@inheritDoc}
67           */
68          @Override
69          public V init() {
70              return value;
71          }
72      }
73  
74      /**
75       * Default constructor. Uses the default parameters to initialize its internal
76       * {@link ConcurrentHashMap}.
77       */
78      public LazyInitializedCacheMap() {
79          this.cache = new ConcurrentHashMap<>();
80      }
81  
82      /**
83       * This constructor allows to provide a fine tuned {@link ConcurrentHashMap}
84       * to stick with each special case the user needs.
85       * 
86       * @param map The map to use as a cache
87       */
88      public LazyInitializedCacheMap(ConcurrentHashMap<K, LazyInitializer<V>> map) {
89          this.cache = map;
90      }
91  
92      /**
93       * {@inheritDoc}
94       */
95      @Override
96      public V get(Object key) {
97          LazyInitializer<V> c = cache.get(key);
98  
99          if (c != null) {
100             return c.get();
101         }
102 
103         return null;
104     }
105 
106     /**
107      * {@inheritDoc}
108      */
109     @Override
110     public V remove(Object key) {
111         LazyInitializer<V> c = cache.remove(key);
112         
113         if (c != null) {
114             return c.get();
115         }
116 
117         return null;
118     }
119 
120     /**
121      * If the specified key is not already associated
122      * with a value, associate it with the given value.
123      * This is equivalent to
124      * <pre>
125      *   if (!map.containsKey(key))
126      *       return map.put(key, value);
127      *   else
128      *       return map.get(key);</pre>
129      * except that the action is performed atomically.
130      *
131      * @param key key with which the specified value is to be associated
132      * @param value a lazy initialized value object.
133      * 
134      * @return the previous value associated with the specified key,
135      *         or <tt>null</tt> if there was no mapping for the key
136      */
137     public V putIfAbsent(K key, LazyInitializer<V> value) {
138         LazyInitializer<V> v = cache.get(key);
139         
140         if (v == null) {
141             v = cache.putIfAbsent(key, value);
142             
143             if (v == null) {
144                 return value.get();
145             }
146         }
147 
148         return v.get();
149     }
150 
151     /**
152      * {@inheritDoc}
153      */
154     @Override
155     public V put(K key, V value) {
156         LazyInitializer<V> c = cache.put(key, new NoopInitializer(value));
157 
158         if (c != null) {
159             return c.get();
160         }
161 
162         return null;
163     }
164 
165     /**
166      * Throws {@link UnsupportedOperationException} as this method would imply
167      *         performance drops.
168      */
169     @Override
170     public boolean containsValue(Object value) {
171         throw new UnsupportedOperationException();
172     }
173 
174     /**
175      * Throws {@link UnsupportedOperationException} as this method would imply
176      *         performance drops.
177      */
178     @Override
179     public Collection<V> values() {
180         throw new UnsupportedOperationException();
181     }
182 
183     /**
184      * Throws {@link UnsupportedOperationException} as this method would imply
185      *         performance drops.
186      */
187     @Override
188     public Set<java.util.Map.Entry<K, V>> entrySet() {
189         throw new UnsupportedOperationException();
190     }
191 
192     /**
193      * {@inheritDoc}
194      */
195     @Override
196     public void putAll(Map<? extends K, ? extends V> m) {
197         for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) {
198             cache.put(e.getKey(), new NoopInitializer(e.getValue()));
199         }
200     }
201 
202     /**
203      * @return return the values from the cache
204      */
205     public Collection<LazyInitializer<V>> getValues() {
206         return cache.values();
207     }
208 
209     /**
210      * {@inheritDoc}
211      */
212     @Override
213     public void clear() {
214         cache.clear();
215     }
216 
217     /**
218      * {@inheritDoc}
219      */
220     @Override
221     public boolean containsKey(Object key) {
222         return cache.containsKey(key);
223     }
224 
225     /**
226      * {@inheritDoc}
227      */
228     @Override
229     public boolean isEmpty() {
230         return cache.isEmpty();
231     }
232 
233     /**
234      * {@inheritDoc}
235      */
236     @Override
237     public Set<K> keySet() {
238         return cache.keySet();
239     }
240 
241     /**
242      * {@inheritDoc}
243      */
244     @Override
245     public int size() {
246         return cache.size();
247     }
248 }