View Javadoc

1   /**
2    * Copyright 2008 Atlassian Pty Ltd 
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  
17  package com.atlassian.util.concurrent;
18  
19  import static com.atlassian.util.concurrent.Assertions.notNull;
20  
21  import java.lang.ref.ReferenceQueue;
22  import java.lang.ref.WeakReference;
23  import java.util.HashMap;
24  import java.util.concurrent.ConcurrentHashMap;
25  import java.util.concurrent.ConcurrentMap;
26  
27  /**
28   * {@link WeakMemoizer} caches the result of another function. The result is
29   * {@link WeakReference weakly referenced} internally. This is useful if the
30   * result is expensive to compute or the identity of the result is particularly
31   * important.
32   * <p>
33   * If the results from this function are further cached then they will tend to
34   * stay in this cache for longer.
35   * 
36   * @param <K> comparable descriptor, the usual rules for any {@link HashMap} key
37   * apply.
38   * @param <V> the value
39   */
40  class WeakMemoizer<K, V> implements Function<K, V> {
41      static <K, V> WeakMemoizer<K, V> weakMemoizer(final Function<K, V> delegate) {
42          return new WeakMemoizer<K, V>(delegate);
43      }
44  
45      private final ConcurrentMap<K, MappedReference<K, V>> map;
46      private final ReferenceQueue<V> queue = new ReferenceQueue<V>();
47      private final Function<K, V> delegate;
48  
49      /**
50       * Construct a new {@link WeakMemoizer} instance.
51       * 
52       * @param initialCapacity how large the internal map should be initially.
53       * @param delegate for creating the initial values.
54       * @throws IllegalArgumentException if the initial capacity of elements is
55       * negative.
56       */
57      WeakMemoizer(final @NotNull Function<K, V> delegate) {
58          this.map = new ConcurrentHashMap<K, MappedReference<K, V>>();
59          this.delegate = notNull("delegate", delegate);
60      }
61  
62      /**
63       * Get a result for the supplied Descriptor.
64       * 
65       * @param descriptor must not be null
66       * @return descriptor lock
67       */
68      public V get(final K descriptor) {
69          expungeStaleEntries();
70          notNull("descriptor", descriptor);
71          while (true) {
72              final MappedReference<K, V> reference = map.get(descriptor);
73              if (reference != null) {
74                  final V value = reference.get();
75                  if (value != null) {
76                      return value;
77                  }
78                  map.remove(descriptor, reference);
79              }
80              map.putIfAbsent(descriptor, new MappedReference<K, V>(descriptor, delegate.get(descriptor), queue));
81          }
82      }
83  
84      // expunge entries whose value reference has been collected
85      @SuppressWarnings("unchecked")
86      private void expungeStaleEntries() {
87          MappedReference<K, V> ref;
88          // /CLOVER:OFF
89          while ((ref = (MappedReference<K, V>) queue.poll()) != null) {
90              final K key = ref.getDescriptor();
91              if (key == null) {
92                  // DO NOT REMOVE! In theory this should not be necessary as it
93                  // should not be able to be null - but we have seen it happen!
94                  continue;
95              }
96              map.remove(key, ref);
97          }
98          // /CLOVER:ON
99      }
100 
101     /**
102      * A weak reference that maintains a reference to the key so that it can be
103      * removed from the map when the value is garbage collected.
104      */
105     static final class MappedReference<K, V> extends WeakReference<V> {
106         private final K key;
107 
108         public MappedReference(final K key, final V value, final ReferenceQueue<? super V> q) {
109             super(notNull("value", value), q);
110             this.key = notNull("key", key);
111         }
112 
113         final K getDescriptor() {
114             return key;
115         }
116     }
117 }