1 /**
2 * Copyright (C) 2007 Joern Krueger surething@users.sourceforge.net
3 *
4 * This program is free software; you can redistribute
5 * it and/or modify it under the terms of the GNU General
6 * Public License version 2 as published by the Free Software
7 * Foundation.
8 *
9 * This program is distributed in the hope that it will be
10 * useful, but WITHOUT ANY WARRANTY; without even the implied
11 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 * PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place,
18 * Suite 330, Boston, MA 02111-1307 USA
19 */
20 package de.surethingies.properties;
21
22 import java.util.HashMap;
23 import java.util.Map;
24 import java.util.SortedSet;
25 import java.util.TreeSet;
26
27 import de.surethingies.properties.plain.Entry;
28 import de.surethingies.properties.plain.Setting;
29
30 public class Parameters {
31
32 /**
33 * Initialized by PropertyFactory before first use Group, Parameter, List<DefaultParameter>
34 */
35 private Map<String, SortedSet<Parameter>> knownParams = new HashMap<String, SortedSet<Parameter>>();
36
37 private SortedSet<TypedParameter> typedParameters = new TreeSet<TypedParameter>();
38
39 /**
40 * Add a Parameter which could be used to translate Settings in
41 * TypedParameters
42 *
43 * @param param
44 */
45 protected void addDefaultParameters(String group, SortedSet<Parameter> params) {
46 knownParams.put(group, params);
47 }
48
49 /**
50 * Will cleanup the List of loaded Parameters, but not the List of known
51 * Parameters.
52 */
53 protected void reset() {
54 typedParameters.clear();
55 }
56
57 protected SortedSet<TypedParameter> getTypedParameters() {
58 return typedParameters;
59 }
60
61 /**
62 * Create a new Parameterset with the Values from settings
63 *
64 * @param settings
65 * @return
66 */
67 protected void setParametersFromSettings(Setting settings) {
68
69 Map<String, String> entries = new HashMap<String, String>();
70 for (Entry entry : settings.entries) {
71 entries.put(entry.key, entry.value);
72 }
73
74 if (knownParams.get(settings.group) == null) {
75 throw new ParameterException("There were no Parameters set for Group: " + settings.group);
76 }
77
78
79 for (Parameter param : knownParams.get(settings.group)) {
80 String value = entries.get(param.getIdentifier());
81
82 if (value == null) {
83 value = param.getDefaultValue();
84 }
85
86 typedParameters.add(new TypedParameter(param, settings.id, value));
87 }
88 }
89 }