All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jcckit.util.FlatConfigData Maven / Gradle / Ivy

Go to download

PlantUML is a component that allows to quickly write : * sequence diagram, * use case diagram, * class diagram, * activity diagram, * component diagram, * state diagram * object diagram

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

/**
 *  An implementation of ConfigData based on a flat
 *  representation of the hierachically organized key-value pairs.
 *  Concrete subclasses must implement the methods
 *  {@link #getValue} and {@link #createConfigData} in accordance
 *  with the Template Method pattern and Factory Method pattern,
 *  respectively.
 *  

* In a flat representation of hierachically organized key-value * pairs all key-value pairs are stored in a single Hashtable. * Its key is the full key of the configuration data (i.e. the key * including its path). *

* Example (using the notation for a .properties file): *

 *  title = example
 *  symbolAttributes/className = jcckit.graphic.BasicDrawingAttributes
 *  symbolAttributes/fillColor = 0xcaffee
 *  symbolAttributes/lineColor = 0xff0000
 *  
* The following table shows the result of some method calls at a * FlatConfigData instance prepared with * this example: *

*

* * * * *
Method callResult
get("title")example
getNode("symbolAttributes").get("fillColor") * 0xcaffee
*
*

* In addition FlatConfigData implements inheritance * of key-value pairs. * Basically a node in the tree of key-value pairs * may extend another node in the tree. * The extended node inherit all key-value pairs from the extending * one including the key-value pairs of all descendants. * The value of a inherited key-value pair may be overridden. * Also new key-value pairs may be placed in the inherited node or * anywhere in the subtree. * Note, that the extending node has to be a node which is not a * descendant of the extended node (otherwise a circulary chain * of references occurs). As a consequence not more than 20 inheritance * levels are allowed. *

* The implementation of this kind of inheritance in a flat hashtable * is done by an additional key-value pair of the form *

 *    extending-node/ = extended-node/
 *  
* Example: *
 *  A/a/priority = high
 *  A/a/alpha/hello = universe
 *  A/a/alpha/answer = 42
 *  A/b/1/ = A/a/
 *  A/b/1/alpha/hello = world
 *  A/b/1/alpha/question = 6 * 7
 *  
* The following table shows the result of various method calls * applied at the node A/b/1/ of a FlatConfigData * instance prepared with this example: *

*

* * * * * * *
Method callResultComment
get("priority")highinherited
getNode("alpha").get("hello") * worldoverridden
getNode("alpha").get("question") * 6 * 7added
getNode("alpha").get("answer") * 42inherited
*
* * @author Franz-Josef Elmer */ public abstract class FlatConfigData implements ConfigData { private final String _path; /** Creates a new instance for the specified path. */ public FlatConfigData(String path) { _path = path; } /** * Returns the full key. * @param key A (relative) key. null is not allowed. * @return the path concatenated with key or key * if the path is undefined. */ public String getFullKey(String key) { return _path == null ? key : _path + key; } /** * Returns the value associated with this key. * @param key The relative key. null is not allowed. * @return the associated value. Will be null if no value exists * for key. */ public String get(String key) { return get(_path, key, 0); } /** * Obtains a value in accordance with hierarchy (path) and * inheritance (recursive calls of this routine). */ private String get(String path, String key, int numberOfLevels) { String result = null; if (numberOfLevels < 20) { String fullKey = path == null ? key : path + key; result = getValue(fullKey); if (result == null) { // posAfterDelim is the index in path just after '/' int posAfterDelim = path == null ? -1 : path.length(); String replacement; while (posAfterDelim > 0) { // look for a sub-tree replacement = getValue(path.substring(0, posAfterDelim)); if (replacement != null) { // sub-tree found, add last part of the original path result = get(replacement + path.substring(posAfterDelim), key, numberOfLevels + 1); // break whether result is null or not. break; } // remove last element from the path posAfterDelim = path.lastIndexOf('/', posAfterDelim - 2) + 1; } } } return result; } /** * Returns the ConfigData object associated with this key. * @param key The relative key. * @return the associated value. Will never return null. * Instead an empty ConfigData is returned. */ public ConfigData getNode(String key) { String path = (_path == null ? key : _path + key) + '/'; return createConfigData(path); } /** * Returns the value for the specified full key from the flat * representation of the hierarchically organized key-value pairs. * @param fullKey The full key including path. null is not allowed. * @return the value or null if not found. */ protected abstract String getValue(String fullKey); /** * Returns the FlatConfigData object for the specified full path. * In general path will be used in the constructor with * path argument. * @param path The full path. * @return a new instance in any case. */ protected abstract ConfigData createConfigData(String path); }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy