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
package com.java110.common.util;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
 
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 isNull(JSONObject jsonObject,String key,String message){
        Assert.isNull(jsonObject,message);
 
        if(!jsonObject.containsKey(key)){
            throw new IllegalArgumentException(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;
    }
}