Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: bb69be2f2d86bb58ab23f4fbf977acf8037a98c3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/***************************************************************************
 * Copyright (c) 2004, 2005, 2006 Eike Stepper, Germany.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *    Eike Stepper - initial API and implementation
 **************************************************************************/
package org.eclipse.internal.net4j.util;

import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Implementation note: {@link AbstractCachingMap} does not preserve the
 * "modifyable view" contract of {@link Map#entrySet()} as well as of
 * {@link Map#keySet()}, i.e. they are disconnected sets and modifications
 * applied to them are not applied to their underlying
 * {@link AbstractCachingMap}.
 * <p>
 * 
 * @author Eike Stepper
 */
public abstract class AbstractCachingMap<K, V> extends AbstractDelegatingMap<K, V>
{
  protected AbstractCachingMap()
  {
  }

  protected AbstractCachingMap(Map<? extends K, ? extends V> t)
  {
    super(t);
  }

  public void clear()
  {
    cachedClear();
  }

  public boolean containsKey(Object key)
  {
    return cachedContainsKey(key) || delegatedContainsKey(key);
  }

  public boolean containsValue(Object value)
  {
    return cachedContainsValue(value) || cachedContainsValue(value);
  }

  public Set<Entry<K, V>> entrySet()
  {
    return mergedEntrySet();
  }

  public V get(Object key)
  {
    V result = cachedGet(key);
    if (result == null)
    {
      result = delegatedGet(key);
    }

    return result;
  }

  public boolean isEmpty()
  {
    return cachedIsEmpty() && delegatedIsEmpty();
  }

  public Set<K> keySet()
  {
    return mergedKeySet();
  }

  public V put(K key, V value)
  {
    return cachedPut(key, value);
  }

  public void putAll(Map<? extends K, ? extends V> t)
  {
    cachedPutAll(t);
  }

  public V remove(Object key)
  {
    return cachedRemove(key);
  }

  public int size()
  {
    return keySet().size();
  }

  public Collection<V> values()
  {
    return mergedValues();
  }

  @Override
  public boolean equals(Object obj)
  {
    return mergedEquals(obj);
  }

  @Override
  public int hashCode()
  {
    return mergedHashCode();
  }

  @Override
  public String toString()
  {
    return mergedToString();
  }

  protected void cachedClear()
  {
    getCache().clear();
  }

  protected boolean cachedContainsKey(Object key)
  {
    return getCache().containsKey(key);
  }

  protected boolean cachedContainsValue(Object value)
  {
    return getCache().containsValue(value);
  }

  protected Set<Entry<K, V>> cachedEntrySet()
  {
    return getCache().entrySet();
  }

  protected V cachedGet(Object key)
  {
    return getCache().get(key);
  }

  protected boolean cachedIsEmpty()
  {
    return getCache().isEmpty();
  }

  protected Set<K> cachedKeySet()
  {
    return getCache().keySet();
  }

  protected V cachedPut(K key, V value)
  {
    return getCache().put(key, value);
  }

  protected void cachedPutAll(Map<? extends K, ? extends V> t)
  {
    getCache().putAll(t);
  }

  protected V cachedRemove(Object key)
  {
    return getCache().remove(key);
  }

  protected int cachedSize()
  {
    return getCache().size();
  }

  protected Collection<V> cachedValues()
  {
    return getCache().values();
  }

  protected boolean cachedEquals(Object obj)
  {
    return getCache().equals(obj);
  }

  protected int cachedHashCode()
  {
    return getCache().hashCode();
  }

  protected String cachedToString()
  {
    return getCache().toString();
  }

  protected Set<Entry<K, V>> mergedEntrySet()
  {
    final Map<K, V> merged = new HashMap<K, V>();
    merged.putAll(getDelegate());
    merged.putAll(getCache());
    return merged.entrySet();
  }

  protected Set<K> mergedKeySet()
  {
    final Set<K> merged = new HashSet<K>();
    merged.addAll(getDelegate().keySet());
    merged.addAll(getCache().keySet());
    return merged;
  }

  protected Collection<V> mergedValues()
  {
    final List<V> result = new ArrayList<V>();
    for (K key : keySet())
    {
      result.add(get(key));
    }

    return result;
  }

  /**
   * @see AbstractMap#equals(Object)
   */
  protected boolean mergedEquals(Object o)
  {
    if (o == this)
      return true;

    if (!(o instanceof Map))
      return false;

    Map<K, V> t = (Map<K, V>)o;
    if (t.size() != size())
      return false;

    try
    {
      Iterator<Entry<K, V>> i = entrySet().iterator();
      while (i.hasNext())
      {
        Entry<K, V> e = i.next();
        K key = e.getKey();
        V value = e.getValue();
        if (value == null)
        {
          if (!(t.get(key) == null && t.containsKey(key)))
            return false;
        }
        else
        {
          if (!value.equals(t.get(key)))
            return false;
        }
      }
    }
    catch (ClassCastException unused)
    {
      return false;
    }
    catch (NullPointerException unused)
    {
      return false;
    }

    return true;
  }

  /**
   * @see AbstractMap#hashCode()
   */
  protected int mergedHashCode()
  {
    int h = 0;
    Iterator<Entry<K, V>> i = entrySet().iterator();
    while (i.hasNext())
      h += i.next().hashCode();
    return h;
  }

  /**
   * @see AbstractMap#toString()
   */
  protected String mergedToString()
  {
    StringBuffer buf = new StringBuffer();
    buf.append("{"); //$NON-NLS-1$

    Iterator<Entry<K, V>> i = entrySet().iterator();
    boolean hasNext = i.hasNext();
    while (hasNext)
    {
      Entry<K, V> e = i.next();
      K key = e.getKey();
      V value = e.getValue();
      if (key == this)
        buf.append("(this Map)"); //$NON-NLS-1$
      else
        buf.append(key);
      buf.append("="); //$NON-NLS-1$
      if (value == this)
        buf.append("(this Map)"); //$NON-NLS-1$
      else
        buf.append(value);
      hasNext = i.hasNext();
      if (hasNext)
        buf.append(", "); //$NON-NLS-1$
    }

    buf.append("}"); //$NON-NLS-1$
    return buf.toString();
  }

  protected abstract Map<K, V> getCache();
}

Back to the top