KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jcckit > util > ConfigParametersBasedConfigData


1 /*
2  * Copyright 2003-2004, Franz-Josef Elmer, All rights reserved
3  *
4  * This library is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation; either version 2.1 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details
13  * (http://www.gnu.org/copyleft/lesser.html).
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package jcckit.util;
20
21 /**
22  * An implementation of {@link ConfigData} based on two instances of
23  * {@link ConfigParameters}. The second one serves as a set of
24  * default parameters. It will be used if the first one has not the requested
25  * key-value pair.
26  *
27  * @author Franz-Josef Elmer
28  */

29 public class ConfigParametersBasedConfigData implements ConfigData {
30   private ConfigParameters _config;
31   private ConfigParameters _defaultConfig;
32
33   /**
34    * Creates an instance.
35    * @param config A set of key-value pairs.
36    * @param defaultConfig The default set of key-value pairs.
37    */

38   public ConfigParametersBasedConfigData(ConfigParameters config,
39                                          ConfigParameters defaultConfig) {
40     _config = config;
41     _defaultConfig = defaultConfig;
42   }
43
44   /**
45    * Returns the full key.
46    * @param key A (relative) key. <tt>null</tt> is not allowed.
47    * @return the full key including path.
48    */

49   public String JavaDoc getFullKey(String JavaDoc key) {
50     return _config.getFullKey(key);
51   }
52
53   /**
54    * Returns the value associated with this key.
55    * @param key The relative key. <tt>null</tt> is not allowed.
56    * @return the associated value. Will be <tt>null</tt> if no value exists
57    * for <tt>key</tt>.
58    */

59   public String JavaDoc get(String JavaDoc key) {
60     String JavaDoc value = _config.get(key, null);
61     return value == null ? _defaultConfig.get(key, null) : value;
62   }
63
64   /**
65    * Returns the <tt>ConfigData</tt> object associated with this key.
66    * @param key The relative key. <tt>null</tt> is not allowed.
67    * @return the associated value. Will never return <tt>null</tt>.
68    * Instead an empty <tt>ConfigData</tt> is returned.
69    */

70   public ConfigData getNode(String JavaDoc key) {
71     return new ConfigParametersBasedConfigData(_config.getNode(key),
72                                                _defaultConfig.getNode(key));
73   }
74 }
75
Popular Tags