美文网首页
写一个去除实体参数中String类型值的空格和换行工具类

写一个去除实体参数中String类型值的空格和换行工具类

作者: 我是一颗小虎牙_ | 来源:发表于2021-11-29 10:16 被阅读0次

系统中数据经常会进行新增或者更新,正常情况下如实保存就行,特殊情况下则需要对传进来的参数进行一些特殊的处理,比如说去掉前后空格或者去掉换行或者中间的若干个空格,来使数据更加严谨和准确,排除掉烂数据。(还有一大部分原因就是测试的角度太刁钻)

所以经常会对每个参数进行单独处理,所以封装一个处理的工具类,简化数据处理过程。

坐标

<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: 处理参数内前后空格
 * @date 2021/11/27 10:00
 */
public class TrimStringUtil {

    /**
     * 替换Map中的value值并转换成 T , 默认全部处理
     * <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&lt;Student&gt;(){});</p>
     * @param hashMap           原始参数键值对
     * @param typeReference     转换类型
     * @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值并转换成 T , 默认全部处理
     * <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&lt;Student&gt;(){}, true, "name", "age");</p>
     * @param hashMap           原始参数键值对
     * @param typeReference     转换类型
     * @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值并转换成 T ,根据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&lt;Student&gt;(){}, false, null);</p>
     * @param hashMap           原始参数键值对
     * @param typeReference     转换类型
     * @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));
}

结果

{"address":"    北京    ","age":"123","name":"123456"}

相关文章

网友评论

      本文标题:写一个去除实体参数中String类型值的空格和换行工具类

      本文链接:https://www.haomeiwen.com/subject/wjgkxrtx.html