public static int getSize(Map<?, ?> t) {
return t == null ? 0 : t.size();
}
public static int getSize(Hashtable<?, ?> t) {
return t == null ? 0 : t.size();
}
public static int getSize(Collection<?> c) {
return c == null ? 0 : c.size();
}
public static<T> T getIndex(List<T> c, int index){
return index < getSize(c) && index >= 0 ? c.get(index) : null;
}
/**
*
* @param param
* @return
*/
public static boolean getBoolFlag(String param) {
String yn = DBUtils.getYesNoFlag(param);
return DBUtils.getBoolValue(yn);
}
public static<T> void sort(List<T> list, Sorter<? super T> sorter){
sort(list, sorter, null);
}
public static<T> void sort(List<T> list, Sorter<? super T> sorter, SortType sortType){
sorter.sort(list, sortType != null ? sortType : SortType.Ascend);
}
public static<K, V> Map<K, List<V>> getGroups(Collection<V> collection, KeyPicker<K, ? super V> picker){
Map<K, List<V>> map = new HashMap<K, List<V>>();
if(!CollectionUtils.isEmpty(collection)){
for(V output : collection){
K key = picker.pickKey(output);
List<V> elements = null;
if(map.containsKey(key)){
elements = map.get(key);
}else{
elements = new ArrayList<V>();
map.put(key, elements);
}
elements.add(output);
}
}
return map;
}
public static<V> List<V> filter(List<V> list, KeyFilter<? super V>... filters){
List<V> filteredList = new ArrayList<V>();
if(!CollectionUtils.isEmpty(list) && !ArrayUtils.isEmpty(filters)){
for(V v : list){
boolean matched = true;
for(KeyFilter<? super V> c : filters){
matched = matched && c.matched(v);
}
if(matched){
filteredList.add(v);
}
}
}
return filteredList;
}
/**
* act like oracle decode function, return result depends on condition value
* @param condition
* @param defaltValue
* @param pairs
* @return
*/
@SuppressWarnings("unchecked")
public static<T, V> V decode(T condition, V defaltValue, Object... pairs){
V result = null;
if(pairs != null){
Assert.state((pairs.length % 2) == 0, "condition/value length must be even number");
}
boolean foundMatch = false;
for(int i = 0; i < pairs.length; i = i + 2){
T con = (T)pairs[i];
if((condition == null && con == null) || (condition != null && (condition == con || condition.equals(con)))){
result = (V)pairs[i + 1];
foundMatch = true;
break;
}
}
if(!foundMatch){
result = defaltValue;
}
return result;
}
public static<T> void add(Collection<? super T> source, Collection<T> target){
if(source != null && !CollectionUtils.isEmpty(target)){
source.addAll(target);
}
}
public static<T> void add(Collection<? super T> source, T target){
if(source != null){
source.add(target);
}
}
public static<T> boolean contains(Collection<? extends T> c, T e){
return c != null && c.contains(e);
}
private CommonUtils() {}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public abstract class KeyFilter<K> {
public abstract boolean matched(K k);
protected boolean matched(K k, String key){
return k != null && StringUtils.isNotBlank(key);
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public abstract class KeyPicker<K, V> {
/**
* pick the key of input value
* @param v value to compose key
* @return the key of input parameter
*/
public abstract K pickKey(V v);
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public abstract class Sorter<T> {
public enum SortType {Ascend, Decend};
private final ComparatorCallBack ascComparator = new ComparatorCallBack(this, SortType.Ascend);
private final ComparatorCallBack desComparator = new ComparatorCallBack(this, SortType.Decend);
protected abstract int compare(T t1, T t2);
public<V extends T> void sort(List<V> list, SortType sortType){
if(!CollectionUtils.isEmpty(list)){
// default to asc compare
Comparator<T> comparator = ascComparator;
if(sortType == SortType.Decend){
comparator = desComparator;
}
Collections.sort(list, comparator);
}
}
private class ComparatorCallBack implements Comparator<T>{
private Sorter<T> parent;
private SortType sortType;
public ComparatorCallBack(Sorter<T> parent, SortType sortType){
this.parent = parent;
this.sortType = sortType;
}
@Override
public int compare(T t1, T t2) {
int result;
if(sortType == SortType.Ascend){
result = parent.compare(t1, t2);
}else{
result = parent.compare(t2, t1);
}
return result;
}
}
}