package com.irrigation.icl.utils; import com.google.common.collect.Lists; import com.irrigation.icl.exception.ContextRuntimeException; import org.springframework.beans.BeanUtils; import org.springframework.util.StringUtils; import java.util.*; /** * 对象工具类 */ public class ObjectUtils { /** * 复制对象错误提示 */ private final static String COPY_OBJECT_FAIL = "复制对象失败"; private ObjectUtils() { } /** * 判断对象司是否是空 * * @param obj * @return */ public static boolean isNull(Object obj) { return obj == null; } /** * 判断对象司是否不是空 * * @param obj * @return */ public static boolean nonNull(Object obj) { return obj != null; } /** * 判断两个对象是否相等 * * @param a * @param b * @return */ public static boolean equals(Object a, Object b) { return a != null && a.equals(b); } /** * 判断两个对象是否不相等 * * @param a * @param b * @return */ public static boolean unequal(Object a, Object b) { return (a != null && !a.equals(b)) || (b != null && !b.equals(a)); } /** * 判断集合是否是空 * * @param collection * @return */ public static boolean isEmpty(Collection collection) { return collection == null || collection.isEmpty(); } /** * 判断集合是否不是空 * * @param collection * @return */ public static boolean nonEmpty(Collection collection) { return !isEmpty(collection); } /** * 判断map 是否是空 * * @param map * @return */ public static boolean isEmpty(Map map) { return map == null || map.isEmpty(); } /** * 判断map 是否不是空 * * @param map * @return */ public static boolean nonEmpty(Map map) { return !isEmpty(map); } /** * 判断字符串 是否是 null 或 length=0 * * @param cs * @return */ public static boolean stringIsEmpty(CharSequence cs) { return cs == null || cs.length() == 0; } /** * 判断字符串 是否不是 null 并且 length!=0 * * @param cs * @return */ public static boolean stringNonEmpty(CharSequence cs) { return !stringIsEmpty(cs); } /** * 判断字符是否是空字符 * * @param cs * @return */ public static boolean stringIsBlank(CharSequence cs) { int strLen; if (cs == null || (strLen = cs.length()) == 0) { return true; } for (int i = 0; i < strLen; i++) { if (!Character.isWhitespace(cs.charAt(i))) { return false; } } return true; } /** * 判断字符是否是空字符 * * @param cs * @return */ public static boolean stringNonBlankB(CharSequence cs) { return !stringIsBlank(cs); } /** * 去掉字符串前后空白字符 * * @param str * @return */ public static String stringTrim(final String str) { return str == null ? null : str.trim(); } /** * 单个对象复制 * * @param source * @param targetClass * @param * @return */ public static T copyObject(Object source, Class targetClass) { if (isNull(source)) { return null; } try { T target = targetClass.newInstance(); BeanUtils.copyProperties(source, target); return target; } catch (Exception e) { throw new ContextRuntimeException(COPY_OBJECT_FAIL, e); } } /** * 集合复制 * * @param sourceList * @param targetClass * @param * @return */ public static List copyObjectList(List sourceList, Class targetClass) { List targetList = Lists.newArrayList(); if (nonEmpty(sourceList)) { for (S source : sourceList) { targetList.add(copyObject(source, targetClass)); } } return targetList; } /** * 集合通过连接符号返回字符串 * * @param iterable * @param separator * @return */ public static String stringJoin(Iterable iterable, String separator) { return iterable == null ? null : stringJoin(iterable.iterator(), separator); } private static String stringJoin(Iterator iterator, String separator) { if (iterator == null) { return null; } else if (!iterator.hasNext()) { return ""; } else { Object first = iterator.next(); if (!iterator.hasNext()) { return Objects.toString(first, ""); } else { StringBuilder buf = new StringBuilder(256); if (first != null) { buf.append(first); } while (iterator.hasNext()) { if (separator != null) { buf.append(separator); } Object obj = iterator.next(); if (obj != null) { buf.append(obj); } } return buf.toString(); } } } }