转载

java中的万能实体类

万能实体类可以少写一些代码,挺方便的一个工具包

版权声明:本文由 低调小熊猫 发表于 低调小熊猫的博客

转载声明:自由转载-非商用-非衍生-保持署名,非商业转载请注明作者及出处,商业转载请联系作者本人qq:2696284032

文章链接: https://aodeng.cc/archives/javazdwnstl

代码

package com.account.web.vo.base;

import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import com.account.core.util.DateUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
@SuppressWarnings({ "rawtypes", "unchecked" })
public class PageData extends HashMap implements Map{

    private static final long serialVersionUID = 1L;

    Map map = null;
    HttpServletRequest request;

    public PageData(HttpServletRequest request){
        this.request = request;
        Map properties = request.getParameterMap();
        Map returnMap = new HashMap(); 
        Iterator entries = properties.entrySet().iterator(); 
        Map.Entry entry; 
        String name = "";  
        String value = "";  
        while (entries.hasNext()) {
            entry = (Map.Entry) entries.next(); 
            name = (String) entry.getKey(); 
            Object valueObj = entry.getValue(); 
            if(null == valueObj){ 
                value = ""; 
            }else if(valueObj instanceof String[]){ 
                String[] values = (String[])valueObj;
                for(int i=0;i<values.length;i++){ 
                     value = values[i] + ",";
                }
                value = value.substring(0, value.length()-1); 
            }else{
                value = valueObj.toString(); 
            }
            returnMap.put(name, value); 
        }
        map = returnMap;
    }

    public PageData() {
        map = new HashMap();
    }

    @Override
    public Object get(Object key) {
        Object obj = null;
        if(map.get(key) instanceof Object[]) {
            Object[] arr = (Object[])map.get(key);
            obj = request == null ? arr:(request.getParameter((String)key) == null ? arr:arr[0]);
        } else {
            obj = map.get(key);
        }
        return obj;
    }

    public String getString(Object key) {
        if(null==key){
            return "";
        }
        Object v = get(key);
        if(null==v){
            return "";
        }
        return v.toString();
    }
    public Integer getInt(Object key) {
        if (null == key) {
            return null;
        }
        Object v = get(key);
        if (null == v) {
            return null;
        }
        return (int) Double.parseDouble(v.toString());
    }

    public Long getLong(Object key) {
        if (null == key) {
            return null;
        }
        Object v = get(key);
        if (null == v) {
            return null;
        }
        return Long.parseLong(v.toString());
    }

    public Double getDouble(Object key) {
        if (null == key) {
            return null;
        }
        Object v = get(key);
        if (null == v) {
            return null;
        }
        return Double.parseDouble(v.toString());
    }

    public Date getDate(Object key) {
        if (null == key) {
            return null;
        }
        Object v = get(key);
        if (null == v) {
            return null;
        }
        return DateUtil.fomatDate(v.toString());
    }
    @Override
    public Object put(Object key, Object value) {
        return map.put(key, value);
    }

    @Override
    public Object remove(Object key) {
        return map.remove(key);
    }

    public void clear() {
        map.clear();
    }

    public boolean containsKey(Object key) {
        // TODO Auto-generated method stub
        return map.containsKey(key);
    }

    public boolean containsValue(Object value) {
        // TODO Auto-generated method stub
        return map.containsValue(value);
    }

    public Set entrySet() {
        // TODO Auto-generated method stub
        return map.entrySet();
    }

    public boolean isEmpty() {
        // TODO Auto-generated method stub
        return map.isEmpty();
    }

    public Set keySet() {
        // TODO Auto-generated method stub
        return map.keySet();
    }

    public void putAll(Map t) {
        // TODO Auto-generated method stub
        map.putAll(t);
    }

    public int size() {
        // TODO Auto-generated method stub
        return map.size();
    }

    public Collection values() {
        // TODO Auto-generated method stub
        return map.values();
    }

    public static String toListString(List<PageData> list) {
        JSONArray ja = new JSONArray();
        for (PageData new_pd : list) {
            ja.add(JSONObject.fromObject(new_pd));
        }
        return ja.toString();
    }
}
原文  https://aodeng.cc/archives/javazdwnstl
正文到此结束
Loading...