Skip to content

Commit

Permalink
Made InChIGeneratorFactory a singleton.
Browse files Browse the repository at this point in the history
Refactored InChIGeneratorFactory into a (thread safe) singleton.

Signed-off-by: Egon Willighagen <egon.willighagen@gmail.com>
  • Loading branch information
jonalv authored and egonw committed Sep 3, 2009
1 parent af4fac7 commit 242da91
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
38 changes: 33 additions & 5 deletions src/main/org/openscience/cdk/inchi/InChIGeneratorFactory.java
@@ -1,7 +1,8 @@
/* $Revision$ $Author$ $Date$
*
* Copyright (C) 2006-2007 Sam Adams <sea36@users.sf.net>
*
* 2009 Jonathan Alvarsson <jonalv@users.sf.net>
* Contact: cdk-devel@lists.sourceforge.net
*
* This program is free software; you can redistribute it and/or
Expand Down Expand Up @@ -32,11 +33,19 @@
* <p>Factory providing access to InChIGenerator and InChIToStructure. See those
* classes for examples of use. These methods make use of the JNI-InChI library.
*
* <p>The InchiGeneratorFactory is a singleton class, which means that there
* exists only one instance of the class. An instance of this class is obtained
* with:
* <pre>
* InchiGeneratorFactory factory = InchiGeneratorFactory.getInstance();
* </pre>
*
* <p>InChI/Structure interconversion is implemented in this way so that we can
* check whether or not the native code required is available. If the native
* code cannot be loaded a CDKException will be thrown when the constructor
* is called. The most common problem is that the native code is not in the
* the correct location. Java searches the locations in the PATH environmental
* code cannot be loaded during the first call to <code>getInstance</code>
* method (when the instance is created) a CDKException will be thrown. The
* most common problem is that the native code is not in the * the correct
* location. Java searches the locations in the PATH environmental
* variable, under Windows, and LD_LIBRARY_PATH under Linux, so the JNI-InChI
* native libraries must be in one of these locations. If the JNI-InChI jar file
* is being used and either the current working directory, or '.' are contained
Expand All @@ -54,6 +63,8 @@
* @cdk.githash
*/
public class InChIGeneratorFactory {

private static InChIGeneratorFactory INSTANCE;

/**
* <p>Constructor for InChIGeneratorFactory. Ensures that native code
Expand All @@ -62,7 +73,7 @@ public class InChIGeneratorFactory {
*
* @throws CDKException if unable to load native code
*/
public InChIGeneratorFactory() throws CDKException {
private InChIGeneratorFactory() throws CDKException {
try {
JniInchiWrapper.loadLibrary();
} catch (LoadNativeLibraryException lnle) {
Expand All @@ -71,6 +82,23 @@ public InChIGeneratorFactory() throws CDKException {
}
}

/**
* Gives the one <code>InChIGeneratorFactory</code> instance,
* if needed also creates it.
*
* @return the one <code>InChIGeneratorFactory</code> instance
* @throws CDKException if unable to load native code when attempting
* to create the factory
*/
public static InChIGeneratorFactory getInstance() throws CDKException {
synchronized ( InChIGeneratorFactory.class ) {
if ( INSTANCE == null ) {
INSTANCE = new InChIGeneratorFactory();
}
return INSTANCE;
}
}

/**
* Gets InChI generator for CDK IAtomContainer.
*
Expand Down
2 changes: 1 addition & 1 deletion src/test/org/openscience/cdk/inchi/InChIGeneratorTest.java
Expand Up @@ -42,7 +42,7 @@ public class InChIGeneratorTest extends CDKTestCase {

protected InChIGeneratorFactory getFactory() throws Exception {
if (factory == null) {
factory = new InChIGeneratorFactory();
factory = InChIGeneratorFactory.getInstance();
}
return(factory);
}
Expand Down

0 comments on commit 242da91

Please sign in to comment.