1   /**
2    * Distribution License:
3    * JSword is free software; you can redistribute it and/or modify it under
4    * the terms of the GNU Lesser General Public License, version 2.1 as published by
5    * the Free Software Foundation. This program is distributed in the hope
6    * that it will be useful, but WITHOUT ANY WARRANTY; without even the
7    * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8    * See the GNU Lesser General Public License for more details.
9    *
10   * The License is available on the internet at:
11   *       http://www.gnu.org/copyleft/lgpl.html
12   * or by writing to:
13   *      Free Software Foundation, Inc.
14   *      59 Temple Place - Suite 330
15   *      Boston, MA 02111-1307, USA
16   *
17   * Copyright: 2012
18   *     The copyright to this program is held by it's authors.
19   *
20   * ID: $Id: Versifications.java 2223 2012-01-26 21:28:02Z dmsmith $
21   */
22  package org.crosswire.jsword.versification.system;
23  
24  import java.util.HashMap;
25  import java.util.HashSet;
26  import java.util.Map;
27  import java.util.Set;
28  
29  import org.crosswire.jsword.versification.Versification;
30  
31  /**
32   * The Versifications class manages the creation of Versifications as needed.
33   * It delays the construction of the Versification until getVersification(String name) is called.
34   *
35   * @see gnu.lgpl.License for license details.<br>
36   *      The copyright to this program is held by it's authors.
37   * @author DM Smith [dmsmith555 at yahoo dot com]
38   */
39  public class Versifications {
40  
41      /**
42       * The default Versification for JSword is the KJV.
43       * This is subject to change at any time.
44       */
45      public static final String DEFAULT_V11N = SystemKJV.V11N_NAME;
46  
47      /**
48       * Get the singleton instance of Versifications.
49       *
50       * @return the singleton
51       */
52      public static Versifications instance() {
53          return instance;
54      }
55  
56      /**
57       * Get the default Versification.
58       *
59       * @return the default Versification.
60       */
61      public synchronized Versification getDefaultVersification() {
62          return getVersification(DEFAULT_V11N);
63      }
64  
65      /**
66       * Get the Versification by its name. If name is null then return the default Versification.
67       *
68       * @param name the name of the Versification
69       * @return the Versification or null if it is not known.
70       */
71      public synchronized Versification getVersification(String name) {
72          String actual = name;
73          if (actual == null) {
74              actual = DEFAULT_V11N;
75          }
76  
77          // This class delays the building of a Versification to when it is
78          // actually needed.
79          Versification rs = fluffed.get(actual);
80          if (rs == null) {
81              rs = fluff(actual);
82              if (rs != null) {
83                  fluffed.put(actual, rs);
84              }
85          }
86  
87          return rs;
88      }
89  
90      /**
91       * Determine whether the named Versification is known.
92       *
93       * @param name the name of the Versification
94       * @return true when the Versification is available for use
95       */
96      public synchronized boolean isDefined(String name) {
97          return name == null || known.contains(name);
98      }
99  
100     private Versification fluff(String name) {
101         if (SystemKJV.V11N_NAME.equals(name)) {
102             return new SystemKJV();
103         }
104         if (SystemCatholic.V11N_NAME.equals(name)) {
105             return new SystemCatholic();
106         }
107         if (SystemCatholic2.V11N_NAME.equals(name)) {
108             return new SystemCatholic2();
109         }
110         if (SystemKJVA.V11N_NAME.equals(name)) {
111             return new SystemKJVA();
112         }
113         if (SystemGerman.V11N_NAME.equals(name)) {
114             return new SystemGerman();
115         }
116         if (SystemLeningrad.V11N_NAME.equals(name)) {
117             return new SystemLeningrad();
118         }
119         if (SystemLuther.V11N_NAME.equals(name)) {
120             return new SystemLuther();
121         }
122         if (SystemMT.V11N_NAME.equals(name)) {
123             return new SystemMT();
124         }
125         if (SystemNRSV.V11N_NAME.equals(name)) {
126             return new SystemNRSV();
127         }
128         if (SystemNRSVA.V11N_NAME.equals(name)) {
129             return new SystemNRSVA();
130         }
131         if (SystemSynodal.V11N_NAME.equals(name)) {
132             return new SystemSynodal();
133         }
134         if (SystemSynodalP.V11N_NAME.equals(name)) {
135             return new SystemSynodalP();
136         }
137         return null;
138     }
139 
140     /**
141      * Add a Versification that is not predefined by JSword.
142      *
143      * @param rs the Versification to register
144      */
145     public synchronized void register(Versification rs) {
146         fluffed.put(rs.getName(), rs);
147         known.add(rs.getName());
148     }
149 
150     /**
151      * This class is a singleton, enforced by a private constructor.
152      */
153     private Versifications() {
154         known = new HashSet<String>();
155         known.add(SystemCatholic.V11N_NAME);
156         known.add(SystemCatholic2.V11N_NAME);
157         known.add(SystemKJV.V11N_NAME);
158         known.add(SystemGerman.V11N_NAME);
159         known.add(SystemKJVA.V11N_NAME);
160         known.add(SystemLeningrad.V11N_NAME);
161         known.add(SystemLuther.V11N_NAME);
162         known.add(SystemMT.V11N_NAME);
163         known.add(SystemNRSV.V11N_NAME);
164         known.add(SystemNRSVA.V11N_NAME);
165         known.add(SystemSynodal.V11N_NAME);
166         known.add(SystemSynodalP.V11N_NAME);
167         known.add(SystemVulg.V11N_NAME);
168         fluffed = new HashMap<String, Versification>();
169     }
170 
171     /**
172      * The set of v11n names.
173      */
174     private Set<String> known;
175 
176     /**
177      * The map of instantiated Versifications, given by their names.
178      */
179     private Map<String, Versification> fluffed;
180 
181     private static final Versifications instance = new Versifications();
182 }
183