From 242da910d0f9823e58fafd33325b793d4ad9ea50 Mon Sep 17 00:00:00 2001 From: jonalv Date: Thu, 3 Sep 2009 21:23:13 +0800 Subject: [PATCH] Made InChIGeneratorFactory a singleton. Refactored InChIGeneratorFactory into a (thread safe) singleton. Signed-off-by: Egon Willighagen --- .../cdk/inchi/InChIGeneratorFactory.java | 38 ++++++++++++++++--- .../cdk/inchi/InChIGeneratorTest.java | 2 +- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/src/main/org/openscience/cdk/inchi/InChIGeneratorFactory.java b/src/main/org/openscience/cdk/inchi/InChIGeneratorFactory.java index 3e2753ee3d4..fbe14090c77 100644 --- a/src/main/org/openscience/cdk/inchi/InChIGeneratorFactory.java +++ b/src/main/org/openscience/cdk/inchi/InChIGeneratorFactory.java @@ -1,7 +1,8 @@ /* $Revision$ $Author$ $Date$ + * * Copyright (C) 2006-2007 Sam Adams - * + * 2009 Jonathan Alvarsson * Contact: cdk-devel@lists.sourceforge.net * * This program is free software; you can redistribute it and/or @@ -32,11 +33,19 @@ *

Factory providing access to InChIGenerator and InChIToStructure. See those * classes for examples of use. These methods make use of the JNI-InChI library. * + *

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: + *

+ * InchiGeneratorFactory factory = InchiGeneratorFactory.getInstance();
+ * 
+ * *

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 getInstance + * 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 @@ -54,6 +63,8 @@ * @cdk.githash */ public class InChIGeneratorFactory { + + private static InChIGeneratorFactory INSTANCE; /** *

Constructor for InChIGeneratorFactory. Ensures that native code @@ -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) { @@ -71,6 +82,23 @@ public InChIGeneratorFactory() throws CDKException { } } + /** + * Gives the one InChIGeneratorFactory instance, + * if needed also creates it. + * + * @return the one InChIGeneratorFactory 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. * diff --git a/src/test/org/openscience/cdk/inchi/InChIGeneratorTest.java b/src/test/org/openscience/cdk/inchi/InChIGeneratorTest.java index 20dcbed7075..983331a9361 100644 --- a/src/test/org/openscience/cdk/inchi/InChIGeneratorTest.java +++ b/src/test/org/openscience/cdk/inchi/InChIGeneratorTest.java @@ -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); }