JAL-3210 Barebones gradle/buildship/eclipse. See README
[jalview.git] / src / jalview / analysis / GeneticCodes.java
index 69a3846..137b7f8 100644 (file)
@@ -12,30 +12,13 @@ import java.util.Map;
 import java.util.StringTokenizer;
 
 /**
- * A static class that provides instances of genetic code translation tables
+ * A singleton that provides instances of genetic code translation tables
  * 
  * @author gmcarstairs
  * @see https://www.ncbi.nlm.nih.gov/Taxonomy/Utils/wprintgc.cgi
  */
 public final class GeneticCodes
 {
-
-  /**
-   * As implemented, this has to be the first table defined in the data file.
-   */
-  private static GeneticCodeI standardTable;
-
-  /**
-   * 
-   * @return the standard code table (table 1)
-   */
-  public static GeneticCodeI getStandardCodeTable()
-  {
-    return (standardTable == null
-            ? standardTable = codeTables.values().iterator().next()
-            : standardTable);
-  }
-
   private static final int CODON_LENGTH = 3;
 
   private static final String QUOTE = "\"";
@@ -56,31 +39,42 @@ public final class GeneticCodes
 
   private static final String RESOURCE_FILE = "/GeneticCodes.dat";
 
-  private static final Map<String, String> ambiguityCodes;
+  private static GeneticCodes instance = new GeneticCodes();
+
+  private Map<String, String> ambiguityCodes;
 
   /*
    * loaded code tables, with keys in order of loading 
    */
-  private static final Map<String, GeneticCodeI> codeTables;
+  private Map<String, GeneticCodeI> codeTables;
 
-  static
+  /**
+   * Private constructor enforces singleton
+   */
+  private GeneticCodes()
   {
-    ambiguityCodes = new HashMap<>();
+    if (instance == null)
+    {
+      ambiguityCodes = new HashMap<>();
 
-    /*
-     * LinkedHashMap preserves order of addition of entries,
-     * so we can assume the Standard Code Table is the first
-     */
-    codeTables = new LinkedHashMap<>();
-    loadAmbiguityCodes(AMBIGUITY_CODES_FILE);
-    loadCodes(RESOURCE_FILE);
+      /*
+       * LinkedHashMap preserves order of addition of entries,
+       * so we can assume the Standard Code Table is the first
+       */
+      codeTables = new LinkedHashMap<>();
+      loadAmbiguityCodes(AMBIGUITY_CODES_FILE);
+      loadCodes(RESOURCE_FILE);
+    }
   }
 
   /**
-   * Private constructor enforces no instantiation
+   * Returns the singleton instance of this class
+   * 
+   * @return
    */
-  private GeneticCodes()
+  public static GeneticCodes getInstance()
   {
+    return instance;
   }
 
   /**
@@ -88,30 +82,41 @@ public final class GeneticCodes
    * 
    * @return
    */
-  public static Iterable<GeneticCodeI> getCodeTables()
+  public Iterable<GeneticCodeI> getCodeTables()
   {
     return codeTables.values();
   }
 
   /**
-   * Answers the code table with the given id -- test suite only
+   * Answers the code table with the given id
    * 
    * @param id
    * @return
    */
-  public static GeneticCodeI getCodeTable(String id)
+  public GeneticCodeI getCodeTable(String id)
   {
     return codeTables.get(id);
   }
 
   /**
+   * A convenience method that returns the standard code table (table 1). As
+   * implemented, this has to be the first table defined in the data file.
+   * 
+   * @return
+   */
+  public GeneticCodeI getStandardCodeTable()
+  {
+    return codeTables.values().iterator().next();
+  }
+
+  /**
    * Loads the code tables from a data file
    */
-  private static void loadCodes(String fileName)
+  protected void loadCodes(String fileName)
   {
     try
     {
-      InputStream is = GeneticCodes.class.getResourceAsStream(fileName);
+      InputStream is = getClass().getResourceAsStream(fileName);
       if (is == null)
       {
         System.err.println("Resource file not found: " + fileName);
@@ -158,11 +163,11 @@ public final class GeneticCodes
    * 
    * @param fileName
    */
-  private static void loadAmbiguityCodes(String fileName)
+  protected void loadAmbiguityCodes(String fileName)
   {
     try
     {
-      InputStream is = GeneticCodes.class.getResourceAsStream(fileName);
+      InputStream is = getClass().getResourceAsStream(fileName);
       if (is == null)
       {
         System.err.println("Resource file not found: " + fileName);
@@ -204,7 +209,7 @@ public final class GeneticCodes
    * @return
    * @throws IOException
    */
-  private static String readLine(BufferedReader dataIn) throws IOException
+  protected String readLine(BufferedReader dataIn) throws IOException
   {
     String line = dataIn.readLine();
     while (line != null && line.startsWith("#"))
@@ -242,8 +247,7 @@ public final class GeneticCodes
    * @return
    * @throws IOException
    */
-  private static String loadOneTable(BufferedReader dataIn)
-          throws IOException
+  protected String loadOneTable(BufferedReader dataIn) throws IOException
   {
     String name = null;
     String id = null;
@@ -303,7 +307,7 @@ public final class GeneticCodes
    * @param name
    * @param codons
    */
-  private static void registerCodeTable(final String id, final String name,
+  protected void registerCodeTable(final String id, final String name,
           final Map<String, String> codons)
   {
     codeTables.put(id, new GeneticCodeI()
@@ -361,7 +365,7 @@ public final class GeneticCodes
    * @param codeTable
    * @return
    */
-  protected static String getAmbiguousTranslation(String codon,
+  protected String getAmbiguousTranslation(String codon,
           Map<String, String> ambiguous, GeneticCodeI codeTable)
   {
     if (codon.length() != CODON_LENGTH)
@@ -419,5 +423,4 @@ public final class GeneticCodes
     ambiguous.put(codon, peptide);
     return peptide;
   }
-
 }