wuxw7
2018-05-22 9e0fa31a2b81a9d2fd2588b78b6de637f0352639
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
package com.java110.common.util;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
 
import java.util.List;
import java.util.Map;
 
/**
 * 自定义 断言
 * Created by wuxw on 2017/4/22.
 */
public class Assert extends org.springframework.util.Assert{
 
    /**
     * 判断 jsonObject 是否为空
     * @param jsonObject
     * @param key
     * @param message
     */
    public static void isNotNull(JSONObject jsonObject,String key,String message){
        Assert.notEmpty(jsonObject,message);
 
        if(!jsonObject.containsKey(key)){
            throw new IllegalArgumentException(message) ;
        }
    }
 
    /**
     * 判断 jsonObject 是否为空
     * @param jsonObject
     * @param key
     * @param message
     */
    public static void jsonObjectHaveKey(JSONObject jsonObject,String key,String message){
        isNotNull(jsonObject,key,message);
    }
 
    /**
     * 判断json是否为空
     * @param jsonArray
     * @param message
     */
    public static void isNull(JSONArray jsonArray,String message){
 
        Assert.isNull(jsonArray,message);
 
        if(jsonArray.size() < 1 ){
            throw new IllegalArgumentException(message) ;
        }
    }
 
    /**
     * 判断list 是否为空
     * @param targetList
     * @param message
     */
    public static void isNull(List<?> targetList , String message){
 
        Assert.isNull(targetList,message);
 
        if(targetList.size()< 1){
            throw new IllegalArgumentException(message) ;
        }
    }
 
    /**
     * 判断是否只有一条记录数据
     * @param targetList
     * @param message
     */
    public static void isOne(List<?> targetList,String message){
        Assert.isNull(targetList,message);
 
        if(targetList.size() != 1){
            throw new IllegalArgumentException(message) ;
        }
    }
 
    /**
     * 校验map 中是否有值
     * @param targetMap
     * @param message
     */
    public static void hasSize(Map<?,?> targetMap, String message){
        Assert.isNull(targetMap,message);
 
        if(targetMap.size() < 1){
            throw new IllegalArgumentException(message);
        }
 
    }
 
    /**
     * 校验是否为JSON
     * @param msg
     * @return
     */
    public static Boolean isJsonObject(String msg) {
        try{
            JSONObject.parseObject(msg);
        }catch (Exception e){
            return false;
        }
        return true;
    }
 
    public static Boolean isPageJsonObject(String msg){
        try{
            JSONObject jsonObject = JSONObject.parseObject(msg);
            if(!jsonObject.containsKey("meta") || !jsonObject.containsKey("param")){
                return false;
            }
        }catch (Exception e){
            return false;
        }
        return true;
    }
 
    /**
     * 校验是否为整数
     * @param text
     * @param msg
     */
    public static void isInteger(String text,String msg){
        if(!StringUtils.isNumeric(text)){
            throw new IllegalArgumentException(msg);
        }
    }
}