zhanghua
2023-09-08 7ef4892f9f24f941aca37e6b3991b808a0aca619
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
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;
    }
}