系統(tǒng)中數(shù)據(jù)經(jīng)常會進行新增或者更新排拷,正常情況下如實保存就行唱矛,特殊情況下則需要對傳進來的參數(shù)進行一些特殊的處理丸氛,比如說去掉前后空格或者去掉換行或者中間的若干個空格培愁,來使數(shù)據(jù)更加嚴(yán)謹(jǐn)和準(zhǔn)確,排除掉爛數(shù)據(jù)缓窜。(還有一大部分原因就是測試的角度太刁鉆)
所以經(jīng)常會對每個參數(shù)進行單獨處理定续,所以封裝一個處理的工具類,簡化數(shù)據(jù)處理過程禾锤。
坐標(biāo)
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency>
完整代碼
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import java.util.*;
/**
* @author Surpass
* @Package com.develop
* @Description: 處理參數(shù)內(nèi)前后空格
* @date 2021/11/27 10:00
*/
public class TrimStringUtil {
/**
* 替換Map中的value值并轉(zhuǎn)換成 T , 默認(rèn)全部處理
* <p>Map<String, Object> map = new HashMap<>();</p>
* <p>map.put("name", " 123456 ");</p>
* <p>map.put("age", " 123");</p>
* <p>map.put("address", " 北京 ");</p>
* <p>Student student = TrimStringUtil.stringTrimDate(map, new TypeReference<Student>(){});</p>
* @param hashMap 原始參數(shù)鍵值對
* @param typeReference 轉(zhuǎn)換類型
* @return T
* @throws
* @author Surpass
* @date 2021/11/27 10:18
*/
public static <T> T stringTrimDate(Map<String, Object> hashMap, TypeReference<T> typeReference) {
return stringTrimDate(hashMap, typeReference, false, "");
}
/**
* 替換Map中的value值并轉(zhuǎn)換成 T , 默認(rèn)全部處理
* <p>Map<String, Object> map = new HashMap<>();</p>
* <p>map.put("name", " 123456 ");</p>
* <p>map.put("age", " 123");</p>
* <p>map.put("address", " 北京 ");</p>
* <p>Student student = TrimStringUtil.stringTrimDate(map, new TypeReference<Student>(){}, true, "name", "age");</p>
* @param hashMap 原始參數(shù)鍵值對
* @param typeReference 轉(zhuǎn)換類型
* @return T
* @throws
* @author Surpass
* @date 2021/11/27 10:18
*/
public static <T> T stringTrimDate(Map<String, Object> hashMap, TypeReference<T> typeReference,
boolean isInclude, String... keys) {
return stringTrimDate(hashMap, typeReference, isInclude, Arrays.asList(keys));
}
/**
* 替換Map中的value值并轉(zhuǎn)換成 T 私股,根據(jù)isInclude判斷需要處理的字段值
* <p>Map<String, Object> map = new HashMap<>();</p>
* <p>map.put("name", " 123456 ");</p>
* <p>map.put("age", " 123");</p>
* <p>map.put("address", " 北京 ");</p>
* <p>Student student = TrimStringUtil.stringTrimDate(map, new TypeReference<Student>(){}, false, null);</p>
* @param hashMap 原始參數(shù)鍵值對
* @param typeReference 轉(zhuǎn)換類型
* @param isInclude 是否包含keys中的字段
* @param keyList 字段枚舉
* @return T
* @throws
* @author Surpass
* @date 2021/11/27 10:15
*/
public static <T> T stringTrimDate(Map<String, Object> hashMap, TypeReference<T> typeReference, boolean isInclude, List<String> keyList){
if (keyList == null) {
keyList = new ArrayList<String>(){{
this.add("");
}};
}
Set<Map.Entry<String, Object>> entries = hashMap.entrySet();
for (Map.Entry<String, Object> entry : entries) {
if (entry.getValue() != null){
String key = entry.getKey();
Object paramValue = entry.getValue();
if (paramValue instanceof String){
String value = (String)paramValue;
if ((isInclude && keyList.contains(key)) || (!isInclude && !keyList.contains(key))) {
String dealString = value.trim().replaceAll("\r\n", "").replaceAll("\\s+", "");
entry.setValue(dealString);
}
}
}
}
return JSON.parseObject(JSONObject.toJSONString(hashMap), typeReference);
}
}
測試類
public static void main(String[] args) throws Exception {
TestTest testTest = new TestTest();
Map<String, Object> map = new HashMap<>();
map.put("name", " 123456 ");
map.put("age", " 123");
map.put("address", " 北京 ");
List<String> list = new ArrayList<String>() {{
this.add("name");
this.add("age");
}};
Student student = TrimStringUtil.stringTrimDate(map, new TypeReference<Student>() {}, true, list);
System.out.println(JSONObject.toJSONString(student));
}
結(jié)果
{"address":" 北京 ","age":"123","name":"123456"}