X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fanalysis%2FGeneticCodes.java;fp=src%2Fjalview%2Fanalysis%2FGeneticCodes.java;h=69a3846c97b7995f68fa8e271f0370f119064710;hb=a83adb45bdf9554e270921b4baad94defd314b36;hp=137b7f8316e8b21e4e75b581fd81dfbe65e30c18;hpb=d4ec118f86b5c9dee801e743c46aaacc7bb521d1;p=jalview.git diff --git a/src/jalview/analysis/GeneticCodes.java b/src/jalview/analysis/GeneticCodes.java index 137b7f8..69a3846 100644 --- a/src/jalview/analysis/GeneticCodes.java +++ b/src/jalview/analysis/GeneticCodes.java @@ -12,13 +12,30 @@ import java.util.Map; import java.util.StringTokenizer; /** - * A singleton that provides instances of genetic code translation tables + * A static class 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 = "\""; @@ -39,42 +56,31 @@ public final class GeneticCodes private static final String RESOURCE_FILE = "/GeneticCodes.dat"; - private static GeneticCodes instance = new GeneticCodes(); - - private Map ambiguityCodes; + private static final Map ambiguityCodes; /* * loaded code tables, with keys in order of loading */ - private Map codeTables; + private static final Map codeTables; - /** - * Private constructor enforces singleton - */ - private GeneticCodes() + static { - if (instance == null) - { - ambiguityCodes = new HashMap<>(); + 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); } /** - * Returns the singleton instance of this class - * - * @return + * Private constructor enforces no instantiation */ - public static GeneticCodes getInstance() + private GeneticCodes() { - return instance; } /** @@ -82,41 +88,30 @@ public final class GeneticCodes * * @return */ - public Iterable getCodeTables() + public static Iterable getCodeTables() { return codeTables.values(); } /** - * Answers the code table with the given id + * Answers the code table with the given id -- test suite only * * @param id * @return */ - public GeneticCodeI getCodeTable(String id) + public static 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 */ - protected void loadCodes(String fileName) + private static void loadCodes(String fileName) { try { - InputStream is = getClass().getResourceAsStream(fileName); + InputStream is = GeneticCodes.class.getResourceAsStream(fileName); if (is == null) { System.err.println("Resource file not found: " + fileName); @@ -163,11 +158,11 @@ public final class GeneticCodes * * @param fileName */ - protected void loadAmbiguityCodes(String fileName) + private static void loadAmbiguityCodes(String fileName) { try { - InputStream is = getClass().getResourceAsStream(fileName); + InputStream is = GeneticCodes.class.getResourceAsStream(fileName); if (is == null) { System.err.println("Resource file not found: " + fileName); @@ -209,7 +204,7 @@ public final class GeneticCodes * @return * @throws IOException */ - protected String readLine(BufferedReader dataIn) throws IOException + private static String readLine(BufferedReader dataIn) throws IOException { String line = dataIn.readLine(); while (line != null && line.startsWith("#")) @@ -247,7 +242,8 @@ public final class GeneticCodes * @return * @throws IOException */ - protected String loadOneTable(BufferedReader dataIn) throws IOException + private static String loadOneTable(BufferedReader dataIn) + throws IOException { String name = null; String id = null; @@ -307,7 +303,7 @@ public final class GeneticCodes * @param name * @param codons */ - protected void registerCodeTable(final String id, final String name, + private static void registerCodeTable(final String id, final String name, final Map codons) { codeTables.put(id, new GeneticCodeI() @@ -365,7 +361,7 @@ public final class GeneticCodes * @param codeTable * @return */ - protected String getAmbiguousTranslation(String codon, + protected static String getAmbiguousTranslation(String codon, Map ambiguous, GeneticCodeI codeTable) { if (codon.length() != CODON_LENGTH) @@ -423,4 +419,5 @@ public final class GeneticCodes ambiguous.put(codon, peptide); return peptide; } + }