jml.utils
Class Utility

java.lang.Object
  extended by jml.utils.Utility

public class Utility
extends java.lang.Object

The Utility class provides some frequently used functions for text processing.

Version:
1.0, 12/11/2011
Author:
Mingjie Qian

Nested Class Summary
static class Utility.ArrayIndexComparator<V extends java.lang.Comparable<? super V>>
          A generic Class that implements Comparator which provide a override comparator function sorting a array's indices based on its values.
static class Utility.keyAscendComparator<K extends java.lang.Comparable<K>>
          Generic comparator for TreeMap to sort the keys in a increasing order.
static class Utility.keyDescendComparator<K extends java.lang.Comparable<K>>
          Generic comparator for TreeMap to sort the keys in a decreasing order.
 
Constructor Summary
Utility()
           
 
Method Summary
static
<K extends java.lang.Comparable<? super K>,V>
java.util.Map<K,V>
sortByKey(java.util.Map<K,V> map, java.lang.String order)
          Sort a map by its keys according to a specified order.
static
<K extends java.lang.Comparable<K>,V>
java.util.Map<K,V>
sortByKeys(java.util.Map<K,V> map, java.lang.String order)
          Sort a map by its keys according to a specified order.
static
<K,V extends java.lang.Comparable<? super V>>
java.util.Map<K,V>
sortByValue(java.util.Map<K,V> map, java.lang.String order)
          Sort a map by its values according to a specified order.
static
<K,V extends java.lang.Comparable<V>>
java.util.Map<K,V>
sortByValues(java.util.Map<K,V> map, java.lang.String order)
          Sort a map by its values according to a specified order.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Utility

public Utility()
Method Detail

sortByKeys

public static <K extends java.lang.Comparable<K>,V> java.util.Map<K,V> sortByKeys(java.util.Map<K,V> map,
                                                                                  java.lang.String order)
Sort a map by its keys according to a specified order. Note: the returned map does not allow access by keys. One should use entries in stead. One can cast the returned map to TreeMap but not HashMap. The input map can be any map.

Type Parameters:
K - Class type for the key in the map.
V - Class type for the value in the map.
Parameters:
map - The map to be sorted.
order - The String indicating the order by which the map to be sorted, either "descend" or "ascend".
Returns:
A sorted TreeMap by the order specified by .

sortByValues

public static <K,V extends java.lang.Comparable<V>> java.util.Map<K,V> sortByValues(java.util.Map<K,V> map,
                                                                                    java.lang.String order)
Sort a map by its values according to a specified order. Note: the returned map does not allow access by keys. One should use entries in stead. One can cast the returned map to TreeMap but not HashMap. The input map can be any map.

Type Parameters:
K - Class type for the key in the map.
V - Class type for the value in the map.
Parameters:
map - The map to be sorted.
order - The String indicating the order by which the map to be sorted, either "descend" or "ascend".
Returns:
A sorted TreeMap by the order specified by .

sortByValue

public static <K,V extends java.lang.Comparable<? super V>> java.util.Map<K,V> sortByValue(java.util.Map<K,V> map,
                                                                                           java.lang.String order)
Sort a map by its values according to a specified order. The input map can be any map. One can cast the returned map to HashMap but not TreeMap.

Type Parameters:
K - Class type for the key in the map.
V - Class type for the value in the map.
Parameters:
map - The map to be sorted which can be TreeMap or HashMap.
order - The String indicating the order by which the map to be sorted, either "descend" or "ascend".
Returns:
A sorted TreeMap by the order specified by .

sortByKey

public static <K extends java.lang.Comparable<? super K>,V> java.util.Map<K,V> sortByKey(java.util.Map<K,V> map,
                                                                                         java.lang.String order)
Sort a map by its keys according to a specified order. The input map can be any map. One can cast the returned map to HashMap but not TreeMap.

Type Parameters:
K - Class type for the key in the map.
V - Class type for the value in the map.
Parameters:
map - The map to be sorted which can be TreeMap or HashMap.
order - The String indicating the order by which the map to be sorted, either "descend" or "ascend".
Returns:
A sorted TreeMap by the order specified by .