All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.onetwo.common.utils.ParamUtils Maven / Gradle / Ivy

There is a newer version: 4.7.2
Show newest version
package org.onetwo.common.utils;

import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import org.onetwo.common.reflect.ReflectUtils;

public abstract class ParamUtils {

    public static String objectToParamString(Object obj){
    	return comparableKeyMapToParamString(ReflectUtils.getBeanToMapConvertor().toFlatMap(obj));
    }

    public static > String comparableKeyMapToParamString(Map params){
    	return toParamString(params, Comparator.comparing(e->e));
    }
    public static String mapToParamString(Map params){
    	return toParamString(params, null);
    }
    
    public static  String toParamString(Map params, Comparator comparator){
		Map map = null;
		if(comparator==null){
			map = new HashMap<>(params);
		}else{
			map = new TreeMap<>(comparator);
			map.putAll(params);
//			System.out.println("sortmap:"+map);
		}
		
		StringBuilder sb = new StringBuilder();
		int index = 0;
		for(Map.Entry entry : (Set>)map.entrySet()){
			if(entry.getValue()==null)
				continue;
			Collection values = CUtils.toCollection(entry.getValue());
			for(Object value : values){
				if(index!=0)
					sb.append("&");
				sb.append(entry.getKey()).append("=").append(value.toString());
				index++;
			}
		}
		return sb.toString();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy