Assert.java 10.6 KB
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 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
package com.java110.utils.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;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 自定义 断言
 * Created by wuxw on 2017/4/22.
 */
public class Assert extends org.springframework.util.Assert {


    public static void hasValue(Object value,String msg){
        Assert.notNull(value, msg);
        Assert.hasLength(value.toString(), msg);

    }

    /**
     * 判断 jsonObject 是否为空
     *
     * @param jsonObject
     * @param key
     * @param message
     */
    public static void isNotNull(Map 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);
    }


    /**
     * 判断 jsonObject 是否为空
     *
     * @param jsonStr
     * @param key
     * @param message
     */
    public static void jsonObjectHaveKey(String jsonStr, String key, String message) {
        Assert.hasLength(jsonStr, "不是有效的json为空," + message);
        if (isJsonObject(jsonStr)) {
            JSONObject jsonObject = JSONObject.parseObject(jsonStr);
            isNotNull(jsonObject, key, message);
        } else {
            throw new IllegalArgumentException(message);
        }
    }

    /**
     * 判断 jsonObject 是否为空
     *
     * @param info
     * @param key
     * @param message
     */
    public static void hasKey(Map info, String key, String message) {
        isNotNull(info, key, message);
    }

    /**
     * 判断 jsonObject 是否为空
     *
     * @param info
     * @param key
     * @param message
     */
    public static void hasKeyAndValue(Map info, String key, String message) {
        isNotNull(info, key, message);
        hasLength(info.get(key) == null ? "" : info.get(key).toString(), message);
    }

    /**
     * 判断 jsonObject 是否为空
     *
     * @param info
     * @param key
     * @param message
     */
    public static void hasKeyAndValue(Object info, String key, String message) {
        hasKeyAndValue(BeanConvertUtil.beanCovertMap(info), key, message);
    }


    /**
     * 判断json是否为空
     *
     * @param jsonArray
     * @param message
     */
    public static void listIsNull(List jsonArray, String message) {

        if (jsonArray != null && !jsonArray.isEmpty()) {
            throw new IllegalArgumentException(message);
        }
    }

    /**
     * 判断json是否为空
     *
     * @param jsonArray
     * @param message
     */
    public static void listNotNull(List jsonArray, String message) {

        Assert.notNull(jsonArray, message);

        if (jsonArray.size() < 1) {
            throw new IllegalArgumentException(message);
        }
    }


    /**
     * 数组只有一条数据
     *
     * @param jsonArray
     * @param message
     */
    public static void listOnlyOne(List jsonArray, String message) {

        Assert.notNull(jsonArray, message);

        if (jsonArray.size() != 1) {
            throw new IllegalArgumentException(message);
        }
    }

    /**
     * 数组只有一条数据
     *
     * @param jsonArray
     * @param message
     */
    public static void listLeastOne(List jsonArray, String message) {

        Assert.notNull(jsonArray, message);

        if (jsonArray.isEmpty()) {
            throw new IllegalArgumentException(message);
        }
    }


    /**
     * 判断list 是否为空
     *
     * @param targetList
     * @param message
     */
    public static void isNotNull(List<?> targetList, String message) {

        Assert.notNull(targetList, message);

        if (targetList.size() < 1) {
            throw new IllegalArgumentException(message);
        }
    }

    /**
     * 判断是否只有一条记录数据
     *
     * @param targetList
     * @param message
     */
    public static void isOne(List<?> targetList, String message) {
        Assert.notNull(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);
        }

    }

    /**
     * 判断 jsonObject 是否为空
     *
     * @param strValue
     * @param message
     */
    public static void isJsonObject(String strValue, String message) {
        if (!isJsonObject(strValue)) {
            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);
        }
    }

    public static void isDate(String text, String msg) {
        try {
            DateUtil.getDefaultDateFromString(text);
        } catch (Exception e) {
            throw new IllegalArgumentException(msg);
        }
    }

    public static void isDate(String text, String format, String msg) {
        try {
            DateUtil.getDateFromString(text, format);
        } catch (Exception e) {
            throw new IllegalArgumentException(msg);
        }
    }


    /**
     * 判断字符串是否是金额
     *
     * @param str 金额字符串
     * @param msg 异常时信息
     */
    public static void isMoney(String str, String msg) {
        Pattern pattern = java.util.regex.Pattern.compile("^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,2})?$"); // 判断小数点后2位的数字的正则表达式
        Matcher match = pattern.matcher(str);
        if (!match.matches()) {
            throw new IllegalArgumentException(msg);

        }
    }

    /**
     * 检验是否在 infos 中存在 flowComponent 对应组件的key
     *
     * @param infos
     * @param flowComponent
     * @param key
     * @param message
     */
    public static void hasKeyByFlowData(JSONArray infos, String flowComponent, String key, String message) {

        for (int infoIndex = 0; infoIndex < infos.size(); infoIndex++) {
            JSONObject _info = infos.getJSONObject(infoIndex);
            if (_info.containsKey(flowComponent) && _info.getString("flowComponent").equals(flowComponent)) {
                hasKeyAndValue(_info, key, message);
                break;
            }
        }

    }

    @SuppressWarnings("rawtypes")
    public static boolean objIsEmpty(Object o) {
        if (o == null) {
            return true;
        }
        if (o instanceof String) {
            if (o.toString().trim().equals("")) {
                return true;
            }
            if (o.equals("null") || o.equals("NULL")) {
                return true;
            }
        } else if (o instanceof List) {
            if (((List) o).size() == 0) {
                return true;
            }
        } else if (o instanceof Map) {
            if (((Map) o).size() == 0) {
                return true;
            }
        } else if (o instanceof Set) {
            if (((Set) o).size() == 0) {
                return true;
            }
        } else if (o instanceof Object[]) {
            if (((Object[]) o).length == 0) {
                return true;
            }
        } else if (o instanceof int[]) {
            if (((int[]) o).length == 0) {
                return true;
            }
        } else if (o instanceof long[]) {
            if (((long[]) o).length == 0) {
                return true;
            }
        }
        return false;
    }


    /**
     * 检验是否在 infos 中存在 flowComponent 对应组件的key
     *
     * @param key
     * @param message
     */
    public static void isEmail(JSONObject info, String key, String message) {
        hasKeyAndValue(info, key, message);
        if (!ValidatorUtil.isEmail(info.getString(key))) {
            throw new IllegalArgumentException(message);
        }
    }

    public static void judgeAttrValue(JSONObject paramObj){
        if (!paramObj.containsKey("attrs")) {
            return;
        }

        JSONArray attrs = paramObj.getJSONArray("attrs");
        if (attrs.size() < 1) {
            return;
        }
        JSONObject attr = null;
        for (int attrIndex = 0; attrIndex < attrs.size(); attrIndex++) {
            attr = attrs.getJSONObject(attrIndex);
            if (!"Y".equals(attr.getString("required"))) {
                continue;
            }
            Assert.hasKeyAndValue(attr, "value", attr.getString("specName") + "不能为空");

            //整数
            if ("2002".equals(attr.getString("specValueType"))) {
                Assert.isInteger(attr.getString("value"), attr.getString("specName") + "不是整数");
            }

            //整数
            if ("3003".equals(attr.getString("specValueType"))) {
                Assert.isMoney(attr.getString("value"), attr.getString("specName") + "不是金额类型 如 3.00");
            }

            // 日期4004
            if ("4004".equals(attr.getString("specValueType"))) {
                Assert.isDate(attr.getString("value"), DateUtil.DATE_FORMATE_STRING_B, attr.getString("specName") + "不是日期格式 YYYY-MM-DD");
            }

            // 日期5005
            if ("5005".equals(attr.getString("specValueType"))) {
                Assert.isDate(attr.getString("value"), DateUtil.DATE_FORMATE_STRING_A, attr.getString("specName") + "不是日期格式 YYYY-MM-DD hh:mm:ss");
            }
        }
    }


}