ComnUtil.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package cn.drewslab.heimdall.utils;
  2. import java.util.*;
  3. /**
  4. * @author gongjunlang
  5. */
  6. public class ComnUtil {
  7. /**
  8. * 判断一个对象是否为空
  9. * <p>
  10. * 以下情况为true
  11. * <p>
  12. * 对象为null
  13. * <p>
  14. * 字符串对象为""或者length为0
  15. * <p>
  16. * 集合对象size为0
  17. * <p>
  18. * 数组对象length为0
  19. *
  20. * @param obj
  21. * @return
  22. */
  23. public static boolean isEmpty(Object obj) {
  24. if (obj == null) {
  25. return true;
  26. } else if (obj instanceof String) {
  27. return ((String) obj).trim().equals("");
  28. } else if (obj instanceof StringBuilder) {
  29. return ((StringBuilder) obj).length() == 0;
  30. } else if (Collection.class.isAssignableFrom(obj.getClass())) {
  31. return ((Collection<?>) obj).size() == 0;
  32. } else if (obj instanceof Map) {
  33. return ((Map<?, ?>) obj).size() == 0;
  34. } else if (obj.getClass().isArray()) {
  35. if (obj instanceof byte[]) {
  36. return ((byte[]) obj).length == 0;
  37. }
  38. if (obj instanceof short[]) {
  39. return ((short[]) obj).length == 0;
  40. }
  41. if (obj instanceof int[]) {
  42. return ((int[]) obj).length == 0;
  43. }
  44. if (obj instanceof long[]) {
  45. return ((long[]) obj).length == 0;
  46. }
  47. if (obj instanceof char[]) {
  48. return ((char[]) obj).length == 0;
  49. }
  50. if (obj instanceof float[]) {
  51. return ((float[]) obj).length == 0;
  52. }
  53. if (obj instanceof double[]) {
  54. return ((double[]) obj).length == 0;
  55. }
  56. if (obj instanceof boolean[]) {
  57. return ((boolean[]) obj).length == 0;
  58. }
  59. return ((Object[]) obj).length == 0;
  60. } else if (obj instanceof StringBuffer) {
  61. return ((StringBuffer) obj).length() == 0;
  62. }
  63. return false;
  64. }
  65. /**
  66. * 判断一个对象是否为空
  67. * <p>
  68. * 以下情况为true
  69. * <p>
  70. * 对象为null
  71. * <p>
  72. * 字符串对象为""或者length为0
  73. * <p>
  74. * 集合对象size为0
  75. * <p>
  76. * 数组对象length为0
  77. * <p>
  78. * 数字对象值为0
  79. *
  80. * @param obj
  81. * @return
  82. */
  83. public static boolean isEmptyOrZero(Object obj) {
  84. if (Number.class.isAssignableFrom(obj.getClass())) {
  85. if (((Number) obj).intValue() == 0 || ((Number) obj).doubleValue() == 0 || ((Number) obj).longValue() == 0
  86. || ((Number) obj).floatValue() == 0 || ((Number) obj).shortValue() == 0
  87. || ((Number) obj).byteValue() == 0) {
  88. return true;
  89. }
  90. }
  91. return isEmpty(obj);
  92. }
  93. /**
  94. * 适用于非复合主键数据
  95. *
  96. * @param key
  97. * @return
  98. */
  99. public static String getStringWithSingleQuotes(String key, String split) {
  100. String[] ids = key.split(split);
  101. String result = "";
  102. for (String id : ids) {
  103. result += ",'" + id + "'";
  104. }
  105. if (result.length() > 0) {
  106. return result.substring(1);
  107. }
  108. return null;
  109. }
  110. public static List<String> string2List(String key, String split) {
  111. if (isEmpty(key)) {
  112. return new ArrayList<String>();
  113. }
  114. return Arrays.asList(key.split(split));
  115. }
  116. /**
  117. * @param src
  118. * @param concat
  119. * @return
  120. */
  121. public static <T> T[] concatArray(T[] src, T[] concat) {
  122. List<T> srcList = new ArrayList<T>(Arrays.asList(src));
  123. List<T> concatList = new ArrayList<T>(Arrays.asList(concat));
  124. srcList.addAll(concatList);
  125. return srcList.toArray(src);
  126. }
  127. // public static Object[] concatArray(Object[] src, Object[] concat) {
  128. // List<Object> srcList = new ArrayList<Object>(Arrays.asList(src));
  129. // List<Object> concatList = new ArrayList<Object>(Arrays.asList(concat));
  130. // srcList.addAll(concatList);
  131. // return srcList.toArray(src);
  132. // }
  133. }