/**
 * @license
 * Copyright 2017 The FOAM Authors. All Rights Reserved.
 * http://www.apache.org/licenses/LICENSE-2.0
 */

package foam.core;

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

public class MultitonInfo<T>
  implements Axiom, ContextFactory<T>
{
  Map<Object, T> instanceMap = new HashMap<Object, T>();

  String name;
  PropertyInfo p;

  public MultitonInfo(String name, PropertyInfo p) {
    this.name = name;
    this.p = p;
  }

  public String getName() {
    return name;
  }

  public T getInstance(Map<String, Object> args, X x) {
    Object key = args.get(p.getName());
    if ( ! instanceMap.containsKey(key) ) {
      try {
        Class<T> type = (Class<T>)p.getClassInfo().getObjClass();
        T obj = type.newInstance();
        ((ContextAware)obj).setX(x);
        for (Map.Entry<String, Object> entry : args.entrySet()) {
          ((FObject)obj).setProperty(entry.getKey(), entry.getValue());
        }
        instanceMap.put(key, obj);
      } catch (java.lang.Exception e) {
        e.printStackTrace();
        return null;
      }
    }
    return instanceMap.get(key);
  }
}

@@ -502,16 +502,29 @@
  public 
+synchronized 
 T getIns
@@ -810,16 +810,17 @@
    for (
+ 
 Map.Entr
@@ -860,16 +860,17 @@
 trySet()
+ 
 ) %7B%0A    

Make multiton creation thread safe and fix some code style.