New file |
| | |
| | | package com.ycl.common.util; |
| | | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.io.Serializable; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.Date; |
| | | import java.util.HashMap; |
| | | import java.util.Iterator; |
| | | |
| | | @Slf4j |
| | | public class ParamsMap extends HashMap<String, Object> implements Serializable{ |
| | | |
| | | private static final long serialVersionUID = -6716714932665322981L; |
| | | |
| | | public ParamsMap(){} |
| | | |
| | | public ParamsMap(HttpServletRequest request) { |
| | | if(request==null)return; |
| | | Iterator<Entry<String, String[]>> entries = request.getParameterMap().entrySet().iterator(); |
| | | Entry<String, String[]> entry; |
| | | String name = ""; |
| | | while (entries.hasNext()) { |
| | | String value = ""; |
| | | entry = (Entry<String, String[]>) 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(); |
| | | } |
| | | this.put(name, value); |
| | | } |
| | | } |
| | | |
| | | public ParamsMap(String code, Object value) { |
| | | this.put(code, value); |
| | | } |
| | | |
| | | /** |
| | | * |
| | | * @Title: getString |
| | | * @Description: 获取字符串值 |
| | | * @param key |
| | | * @return |
| | | */ |
| | | public String getString(String key) { |
| | | try { |
| | | if (containsKey(key)) { |
| | | if(get(key)!=null) return get(key).toString(); |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("ParamsMap getString is error:{}",e); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * |
| | | * @Title: getInteter |
| | | * @Description: 获取整型值 |
| | | * @param key |
| | | * @return |
| | | */ |
| | | public Integer getInteter(String key) { |
| | | try { |
| | | if (containsKey(key)) { |
| | | if(get(key)!=null && !"".equals(get(key))) |
| | | return Integer.parseInt(getString(key)); |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("ParamsMap getInteter is error:{}",e); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * |
| | | * @Title: getAsLong |
| | | * @Description: 获取Long值 |
| | | * @param key |
| | | * @return |
| | | */ |
| | | public Long getLong(String key){ |
| | | try { |
| | | if (containsKey(key)) { |
| | | if(get(key)!=null && !"".equals(get(key))) |
| | | return Long.parseLong(getString(key)); |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("ParamsMap getLong is error:{}",e); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * |
| | | * @Title: getAsDouble |
| | | * @Description: 获取Double值 |
| | | * @param key |
| | | * @return |
| | | */ |
| | | public Double getDouble(String key){ |
| | | try { |
| | | if (containsKey(key)) { |
| | | if(get(key)!=null && !"".equals(get(key))) |
| | | return Double.parseDouble(getString(key)); |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("ParamsMap getDouble is error:{}",e); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * |
| | | * @Title: getFloat |
| | | * @Description: 获取Float值 |
| | | * @param key |
| | | * @return |
| | | */ |
| | | public Float getFloat(String key){ |
| | | try { |
| | | if (containsKey(key)) { |
| | | if(get(key)!=null && !"".equals(get(key))) |
| | | return Float.parseFloat(getString(key)); |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("ParamsMap getFloat is error:{}",e); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * |
| | | * @Title: getBoolean |
| | | * @Description: 获取Boolean值 |
| | | * @param key |
| | | * @return |
| | | */ |
| | | public Boolean getBoolean(String key){ |
| | | try { |
| | | if (containsKey(key)) { |
| | | if(get(key)!=null && !"".equals(get(key))) |
| | | return Boolean.parseBoolean(getString(key)); |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("ParamsMap getBoolean is error:{}",e); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * |
| | | * @Title: getDate |
| | | * @Description: 获取日期值 |
| | | * @param key |
| | | * @return |
| | | */ |
| | | public Date getDate(String key){ |
| | | try { |
| | | if (containsKey(key)) { |
| | | SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | if(get(key)!=null && !"".equals(get(key))) |
| | | return df.parse(get(key).toString()); |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("ParamsMap getDate is error:{}",e); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * |
| | | * @Title: getStringArray |
| | | * @Description: 获取字符串数组 |
| | | * @param key |
| | | * @return |
| | | */ |
| | | public String[] getArray(String key){ |
| | | try { |
| | | if (containsKey(key)) { |
| | | if(get(key)==null && !"".equals(get(key))) return new String[0]; |
| | | return get(key).toString().split(","); |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("ParamsMap getStringArray is error:{}",e); |
| | | e.printStackTrace(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * |
| | | * @Title: getIntegerArray |
| | | * @Description: 获取整型数组 |
| | | * @param key |
| | | * @return |
| | | */ |
| | | public Integer[] getIntegerArray(String key){ |
| | | try { |
| | | if (containsKey(key)) { |
| | | String [] tmp = getArray(key); |
| | | if(tmp==null)return null; |
| | | Integer []tmpInt = new Integer[tmp.length]; |
| | | int index = 0; |
| | | for(String s:tmp){ |
| | | if(s==null||s.equals(""))continue; |
| | | tmpInt[index]=Integer.parseInt(s); |
| | | } |
| | | return tmpInt; |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("ParamsMap getAsIntegerArray is error:{}",e); |
| | | e.printStackTrace(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | public Long[] getLongArray(String key) { |
| | | try { |
| | | if(containsKey(key)) { |
| | | String [] tmp = getArray(key); |
| | | if(tmp==null)return null; |
| | | Long [] tmpInt = new Long[tmp.length]; |
| | | int index = 0; |
| | | for(String s:tmp){ |
| | | if(s==null||s.equals(""))continue; |
| | | tmpInt[index]=Long.parseLong(s); |
| | | } |
| | | return tmpInt; |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("ParamsMap getLongArray is error:{}",e); |
| | | e.printStackTrace(); |
| | | } |
| | | return null; |
| | | } |
| | | } |