1 /* $Id$ */
2 /***************************************************************************
3  *                   (C) Copyright 2003-2010 - Stendhal                    *
4  ***************************************************************************
5  ***************************************************************************
6  *                                                                         *
7  *   This program is free software; you can redistribute it and/or modify  *
8  *   it under the terms of the GNU General Public License as published by  *
9  *   the Free Software Foundation; either version 2 of the License, or     *
10  *   (at your option) any later version.                                   *
11  *                                                                         *
12  ***************************************************************************/
13 package games.stendhal.server.core.config.factory;
14 
15 import java.util.Map;
16 
17 /**
18  * A configuration context for general object factories.
19  */
20 public class ConfigurableFactoryContext {
21 
22 	private final Map<String, String> attributes;
23 
24 	/**
25 	 * Create a configuration context using an attribute map. NOTE: The
26 	 * attributes are not copied.
27 	 *
28 	 * @param attributes
29 	 *            The attributes.
30 	 */
ConfigurableFactoryContext(final Map<String, String> attributes)31 	public ConfigurableFactoryContext(final Map<String, String> attributes) {
32 		this.attributes = attributes;
33 	}
34 
35 	/**
36 	 * Extracts a boolean value from a string.
37 	 *
38 	 * @param name
39 	 *            name of the attribute (only used for error handling)
40 	 * @param value
41 	 *            value to parse
42 	 * @return the parsed value
43 	 * @throws IllegalArgumentException
44 	 *             in case the value is not a valid boolean
45 	 */
extractBooleanFromString(final String name, final String value)46 	private static boolean extractBooleanFromString(final String name, final String value) {
47 		if ("true".equals(value)) {
48 			return true;
49 		}
50 
51 		if ("false".equals(value)) {
52 			return false;
53 		}
54 		throw new IllegalArgumentException("Invalid '" + name
55 				+ "' attribute value: '" + value
56 				+ "' should be 'true' or 'false'");
57 	}
58 
59 	/**
60 	 * gets an attribute.
61 	 *
62 	 * @param name
63 	 *            the attribute name.
64 	 * @param defaultValue
65 	 *            the default value it case it is not defined
66 	 * @return the value of the attribute
67 	 * @throws IllegalArgumentException
68 	 *             in case the value is not a valid boolean
69 	 */
getBoolean(final String name, final boolean defaultValue)70 	public boolean getBoolean(final String name, final boolean defaultValue) {
71 		final String value = attributes.get(name);
72 		if (value == null) {
73 			return defaultValue;
74 		}
75 
76 		return extractBooleanFromString(name, value);
77 	}
78 
79 	/**
80 	 * gets an attribute.
81 	 *
82 	 * @param name
83 	 *            the attribute name.
84 	 * @return the value of the attribute
85 	 * @throws IllegalArgumentException
86 	 *             in case the value is not a valid boolean or is missing
87 	 */
getRequiredBoolean(final String name)88 	public boolean getRequiredBoolean(final String name) {
89 		final String value = this.getRequiredString(name);
90 		return extractBooleanFromString(name, value);
91 	}
92 
93 	/**
94 	 * gets an attribute.
95 	 *
96 	 * @param name
97 	 *            the attribute name.
98 	 * @param defaultValue
99 	 *            the default value it case it is not defined
100 	 * @return the value of the attribute
101 	 * @throws IllegalArgumentException
102 	 *             in case the value is not a valid integer
103 	 */
getInt(final String name, final int defaultValue)104 	public int getInt(final String name, final int defaultValue) {
105 		final String value = attributes.get(name);
106 		if (value == null) {
107 			return defaultValue;
108 		}
109 
110 		try {
111 			return Integer.parseInt(value);
112 		} catch (final NumberFormatException ex) {
113 			throw new IllegalArgumentException("Invalid '" + name
114 					+ "' attribute value: " + value
115 					+ " is not a valid integer.");
116 		}
117 	}
118 
119 	/**
120 	 * gets an attribute.
121 	 *
122 	 * @param name
123 	 *            the attribute name.
124 	 * @return the value of the attribute
125 	 * @throws IllegalArgumentException
126 	 *             in case the value is not a valid integer or is missing
127 	 */
getRequiredInt(final String name)128 	public int getRequiredInt(final String name) {
129 		final String value = this.getRequiredString(name);
130 		try {
131 			return Integer.parseInt(value);
132 		} catch (final NumberFormatException ex) {
133 			throw new IllegalArgumentException("Invalid '" + name
134 					+ "' attribute value: " + value
135 					+ " is not a valid integer.");
136 		}
137 	}
138 
139 	/**
140 	 * gets an attribute.
141 	 *
142 	 * @param name
143 	 *            the attribute name.
144 	 * @param defaultValue
145 	 *            the default value it case it is not defined
146 	 * @return the value of the attribute
147 	 */
getString(final String name, final String defaultValue)148 	public String getString(final String name, final String defaultValue) {
149 		final String value = attributes.get(name);
150 		if (value == null) {
151 			return defaultValue;
152 		}
153 		return value;
154 	}
155 
156 	/**
157 	 * gets an attribute.
158 	 *
159 	 * @param name
160 	 *            the attribute name.
161 	 * @return the value of the attribute
162 	 * @throws IllegalArgumentException
163 	 *             in case is missing
164 	 */
getRequiredString(final String name)165 	public String getRequiredString(final String name) {
166 		final String value = attributes.get(name);
167 		if (value == null) {
168 			throw new IllegalArgumentException("Missing required attribute "
169 					+ name);
170 		}
171 		return value;
172 	}
173 }
174