summaryrefslogtreecommitdiff
path: root/propertysheet/src/org/eclipse/wb/internal/core/utils/reflect/ClassMap.java
blob: ce0d3397e52569fc2b452ebe031a1db3f5ea3ec3 (plain)
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
/*******************************************************************************
 * Copyright (c) 2011 Google, Inc.
 * 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:
 *    Google, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.wb.internal.core.utils.reflect;

import java.util.HashMap;
import java.util.Map;

/**
 * {@link Map}-like interface for mapping {@link Class} to value.
 * 
 * @author scheglov_ke
 * @coverage core.util
 */
public final class ClassMap<V> {
  ////////////////////////////////////////////////////////////////////////////
  //
  // Constructor
  //
  ////////////////////////////////////////////////////////////////////////////
  /**
   * Creates new instance of {@link ClassMap}.
   */
  public static <V> ClassMap<V> create() {
    return new ClassMap<V>();
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Map
  //
  ////////////////////////////////////////////////////////////////////////////
  public void put(Class<?> key, V value) {
    getMap(key).put(key, value);
  }

  public V get(Class<?> key) {
    return getMap(key).get(key);
  }

  public void remove(Class<?> key) {
    getMap(key).remove(key);
  }

  public void clear(ClassLoader classLoader) {
    getMap(classLoader).clear();
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Implementation
  //
  ////////////////////////////////////////////////////////////////////////////
  private Map<Class<?>, V> getMap(Class<?> key) {
    ClassLoader classLoader = key.getClassLoader();
    return getMap(classLoader);
  }

  @SuppressWarnings("unchecked")
  private Map<Class<?>, V> getMap(ClassLoader classLoader) {
    Object map = ClassLoaderLocalMap.get(classLoader, this);
    if (map == null) {
      map = new HashMap<Class<?>, V>();
      ClassLoaderLocalMap.put(classLoader, this, map);
    }
    return (Map<Class<?>, V>) map;
  }
}