Skip to content

Commit

Permalink
Lazy loading of MACCS keys.
Browse files Browse the repository at this point in the history
Signed-off-by: Stephan Beisken <sbeisken@gmail.com>
Signed-off-by: Egon Willighagen <egonw@users.sourceforge.net>
  • Loading branch information
johnmay authored and egonw committed Dec 18, 2013
1 parent f4d21cf commit b6cf711
Showing 1 changed file with 30 additions and 11 deletions.
41 changes: 30 additions & 11 deletions src/main/org/openscience/cdk/fingerprint/MACCSFingerprinter.java
Expand Up @@ -79,25 +79,20 @@
public class MACCSFingerprinter implements IFingerprinter {
private static ILoggingTool logger =
LoggingToolFactory.createLoggingTool(MACCSFingerprinter.class);
private MaccsKey[] keys = null;

private volatile MaccsKey[] keys = null;

@TestMethod("testFingerprint")
public MACCSFingerprinter() {
try {
keys = readKeyDef();
} catch (IOException e) {
logger.debug(e);
} catch (CDKException e) {
logger.debug(e);
}

}

/** {@inheritDoc} */
@TestMethod("testFingerprint,testfp2")
public IBitFingerprint getBitFingerprint(IAtomContainer atomContainer)
throws CDKException {
if (keys == null)
throw new CDKException("Could not setup key definitions");

MaccsKey[] keys = keys();

int bitsetLength = keys.length;
BitSet fingerPrint = new BitSet(bitsetLength);
Expand Down Expand Up @@ -214,5 +209,29 @@ public ICountFingerprint getCountFingerprint(IAtomContainer container)
throws CDKException {
throw new UnsupportedOperationException();
}


private final Object lock = new Object();

/**
* Access MACCS keys definitions.
*
* @return array of MACCS keys.
* @throws CDKException maccs keys could not be loaded
*/
private MaccsKey[] keys() throws CDKException {
MaccsKey[] result = keys;
if (result == null) {
synchronized (lock) {
result = keys;
if (result == null) {
try {
keys = result = readKeyDef();
} catch (IOException e) {
throw new CDKException("could not read MACCS definitions", e);
}
}
}
}
return result;
}
}

0 comments on commit b6cf711

Please sign in to comment.