很不错的Map工具类

将需要操作(主要用于取值)的Map对象放入该类,然后可以自动的进行类型转换,获取你需要的类型值

package utils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by adinlead on 16/09/28.
 */
public class MapShell {
//    要被包装的Map
    Map map;

    public MapShell() {
            this.map = new HashMap();
    }

    public MapShell(Map map) {
        if (map == null) {
            this.map = new HashMap();
        } else {
            this.map = map;
        }
    }

    public MapShell(Map map, Map spare) {
        if (map == null) {
            this.map = spare;
        } else {
            this.map = map;
        }
    }

    public Map getMap() {
        return map;
    }

    public boolean notEmpty(){
        return this.map.size() > 0;
    }

    public boolean notEmpty(Object key){
        return this.map.containsKey(key) && this.map.get(key) != null && !TextUtil.isEmpty(this.getString(key));
    }

    public boolean isEmpty(){
        return this.map.size() <= 0;
    }

    public boolean isEmpty(Object key){
        return !this.map.containsKey(key) || TextUtil.isEmpty(this.getString(key));
    }

    public Object get(Object key){
        return this.map.get(key);
    }

    public void put(Object key,Object value){
        this.map.put(key,value);
    }

    public Integer getInteger(Object key) {
        return getInteger(key, null);
    }

    public Integer getInteger(Object key, Integer spare) {
        if (this.map.containsKey(key)) {
            Object o = this.map.get(key);
            if (o instanceof Integer) {
                return (Integer) o;
            } else {
                try {
                    return Integer.valueOf(o.toString());
                } catch (NumberFormatException e) {
                    return spare;
                }
            }
        }
        return spare;
    }

    public Long getLong(Object key) {
        return getLong(key, null);
    }

    public Long getLong(Object key, Long spare) {
        if (this.map.containsKey(key)) {
            Object o = this.map.get(key);
            if (o instanceof Long) {
                return (Long) o;
            } else {
                try {
                    return Long.valueOf(o.toString());
                } catch (NumberFormatException e) {
                    return spare;
                }
            }
        }
        return spare;
    }

    public Double getDouble(Object key){
        return getDouble(key,null);
    }

    public Double getDouble(Object key, Double spare) {
        if (this.map.containsKey(key)) {
            Object o = this.map.get(key);
            if (o instanceof Double) {
                return (Double) o;
            } else {
                try {
                    return Double.valueOf(o.toString());
                } catch (NumberFormatException e) {
                    return spare;
                }
            }
        }
        return spare;
    }

    public String getString(Object key) {
        return getString(key, null);
    }

    public String getString(Object key, String spare) {
        if (this.map.containsKey(key)) {
            return String.valueOf(this.map.get(key));
        }
        return spare;
    }

    public int size(){
        return this.map.size();
    }

    public boolean valueEquation(Object key, Object val) {
        Object o = this.map.get(key);
        if (o == val)
            return true;
        else
            return val.equals(val);
    }

    /**
     * @function 保留键存在于参数中的的值
     * @param keys
     */
    public void retain(Object... keys){
        Map tmp = this.map;
        this.map.clear();
        for(Object key:keys){
            this.map.put(key,tmp.get(key));
        }
    }
}
class TextUtil {
    /**
     * Returns true if the string is null or 0-length.
     *
     * @param str the string to be examined
     * @return true if str is null or zero length
     */
    public static boolean isEmpty(CharSequence str) {
        if (str == null || str.length() == 0)
            return true;
        else
            return false;
    }

    /**
     * Returns the length that the specified CharSequence would have if
     * spaces and control characters were trimmed from the start and end,
     * as by {@link String#trim}.
     */
    public static int getTrimmedLength(CharSequence s) {
        int len = s.length();

        int start = 0;
        while (start < len && s.charAt(start) <= ' ') {
            start++;
        }

        int end = len;
        while (end > start && s.charAt(end - 1) <= ' ') {
            end--;
        }

        return end - start;
    }

    public static String stringOf(Object object) {
        if (object == null)
            return null;
        else
            return object.toString();
    }

    public static Boolean isInteger(String str) {
        return str != null && str.matches("[0-9]*");
    }

    public static Boolean isDecimal(String str){
        return str!=null && str.matches("^[0-9]+\\.([0-9]+)?$");
    }

    public static Boolean isNumber(String str){
        return str!= null && str.matches("^[0-9]+(\\.[0-9]+)?$");
    }

    public static Integer parseInt(String str,int standby){
        if(str == null) return standby;
        try {
            return Integer.parseInt(str);
        }catch (NumberFormatException e){
            return standby;
        }
    }

    public static String getFileRandomName(String superaddition){
        String name = System.currentTimeMillis() + "_";
        int random = (int) (Math.random() * 9000 + 1000);
        name += random;
        if(superaddition != null){
            name += "_" + superaddition;
        }
        return name;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值