Skip to content

Commit

Permalink
Synchronise and look atom type matcher.
Browse files Browse the repository at this point in the history
Change-Id: If93b9801521c114b98711857888940ba0599aa71
Signed-off-by: Egon Willighagen <egonw@users.sourceforge.net>
  • Loading branch information
johnmay authored and egonw committed Feb 20, 2015
1 parent 9a4638e commit f7b3388
Showing 1 changed file with 11 additions and 7 deletions.
Expand Up @@ -25,6 +25,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.openscience.cdk.CDKConstants;
import org.openscience.cdk.config.AtomTypeFactory;
Expand Down Expand Up @@ -59,9 +60,10 @@ public class CDKAtomTypeMatcher implements IAtomTypeMatcher {

private AtomTypeFactory factory;
private int mode;

private final static Object lock = new Object();

private static Map<Integer, Map<IChemObjectBuilder, CDKAtomTypeMatcher>> factories = new Hashtable<Integer, Map<IChemObjectBuilder, CDKAtomTypeMatcher>>(
1);
private static Map<Integer, Map<IChemObjectBuilder, CDKAtomTypeMatcher>> factories = new ConcurrentHashMap<>(5);

private CDKAtomTypeMatcher(IChemObjectBuilder builder, int mode) {
factory = AtomTypeFactory.getInstance("org/openscience/cdk/dict/data/cdk-atom-types.owl", builder);
Expand All @@ -73,11 +75,13 @@ public static CDKAtomTypeMatcher getInstance(IChemObjectBuilder builder) {
}

public static CDKAtomTypeMatcher getInstance(IChemObjectBuilder builder, int mode) {
if (!factories.containsKey(mode))
factories.put(mode, new Hashtable<IChemObjectBuilder, CDKAtomTypeMatcher>(1));
if (!factories.get(mode).containsKey(builder))
factories.get(mode).put(builder, new CDKAtomTypeMatcher(builder, mode));
return factories.get(mode).get(builder);
synchronized (lock) {
if (!factories.containsKey(mode))
factories.put(mode, new Hashtable<IChemObjectBuilder, CDKAtomTypeMatcher>(1));
if (!factories.get(mode).containsKey(builder))
factories.get(mode).put(builder, new CDKAtomTypeMatcher(builder, mode));
return factories.get(mode).get(builder);
}
}

/** {@inheritDoc} */
Expand Down

0 comments on commit f7b3388

Please sign in to comment.