org.dhmp.util
Class Algorithm

java.lang.Object
  extended byorg.dhmp.util.Algorithm

public class Algorithm
extends java.lang.Object

General proposed algorithms.

Author:
hideyuki

Nested Class Summary
static class Algorithm.BinaryFunction
          Abstract fucntion for map transformation.
static class Algorithm.UnaryFunction
          Abstract fucntion for map transformation.
 
Method Summary
static HierarchicalMap append(HierarchicalMap appended, HierarchicalMap appender)
          Appends second map into first map.
static HierarchicalMap append(HierarchicalMap appended, HierarchicalMap appender, boolean checkRecursion)
           
static HierarchicalMap append(HierarchicalMap appended, LinkedHashMultiMap.EntrySet appender)
          Appends entryset into HierarchicalMap.
static HierarchicalMap append(HierarchicalMap appended, java.util.Map appender)
          Appends map into HierarchicalMap.
static HierarchicalMap denormalize(HierarchicalMap map, java.lang.String field, java.lang.String keyField)
          Restructure the map, ordering by content of key field.
static HierarchicalMap find(HierarchicalMap map, java.lang.String field, java.lang.String keyField, java.lang.Object value)
          Finds a node which contains a specific value.
static HierarchicalMap foreach(HierarchicalMap input, Algorithm.BinaryFunction function)
          Traverse the entire HierarchicalMap using Depth-first strategie.
static HierarchicalMap foreach(HierarchicalMap input, Algorithm.UnaryFunction function)
          Traverse the entire HierarchicalMap using Depth-first strategie.
static HierarchicalMap merge(java.lang.Object key, HierarchicalMap target, HierarchicalMap source)
          Merge source HierarchicalMap into target HierarchicalMap under key.
static HierarchicalMap normalize(HierarchicalMap map, java.lang.String field, java.lang.String keyField)
          Reverse the resul to denormaization.
static HierarchicalMap overwrite(HierarchicalMap overwritten, HierarchicalMap overwriter)
          Overwrites all the content of first map with content of second map.
static HierarchicalMap transform(HierarchicalMap map1, HierarchicalMap map2, Algorithm.BinaryFunction function)
          Traverse the entire HierarchicalMap using Depth-first strategie and tries to get the same element at second map.
static HierarchicalMap transform(HierarchicalMap map1, HierarchicalMap map2, HierarchicalMap map3)
          Traverse the entire HierarchicalMap map1 using Depth-first strategie and tries to get the element from second map map2.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

denormalize

public static HierarchicalMap denormalize(HierarchicalMap map,
                                          java.lang.String field,
                                          java.lang.String keyField)
Restructure the map, ordering by content of key field. A strucutre like:
 +
 | Chapter
 -----------+
 |          | Title
 |          ---------- first title
 | Chapter
 -----------+
           | Title
           ---------- second title
 
After calling denormalize(map, "Chapter", "Title") will be restructured, i.e. all "Chapter" node will be ordered according its "Title"'s content. to:
 +
 | Chapter
 -----------+
           | first title
           --------------+
           |             | Title
           |             ---------- first title
           | second title
           --------------+
                         | Title
                         ---------- second title
 
Note that the map passes as parameter will be modifyed after this method.

Parameters:
map - that will be restructured.
field - all the field affected by this method.
keyField - key field from which its content will be used to order the structure.
Returns:
restructured map.

normalize

public static HierarchicalMap normalize(HierarchicalMap map,
                                        java.lang.String field,
                                        java.lang.String keyField)
Reverse the resul to denormaization. The result can be physically different from original structure. For each node under field, the sub-node when exists, will be added as field. The sub-node will receive a new leaf "keyField" containing the original sub-node name as its value.

Parameters:
map - that will be restructured.
field - all the field affected by this method.
keyField - key field which is inserted into sub-node containing the original sub-node name.
Returns:
restructured map.

find

public static HierarchicalMap find(HierarchicalMap map,
                                   java.lang.String field,
                                   java.lang.String keyField,
                                   java.lang.Object value)
Finds a node which contains a specific value.

Parameters:
map - that will be searched.
field - from where the search begins.
keyField - key field which is looked if it contains the value.
value - the value which is looking for.
Returns:
sub-node conataining the searching value.

merge

public static HierarchicalMap merge(java.lang.Object key,
                                    HierarchicalMap target,
                                    HierarchicalMap source)
Merge source HierarchicalMap into target HierarchicalMap under key. All the contents from source HierarchicalMap are added under the target's key node. If key is null, the content of source is added under root. This method is not suited for HierarchicalMap using null as valid key. Note that source is not added if the key refers to a leaf node on the target.

Parameters:
key - pointing to a node on target HierarchicalMap where the source is added.
target - HierarchicalMap where the source is added. This HierarchicalMap will be m modified if merge succeeds.
source - containing entries to be added.
Returns:
target HierarchicalMap if merge was succeeded, or null if the key does not refer to a node.

append

public static HierarchicalMap append(HierarchicalMap appended,
                                     HierarchicalMap appender)
Appends second map into first map. Note that original map will be modified after this method. Also the returned map is the modified map itself and not a copy. Differently from HierarchicalMap.addAll(map) method, which simply append all entries from HierarchicalMap passed as parameter, this method tries to find out the correspondence among nodes from appender map to appended map, and appends the value to the nearest node from appended map.

Parameters:
appended - map where the content of the other map is appended.
appender - map containing the nodes to be added.
Returns:
appended map.

append

public static HierarchicalMap append(HierarchicalMap appended,
                                     HierarchicalMap appender,
                                     boolean checkRecursion)

append

public static HierarchicalMap append(HierarchicalMap appended,
                                     java.util.Map appender)
Appends map into HierarchicalMap. Note that original map will be modified after this method. Also the returned HierarchicalMap is the modified HierarchicalMap itself and not a copy. If the HierarchicalMap is null, it creates a new HierarchicalMap and append the content of map on it.

Parameters:
appended - map where the content of the other map is appended.
appender - map containing the entries to be added.
Returns:
appended map.

append

public static HierarchicalMap append(HierarchicalMap appended,
                                     LinkedHashMultiMap.EntrySet appender)
Appends entryset into HierarchicalMap. Note that original map will be modified after this method. Also the returned map is the modified map itself and not a copy. If the map is null, it creates a new map and append the entryset on it.

Parameters:
appended - map where the content of the other map is appended.
appender - entryset containing the entries to be added.
Returns:
appended map.

overwrite

public static HierarchicalMap overwrite(HierarchicalMap overwritten,
                                        HierarchicalMap overwriter)
Overwrites all the content of first map with content of second map. Note that original map will be modified after this method. Also the returned map is the modified map itself and not a copy.

Parameters:
overwritten - map where the content of the other map is overwritten.
overwriter - map containing the nodes to overwrite.
Returns:
overwritten map.

foreach

public static HierarchicalMap foreach(HierarchicalMap input,
                                      Algorithm.UnaryFunction function)
Traverse the entire HierarchicalMap using Depth-first strategie. Applies the unary function to each leaf element on the map. Note that original map will be modified after this method. Also the returned map is the modified map itself and not a copy.

Parameters:
input - map where the content of the other map is traversed.
function - unary function applied to each leaf.
Returns:
modified map.

foreach

public static HierarchicalMap foreach(HierarchicalMap input,
                                      Algorithm.BinaryFunction function)
Traverse the entire HierarchicalMap using Depth-first strategie. Applies the binary function to each leaf element on the map, passing its path (object array containing the key of each node) and its value. Note that original map will be modified after this method. Also the returned map is the modified map itself and not a copy.

Parameters:
input - map where the content of the other map is traversed.
function - binary function applied to each leaf.
Returns:
modified map.

transform

public static HierarchicalMap transform(HierarchicalMap map1,
                                        HierarchicalMap map2,
                                        Algorithm.BinaryFunction function)
Traverse the entire HierarchicalMap using Depth-first strategie and tries to get the same element at second map. Applies the binary function to each leaf element on the map and corresponding leaf on the second map. Note that original map will be modified after this method. Also the returned map is the modified map itself and not a copy.

Parameters:
map1 - map where the content is traversed.
map2 - map where the content of corresponding leaves are retrieved.
function - binary function applied to the leaves.
Returns:
modified map.

transform

public static HierarchicalMap transform(HierarchicalMap map1,
                                        HierarchicalMap map2,
                                        HierarchicalMap map3)
Traverse the entire HierarchicalMap map1 using Depth-first strategie and tries to get the element from second map map2. Map3 coordinates the transformation. It has a node containing "target" and "function". For each leaf at map1 tries to find the corresponding "target" and "fucntion" from map3. Then, the function is applied passing map1's leaf and map2's leaf at "target" as a key. Note that original map will be modified after this method. Also the returned map is the modified map itself and not a copy.

Parameters:
map1 - map where the content is traversed.
map2 - map where the content of corresponding leaves are retrieved.
map3 - contains the target from where map2 nodes are retrieved and binary function that will be applied.
Returns:
modified map.