| DataType.java |
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: 2008
18 * The copyright to this program is held by it's authors.
19 *
20 * ID: $Id: org.eclipse.jdt.ui.prefs 1178 2006-11-06 12:48:02Z dmsmith $
21 */
22 package org.crosswire.common.options;
23
24 import org.crosswire.common.util.Convert;
25
26 /**
27 * A DataType provides the ability to marshal a String value to an object.
28 *
29 * @see gnu.lgpl.License for license details.<br>
30 * The copyright to this program is held by it's authors.
31 * @author DM Smith [dmsmith555 at yahoo dot com]
32 */
33 public enum DataType {
34 /**
35 * A string argument.
36 */
37 STRING ("String") {
38 @Override
39 public Object convertFromString(String value) {
40 return value;
41 }
42 },
43
44 /**
45 * An integer argument.
46 */
47 INTEGER ("Integer") {
48 @Override
49 public Object convertFromString(String value) {
50 return Integer.valueOf(Convert.string2Int(value));
51 }
52 },
53
54 /**
55 * An boolean argument that allows various values for 'true'.
56 */
57 BOOLEAN ("Boolean") {
58 @Override
59 public Object convertFromString(String value) {
60 return Boolean.valueOf(Convert.string2Boolean(value));
61 }
62 };
63
64 /**
65 * @param name
66 * The name of the DataType
67 */
68 private DataType(String name) {
69 this.name = name;
70 }
71
72 /**
73 * Convert a String to an DataType's expected value.
74 * @param input the string to convert
75 * @return the converted value
76 */
77 public abstract Object convertFromString(String input);
78
79 /**
80 * Lookup method to convert from a String
81 */
82 public static DataType fromString(String name) {
83 for (DataType v : values()) {
84 if (v.name().equalsIgnoreCase(name)) {
85 return v;
86 }
87 }
88 // cannot get here
89 assert false;
90 return null;
91 }
92
93 /* (non-Javadoc)
94 * @see java.lang.Enum#toString()
95 */
96 @Override
97 public String toString() {
98 return name;
99 }
100
101 /**
102 * The name of the DataType
103 */
104 private String name;
105 }
106