QJCC homepage

biz.chitec.quarterback.util
Class PropertiesConsolidator

java.lang.Object
  extended bybiz.chitec.quarterback.util.PropertiesConsolidator

public final class PropertiesConsolidator
extends java.lang.Object

Static helper class to consolidate several Properties objects into one and resort them again. Useful for storing the settings of several objects into one storage (anyone saying "registry"???). Example: Say you have several classes and everyone has its own settings. For some reason, you want to consolidate all the settings into one storage, e.g. for storing everything into one file or using it in a combined settings dialog. Then, simply do

 Properties compound=PropertiesConsolidator.merge(
   new String[] {"fileopen","imagelib","general"},
   new Properties[] {fileopener.getProps(),imagelib.getProps(),this.getProps()},
   new Properties());
 

Target then will contain all the contents of the source bundles, with the keys being prepended by the given prefix. E.g. a property selector.maxsize in the fileopener properties will become fileopen.selector.maxsize in the combined bundle.

For dividing the compound bundle into the original parts and passing them back to the classes, do this:

 Map props=PropertiesConsolidator(compound);
 fileopener.setProps(props.get("fileopen"));
 imagelib.setProps(props.get("imagelib"));
 this.setProps(props.get("general"));
 

Version:
$Id: b45a10d5bcf17765c42615a5a1cdf897abb5a9a7 $
Author:
dh

Constructor Summary
private PropertiesConsolidator()
          This is a fully static class
 
Method Summary
static java.util.Map divide(java.util.Properties compound)
          Divides a compound Properties bundle into original bundles.
static java.util.Map divide(java.util.Set prefixes, java.util.Properties compound)
          Divides a compound Properties bundle into original bundles.
static java.util.Map divide(java.lang.String[] prefix, java.util.Properties compound)
          Divides a compound Properties bundle into original bundles.
static java.util.Map extract(java.lang.String prefix, java.util.Properties compound)
          Extract all values with one prefix out of the given compound bundle.
static java.util.Properties merge(java.util.Map source)
          Merges all Properties bundles in the given Map into new Properties bundle
static java.util.Properties merge(java.util.Map source, java.util.Properties target)
          Merges all Properties bundles in the given Map into target Properties.
static java.util.Properties merge(java.lang.String[] prefix, java.util.Properties[] source)
          Merges multiple Properties bundles into one which is freshly created.
static java.util.Properties merge(java.lang.String[] prefix, java.util.Properties[] source, java.util.Properties target)
          Merge multiple Properties bundles into one.
static java.util.Properties merge(java.lang.String prefix, java.util.Properties source, java.util.Properties target)
          Merge one Properties bundle into target.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PropertiesConsolidator

private PropertiesConsolidator()
This is a fully static class

Method Detail

merge

public static java.util.Properties merge(java.util.Map source,
                                         java.util.Properties target)
Merges all Properties bundles in the given Map into target Properties. Map must have String keys, subsequent Properties entries get that string as prefix.

Parameters:
source - Map with String-Properties entries
target - Properties bundle
Returns:
The target Properties bundle

merge

public static java.util.Properties merge(java.util.Map source)
Merges all Properties bundles in the given Map into new Properties bundle


merge

public static java.util.Properties merge(java.lang.String prefix,
                                         java.util.Properties source,
                                         java.util.Properties target)
Merge one Properties bundle into target. Every entry of the source bundle is taken and stored in the target bundle. There, the key value is the one from the source bundle, prepended by the given prefix, which is separated by a dot from the rest.

Parameters:
prefix - A prefix the bundle will have
source - The source bundle with the key-value pairs to be merged
target - The target bundle where the data will be stored in
Returns:
The target bundle

merge

public static java.util.Properties merge(java.lang.String[] prefix,
                                         java.util.Properties[] source,
                                         java.util.Properties target)
Merge multiple Properties bundles into one. All given properties bundles are merged into the target, each one with the prefix given at the same index in the prefix array. source[] entries may be null. prefix[] and source[] must have same length.

Parameters:
prefix - A prefix the bundle will have
source - The source bundle with the key-value pairs to be merged
target - The target bundle where the data will be stored in
Returns:
The target bundle

merge

public static java.util.Properties merge(java.lang.String[] prefix,
                                         java.util.Properties[] source)
Merges multiple Properties bundles into one which is freshly created. All given properties bundles are merged into the target, each one with the prefix given at the same index in the prefix array. source[] entries may be null. prefix[] and source[] must have same length.

Parameters:
prefix - The prefixes for merging
source - The source bundles which are merged in
Returns:
The target bundle

divide

public static java.util.Map divide(java.util.Set prefixes,
                                   java.util.Properties compound)
Divides a compound Properties bundle into original bundles. Traverses the given bundle, extracts the first part of each key (the one before the first "."). If that one is in the given prefixes set, the property is stored with the rest of the original key in a target bundle. Each prefix gets its own bundle. If prefixes is given, the returned Map contains a Properties bundle for each given key, even if no elements for this key existed in the compound bundle. In this case, those result bundles are empty.

Parameters:
prefixes - The prefixes to extract bundles for
compound - The compound source for extraction
Returns:
Map containing all created target bundles

divide

public static java.util.Map divide(java.lang.String[] prefix,
                                   java.util.Properties compound)
Divides a compound Properties bundle into original bundles. Traverses the given bundle. The prefixes to extract for are given as an array.

Parameters:
prefix - Array of prefixes to extract for
compound - The extraction source
Returns:
Map containing all created target bundles

divide

public static java.util.Map divide(java.util.Properties compound)
Divides a compound Properties bundle into original bundles. Traverses the compound bundle, extracts the first part of each key (the one before the first ".") and stores all values in Properties bundles separated by that key component.

Parameters:
compound - The compound bundle
Returns:
Map of Properties bundles, keys are the key prefixes of the compound bundle

extract

public static java.util.Map extract(java.lang.String prefix,
                                    java.util.Properties compound)
Extract all values with one prefix out of the given compound bundle. Traverses the compound bundle and picks all properties where the key starts with the given prefix. Those values are put into a new bundle which is returned. If no key has the given prefix, null is returned. Note that it is much more efficient to call one of the divide() methods and separate the returned Map than calling extract() several times. Use extract() only if you definitely do not need more than one part of the compound Map

Parameters:
prefix - The prefix of interest
compound - The extraction source
Returns:
Map containing only those properties with the given key prefix, keys without that prefix.

QJCC homepage