001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 */
021package org.biojava.nbio.structure.cath;
022
023import org.biojava.nbio.structure.scop.ScopFactory;
024
025import java.util.HashMap;
026import java.util.Map;
027
028/**
029 * Controls global {@link CathDatabase CathDatabases} being used.
030 * Implements a multiton pattern through {@link #getCathDatabase(String)},
031 * and a singleton pattern through {@link #getCathDatabase()}.
032 * @author dmyersturnbull
033 * @see ScopFactory
034 * @see CathInstallation
035 */
036public class CathFactory {
037
038        public static final String VERSION_3_5_0 = "3_5_0";
039        public static final String VERSION_4_0_0 = "4_0_0";
040        public static final String VERSION_4_1_0 = "4_1_0";
041        public static final String LATEST_VERSION = VERSION_4_1_0;
042
043        public static final String DEFAULT_VERSION = LATEST_VERSION;
044
045        private static CathDatabase cath;
046
047        private static Map<String, CathDatabase> versions = new HashMap<String, CathDatabase>();
048
049        /**
050         * Sets the default (singleton) CathDatabase.
051         */
052        public static void setCath(CathDatabase cath) {
053                CathFactory.cath = cath;
054        }
055
056        /**
057         * Returns the default (singleton) CathDatabase.
058         * If the database is null, this will recreate it (lazy initialization).
059         */
060        public static CathDatabase getCathDatabase() {
061                if (cath == null) {
062                        cath = new CathInstallation();
063                }
064                return cath;
065        }
066
067        private CathFactory() {
068
069        }
070
071        /**
072         * Returns a CATH database of the specified version.
073         * @param version For example, "3.5.0"
074         */
075        public static CathDatabase getCathDatabase(String version) {
076                if (version == null) version = DEFAULT_VERSION;
077                CathDatabase cath = versions.get(version);
078                if (cath == null) {
079                        CathInstallation newCath = new CathInstallation();
080                        newCath.setCathVersion(version);
081                        cath = newCath;
082                }
083                return cath;
084        }
085
086}