Assert.java 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. package com.java110.utils.util;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import org.apache.commons.lang3.StringUtils;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.Set;
  8. import java.util.regex.Matcher;
  9. import java.util.regex.Pattern;
  10. /**
  11. * 自定义 断言
  12. * Created by wuxw on 2017/4/22.
  13. */
  14. public class Assert extends org.springframework.util.Assert {
  15. /**
  16. * 判断 jsonObject 是否为空
  17. *
  18. * @param jsonObject
  19. * @param key
  20. * @param message
  21. */
  22. public static void isNotNull(Map jsonObject, String key, String message) {
  23. Assert.notEmpty(jsonObject, message);
  24. if (!jsonObject.containsKey(key)) {
  25. throw new IllegalArgumentException(message);
  26. }
  27. }
  28. /**
  29. * 判断 jsonObject 是否为空
  30. *
  31. * @param jsonObject
  32. * @param key
  33. * @param message
  34. */
  35. public static void jsonObjectHaveKey(JSONObject jsonObject, String key, String message) {
  36. isNotNull(jsonObject, key, message);
  37. }
  38. /**
  39. * 判断 jsonObject 是否为空
  40. *
  41. * @param jsonStr
  42. * @param key
  43. * @param message
  44. */
  45. public static void jsonObjectHaveKey(String jsonStr, String key, String message) {
  46. Assert.hasLength(jsonStr, "不是有效的json为空," + message);
  47. if (isJsonObject(jsonStr)) {
  48. JSONObject jsonObject = JSONObject.parseObject(jsonStr);
  49. isNotNull(jsonObject, key, message);
  50. } else {
  51. throw new IllegalArgumentException(message);
  52. }
  53. }
  54. /**
  55. * 判断 jsonObject 是否为空
  56. *
  57. * @param info
  58. * @param key
  59. * @param message
  60. */
  61. public static void hasKey(Map info, String key, String message) {
  62. isNotNull(info, key, message);
  63. }
  64. /**
  65. * 判断 jsonObject 是否为空
  66. *
  67. * @param info
  68. * @param key
  69. * @param message
  70. */
  71. public static void hasKeyAndValue(Map info, String key, String message) {
  72. isNotNull(info, key, message);
  73. hasLength(info.get(key) == null ? "" : info.get(key).toString(), message);
  74. }
  75. /**
  76. * 判断json是否为空
  77. *
  78. * @param jsonArray
  79. * @param message
  80. */
  81. public static void listIsNull(List jsonArray, String message) {
  82. if (jsonArray != null && jsonArray.size() > 0) {
  83. throw new IllegalArgumentException(message);
  84. }
  85. }
  86. /**
  87. * 判断json是否为空
  88. *
  89. * @param jsonArray
  90. * @param message
  91. */
  92. public static void listNotNull(List jsonArray, String message) {
  93. Assert.notNull(jsonArray, message);
  94. if (jsonArray.size() < 1) {
  95. throw new IllegalArgumentException(message);
  96. }
  97. }
  98. /**
  99. * 数组只有一条数据
  100. *
  101. * @param jsonArray
  102. * @param message
  103. */
  104. public static void listOnlyOne(List jsonArray, String message) {
  105. Assert.notNull(jsonArray, message);
  106. if (jsonArray.size() != 1) {
  107. throw new IllegalArgumentException(message);
  108. }
  109. }
  110. /**
  111. * 判断list 是否为空
  112. *
  113. * @param targetList
  114. * @param message
  115. */
  116. public static void isNotNull(List<?> targetList, String message) {
  117. Assert.notNull(targetList, message);
  118. if (targetList.size() < 1) {
  119. throw new IllegalArgumentException(message);
  120. }
  121. }
  122. /**
  123. * 判断是否只有一条记录数据
  124. *
  125. * @param targetList
  126. * @param message
  127. */
  128. public static void isOne(List<?> targetList, String message) {
  129. Assert.notNull(targetList, message);
  130. if (targetList.size() != 1) {
  131. throw new IllegalArgumentException(message);
  132. }
  133. }
  134. /**
  135. * 校验map 中是否有值
  136. *
  137. * @param targetMap
  138. * @param message
  139. */
  140. public static void hasSize(Map<?, ?> targetMap, String message) {
  141. Assert.isNull(targetMap, message);
  142. if (targetMap.size() < 1) {
  143. throw new IllegalArgumentException(message);
  144. }
  145. }
  146. /**
  147. * 判断 jsonObject 是否为空
  148. *
  149. * @param strValue
  150. * @param message
  151. */
  152. public static void isJsonObject(String strValue, String message) {
  153. if (!isJsonObject(strValue)) {
  154. throw new IllegalArgumentException(message);
  155. }
  156. }
  157. /**
  158. * 校验是否为JSON
  159. *
  160. * @param msg
  161. * @return
  162. */
  163. public static Boolean isJsonObject(String msg) {
  164. try {
  165. JSONObject.parseObject(msg);
  166. } catch (Exception e) {
  167. return false;
  168. }
  169. return true;
  170. }
  171. public static Boolean isPageJsonObject(String msg) {
  172. try {
  173. JSONObject jsonObject = JSONObject.parseObject(msg);
  174. if (!jsonObject.containsKey("meta") || !jsonObject.containsKey("param")) {
  175. return false;
  176. }
  177. } catch (Exception e) {
  178. return false;
  179. }
  180. return true;
  181. }
  182. /**
  183. * 校验是否为整数
  184. *
  185. * @param text
  186. * @param msg
  187. */
  188. public static void isInteger(String text, String msg) {
  189. if (!StringUtils.isNumeric(text)) {
  190. throw new IllegalArgumentException(msg);
  191. }
  192. }
  193. public static void isDate(String text, String msg) {
  194. try {
  195. DateUtil.getDefaultDateFromString(text);
  196. } catch (Exception e) {
  197. throw new IllegalArgumentException(msg);
  198. }
  199. }
  200. public static void isDate(String text, String format, String msg) {
  201. try {
  202. DateUtil.getDateFromString(text, format);
  203. } catch (Exception e) {
  204. throw new IllegalArgumentException(msg);
  205. }
  206. }
  207. /**
  208. * 判断字符串是否是金额
  209. *
  210. * @param str 金额字符串
  211. * @param msg 异常时信息
  212. */
  213. public static void isMoney(String str, String msg) {
  214. Pattern pattern = java.util.regex.Pattern.compile("^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,2})?$"); // 判断小数点后2位的数字的正则表达式
  215. Matcher match = pattern.matcher(str);
  216. if (!match.matches()) {
  217. throw new IllegalArgumentException(msg);
  218. }
  219. }
  220. /**
  221. * 检验是否在 infos 中存在 flowComponent 对应组件的key
  222. *
  223. * @param infos
  224. * @param flowComponent
  225. * @param key
  226. * @param message
  227. */
  228. public static void hasKeyByFlowData(JSONArray infos, String flowComponent, String key, String message) {
  229. for (int infoIndex = 0; infoIndex < infos.size(); infoIndex++) {
  230. JSONObject _info = infos.getJSONObject(infoIndex);
  231. if (_info.containsKey(flowComponent) && _info.getString("flowComponent").equals(flowComponent)) {
  232. hasKeyAndValue(_info, key, message);
  233. break;
  234. }
  235. }
  236. }
  237. @SuppressWarnings("rawtypes")
  238. public static boolean objIsEmpty(Object o) {
  239. if (o == null) {
  240. return true;
  241. }
  242. if (o instanceof String) {
  243. if (o.toString().trim().equals("")) {
  244. return true;
  245. }
  246. if (o.equals("null") || o.equals("NULL")) {
  247. return true;
  248. }
  249. } else if (o instanceof List) {
  250. if (((List) o).size() == 0) {
  251. return true;
  252. }
  253. } else if (o instanceof Map) {
  254. if (((Map) o).size() == 0) {
  255. return true;
  256. }
  257. } else if (o instanceof Set) {
  258. if (((Set) o).size() == 0) {
  259. return true;
  260. }
  261. } else if (o instanceof Object[]) {
  262. if (((Object[]) o).length == 0) {
  263. return true;
  264. }
  265. } else if (o instanceof int[]) {
  266. if (((int[]) o).length == 0) {
  267. return true;
  268. }
  269. } else if (o instanceof long[]) {
  270. if (((long[]) o).length == 0) {
  271. return true;
  272. }
  273. }
  274. return false;
  275. }
  276. /**
  277. * 检验是否在 infos 中存在 flowComponent 对应组件的key
  278. *
  279. * @param key
  280. * @param message
  281. */
  282. public static void isEmail(JSONObject info, String key, String message) {
  283. hasKeyAndValue(info, key, message);
  284. if (!ValidatorUtil.isEmail(info.getString(key))) {
  285. throw new IllegalArgumentException(message);
  286. }
  287. }
  288. }