Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
IntOptionsChoice |
|
| 2.2;2.2 |
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 or later | |
5 | * as published by the Free Software Foundation. This program is distributed | |
6 | * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even | |
7 | * the 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 | * © CrossWire Bible Society, 2005 - 2016 | |
18 | * | |
19 | */ | |
20 | package org.crosswire.common.config; | |
21 | ||
22 | import java.util.Iterator; | |
23 | import java.util.Map; | |
24 | import java.util.ResourceBundle; | |
25 | import java.util.TreeMap; | |
26 | ||
27 | import org.jdom2.Element; | |
28 | ||
29 | /** | |
30 | * A class to convert between strings and objects of a type. | |
31 | * | |
32 | * @see gnu.lgpl.License The GNU Lesser General Public License for details. | |
33 | * @author Joe Walker | |
34 | * @author DM Smith | |
35 | */ | |
36 | 0 | public class IntOptionsChoice extends AbstractReflectedChoice implements MappedChoice<Integer, String> { |
37 | /* | |
38 | * (non-Javadoc) | |
39 | * | |
40 | * @see org.crosswire.common.config.Choice#init(org.jdom2.Element) | |
41 | */ | |
42 | @Override | |
43 | public void init(Element option, ResourceBundle configResources) throws StartupException { | |
44 | 0 | assert configResources != null; |
45 | ||
46 | 0 | super.init(option, configResources); |
47 | ||
48 | 0 | String prefix = getKey() + ".alternative."; |
49 | ||
50 | 0 | options = new TreeMap<Integer, String>(); |
51 | 0 | Iterator<Element> iter = option.getChildren("alternative").iterator(); |
52 | 0 | while (iter.hasNext()) { |
53 | 0 | Element alternative = iter.next(); |
54 | 0 | int number = Integer.parseInt(alternative.getAttributeValue("number")); |
55 | 0 | String name = configResources.getString(prefix + number); |
56 | 0 | options.put(Integer.valueOf(number), name); |
57 | 0 | } |
58 | 0 | } |
59 | ||
60 | /* | |
61 | * (non-Javadoc) | |
62 | * | |
63 | * @see org.crosswire.common.config.MappedChoice#getOptions() | |
64 | */ | |
65 | public Map<Integer, String> getOptions() { | |
66 | 0 | return new TreeMap<Integer, String>(options); |
67 | } | |
68 | ||
69 | /* | |
70 | * (non-Javadoc) | |
71 | * | |
72 | * @see org.crosswire.common.config.Choice#getConvertionClass() | |
73 | */ | |
74 | public Class<Integer> getConversionClass() { | |
75 | 0 | return Integer.TYPE; |
76 | } | |
77 | ||
78 | /* | |
79 | * (non-Javadoc) | |
80 | * | |
81 | * @see | |
82 | * org.crosswire.common.config.AbstractReflectedChoice#convertToString(java | |
83 | * .lang.Object) | |
84 | */ | |
85 | @Override | |
86 | public String convertToString(Object orig) { | |
87 | 0 | return orig.toString(); |
88 | } | |
89 | ||
90 | /* | |
91 | * (non-Javadoc) | |
92 | * | |
93 | * @see | |
94 | * org.crosswire.common.config.AbstractReflectedChoice#convertToObject(java | |
95 | * .lang.String) | |
96 | */ | |
97 | @Override | |
98 | public Object convertToObject(String orig) { | |
99 | // First check to see if this is a number | |
100 | try { | |
101 | 0 | return Integer.valueOf(orig); |
102 | 0 | } catch (NumberFormatException ex) { |
103 | 0 | for (Map.Entry<Integer, String> mapEntry : options.entrySet()) { |
104 | 0 | if (mapEntry.getValue().equals(orig)) { |
105 | 0 | return mapEntry.getKey(); |
106 | } | |
107 | } | |
108 | 0 | return Integer.valueOf(0); |
109 | } | |
110 | } | |
111 | ||
112 | private Map<Integer, String> options; | |
113 | } |