shiyj
2019-09-02 b784175e978b0fcfca8ca8bed7953ae4eacf49e8
java110-common/src/main/java/com/java110/common/util/Assert.java
@@ -6,6 +6,8 @@
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
 * 自定义 断言
@@ -105,6 +107,22 @@
        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);
        }
    }
@@ -211,4 +229,40 @@
            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;
            }
        }
    }
}