JAvaデータ型相互変換ツールクラス


package com.rest.ful.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 *          
 * @author zlzhaoe
 * @version [   , 2017 5 8 ]
 * @see  [   /  ]
 * @since  [  /    ]
 */
public class ConverterUtils {
    /**
     * 
     * 
     * @param obj      string   
     * @param defaultVal    
     * @return obj   string
     */
    public static String toString(Object obj, String defaultVal){
        return (obj != null) ? obj.toString() : defaultVal;
    }
    /**
     * 
     * 
     * @param obj      string   
     * @return       string    
     */
    public static String toString(Object obj){
        return toString(obj, "");
    }

    /**
     * 
     * 
     * @param obj      int   
     * @param defaultVal    
     * @return obj    int 
     */
    public static Integer toInt(Object obj, Integer defaultVal) {
        try
        {
            return (obj != null) ? Integer.parseInt(toString(obj, "0")) : defaultVal;
        }catch(Exception e)
        {
        }
        return defaultVal;
    }

    /**
     * 
     * 
     * @param obj      int   
     * @param defaultVal    
     * @return obj    int 
     */
    public static Integer toInt(Object obj)
    {
        return toInt(obj, 0);
    }

    /**
     * 
     * 
     * @param obj      Integer   
     * @return obj    Integer 
     */
    public static Integer toInteger(Object obj)
    {
        return toInt(obj, null);
    }

    /**
     * 
     * 
     * @param obj      int   
     * @param defaultVal    
     * @return obj    int 
     */
    public static Float toFloat(Object obj, float defaultVal)
    {
        return (obj != null) ? Float.parseFloat(toString(obj, "0")) : defaultVal;
    }

    /**
     * 
     * 
     * @param obj      Float   
     * @return obj    Float 
     */
    public static Float toFloat(Object obj)
    {
        return toFloat(obj, 0);
    }

    /**
     * 
     * 
     * @param obj        
     * @param defaultVal    
     * @return   obj       ,          long  
     */
    public static Long toLong(Object obj, long defaultVal)
    {
        return (obj != null) ? Long.parseLong(toString(obj)) : defaultVal;
    }

    /**
     * 
     * 
     * @param obj        
     * @return   obj        0l,          long  
     */
    public static Long toLong(Object obj)
    {
        return toLong(obj, 0l);
    }

    /** 
     *  object   double  ,        defaultVal
     * @param obj        
     * @param defaultVal    
     * @return       
     */
    public static Double toDouble(Object obj,Double defaultVal)
    {
        try
        {
            return Double.parseDouble(obj.toString());
        }
        catch(Exception e)
        {
            return defaultVal;
        }
    }

    /** 
     *  object   double  ,        0d
     * @param obj        
     * @return       
     */
    public static double toDouble(Object obj)
    {
        return toDouble(obj,0d);
    }

    /**
     *    List>>
     * 
     * @param list      list
     * @return      
     */
    @SuppressWarnings("unchecked")
    public static List> converterForMapList(List list)
    {
        List> result = new ArrayList>();
        for (Object tempObj : list)
        {
            result.add((HashMap)tempObj);
        }
        return result;
    }
}