org.kuali.rice.kns.util.properties
Class PropertyTree

java.lang.Object
  extended by org.kuali.rice.kns.util.properties.PropertyTree
All Implemented Interfaces:
Map

public class PropertyTree
extends Object
implements Map

This class is a Recursive container for single- and multi-level key,value pairs. It relies on the assumption that the consumer (presumably a JSP) will (implicitly) call toString at the end of the chain, which will return the String value of the chain's endpoint. It implements Map because that's how we fool jstl into converting "a.b.c" into get("a").get("b").get("c") instead of getA().getB().getC() Uses LinkedHashMap and LinkedHashSet because iteration order is now important.


Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Field Summary
(package private)  Map children
           
(package private)  String directValue
           
(package private)  boolean flat
           
private static org.apache.log4j.Logger LOG
           
(package private)  PropertyTree parent
           
 
Constructor Summary
  PropertyTree()
          Creates an empty instance with no parent
  PropertyTree(boolean flat)
          Creates an empty instance with no parent.
  PropertyTree(Properties properties)
          Creates an instance pre-loaded with the given Properties
private PropertyTree(PropertyTree parent)
          Creates an empty instance with the given parent.
 
Method Summary
 void clear()
          Unsupported, since you can't change the contents of a PropertyTree once it has been initialized.
private  Map collectEntries(String prefix, boolean flattenEntries)
          Builds a HashMap containing all of the key,value pairs stored in this PropertyTree
 boolean containsKey(Object key)
           
 boolean containsValue(Object value)
           
 Set entrySet()
          Returns an unmodifiable Set containing all key,value pairs in this PropertyTree and its children.
 Object get(Object key)
          Traverses the tree structure until it finds the PropertyTree pointed to by the given key, and returns that PropertyTree instance.
private  PropertyTree getChild(String key)
          Returns the PropertyTree associated with the given key.
 Map getDirectChildren()
           
private  String getDirectValue()
           
 String getProperty(String key)
           
private  PropertyTree getSubtree(String key)
          Returns the PropertyTree object with the given key, or null if there is none.
private  boolean hasChildren()
           
private  boolean hasDirectValue()
           
 boolean isEmpty()
           
 Set keySet()
          Returns an unmodifiable Set containing the keys of all of the entries of this PropertyTree.
 Object put(Object key, Object value)
          Unsupported, since you can't change the contents of a PropertyTree once it has been initialized.
 void putAll(Map t)
          Unsupported, since you can't change the contents of a PropertyTree once it has been initialized.
 Object remove(Object key)
          Unsupported, since you can't change the contents of a PropertyTree once it has been initialized.
private  void setDirectValue(String value)
          Sets the directValue of this PropertyTree to the given value.
 void setProperties(Properties properties)
          Inserts all properties from the given Properties instance into this PropertyTree.
 void setProperty(String key, String value)
          Associates the given key with the given value.
 int size()
           
 String toString()
          Returns the directValue of this PropertyTree, or null if there is none.
private  void validateKey(Object key)
           
private  void validateValue(Object value)
           
 Collection values()
          Returns an unmodifiable Collection containing the values of all of the entries of this PropertyTree.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Field Detail

LOG

private static org.apache.log4j.Logger LOG

flat

final boolean flat

parent

final PropertyTree parent

directValue

String directValue

children

Map children
Constructor Detail

PropertyTree

public PropertyTree()
Creates an empty instance with no parent


PropertyTree

public PropertyTree(boolean flat)
Creates an empty instance with no parent. If flat is true, entrySet and size and the iterators will ignore entries in subtrees.


PropertyTree

private PropertyTree(PropertyTree parent)
Creates an empty instance with the given parent. If flat is true, entrySet and size and the iterators will ignore entries in subtrees.


PropertyTree

public PropertyTree(Properties properties)
Creates an instance pre-loaded with the given Properties

Parameters:
properties -
Method Detail

setProperty

public void setProperty(String key,
                        String value)
Associates the given key with the given value. If the given key has multiple levels (consists of multiple strings separated by '.'), the property value is stored such that it can be retrieved either directly, by calling get() and passing the entire key; or indirectly, by decomposing the key into its separate levels and calling get() successively on the result of the previous level's get.
For example, given
PropertyTree tree = new PropertyTree(); tree.set( "a.b.c", "something" ); the following statements are equivalent ways to retrieve the value:
Object one = tree.get( "a.b.c" ); Object two = tree.get( "a" ).get( "b" ).get( "c" );
Note: since I can't have the get method return both a PropertyTree and a String, getting an actual String requires calling toString on the PropertyTree returned by get.

Parameters:
key -
value -
Throws:
IllegalArgumentException - if the key is null
IllegalArgumentException - if the value is null

setProperties

public void setProperties(Properties properties)
Inserts all properties from the given Properties instance into this PropertyTree.

Parameters:
properties -
Throws:
IllegalArgumentException - if the Properties object is null
IllegalArgumentException - if a property's key is null
IllegalArgumentException - if a property's value is null

getSubtree

private PropertyTree getSubtree(String key)
Returns the PropertyTree object with the given key, or null if there is none.

Parameters:
key -
Returns:
Throws:
IllegalArgumentException - if the key is null

getProperty

public String getProperty(String key)
Parameters:
key -
Returns:
the directValue of the PropertyTree associated with the given key, or null if there is none

getDirectChildren

public Map getDirectChildren()
Returns:
an unmodifiable copy of the direct children of this PropertyTree

toString

public String toString()
Returns the directValue of this PropertyTree, or null if there is none.

This is the hack that makes it possible for jstl to get what it needs when trying to retrive the value of a simple key or of a complex (multi-part) key.

Overrides:
toString in class Object

setDirectValue

private void setDirectValue(String value)
Sets the directValue of this PropertyTree to the given value.

Parameters:
value -

getDirectValue

private String getDirectValue()
Returns:
directValue of this PropertyTree, or null if there is none

hasDirectValue

private boolean hasDirectValue()
Returns:
true if the directValue of this PropertyTree is not null

hasChildren

private boolean hasChildren()
Returns:
true if the this PropertyTree has children

getChild

private PropertyTree getChild(String key)
Returns the PropertyTree associated with the given key. If none exists, creates a new PropertyTree associates it with the given key, and returns it.

Parameters:
key -
Returns:
PropertyTree associated with the given key
Throws:
IllegalArgumentException - if the given key is null

validateKey

private void validateKey(Object key)
Parameters:
key -
Throws:
IllegalArgumentException - if the given key is not a String, or is null

validateValue

private void validateValue(Object value)
Parameters:
value -
Throws:
IllegalArgumentException - if the given value is not a String, or is null

entrySet

public Set entrySet()
Returns an unmodifiable Set containing all key,value pairs in this PropertyTree and its children.

Specified by:
entrySet in interface Map
See Also:
Map.entrySet()

collectEntries

private Map collectEntries(String prefix,
                           boolean flattenEntries)
Builds a HashMap containing all of the key,value pairs stored in this PropertyTree

Returns:

size

public int size()
Specified by:
size in interface Map
Returns:
the number of keys contained, directly or indirectly, in this PropertyTree

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Map
See Also:
Map.isEmpty()

values

public Collection values()
Returns an unmodifiable Collection containing the values of all of the entries of this PropertyTree.

Specified by:
values in interface Map
See Also:
Map.values()

keySet

public Set keySet()
Returns an unmodifiable Set containing the keys of all of the entries of this PropertyTree.

Specified by:
keySet in interface Map
See Also:
Map.keySet()

containsKey

public boolean containsKey(Object key)
Specified by:
containsKey in interface Map
See Also:
Map.containsKey(java.lang.Object)

containsValue

public boolean containsValue(Object value)
Specified by:
containsValue in interface Map
See Also:
Map.containsValue(java.lang.Object)

get

public Object get(Object key)
Traverses the tree structure until it finds the PropertyTree pointed to by the given key, and returns that PropertyTree instance.

Only returns PropertyTree instances; if you want the String value pointed to by a given key, you must call toString() on the returned PropertyTree (after verifying that it isn't null, of course).

Specified by:
get in interface Map
See Also:
Map.get(java.lang.Object)

clear

public void clear()
Unsupported, since you can't change the contents of a PropertyTree once it has been initialized.

Specified by:
clear in interface Map

putAll

public void putAll(Map t)
Unsupported, since you can't change the contents of a PropertyTree once it has been initialized.

Specified by:
putAll in interface Map

remove

public Object remove(Object key)
Unsupported, since you can't change the contents of a PropertyTree once it has been initialized.

Specified by:
remove in interface Map

put

public Object put(Object key,
                  Object value)
Unsupported, since you can't change the contents of a PropertyTree once it has been initialized.

Specified by:
put in interface Map


Copyright © 2004-2011 The Kuali Foundation. All Rights Reserved.