LangUtils { public static boolean hasElement(Object obj){ if(obj==null) return false; else if(CharSequence.class.isAssignableFrom(obj.getClass())){ return StringUtils.isNotBlank(obj.toString()); }else if(Collection.class.isAssignableFrom(obj.getClass())){ return hasElement((Collection)obj); }else if(obj.getClass().isArray()){ if(obj.getClass().getComponentType().isPrimitive()){ return Array.getLength(obj)!=0; }else { return hasElement((Object[])obj); } }else if(Map.class.isAssignableFrom(obj.getClass())){ return hasElement((Map)obj); } return true; }  static Collection<Class<?>> getBaseTypeClass(); static boolean isPrimitive(Class<?> clazz); static boolean isBaseType(Class<?> clazz); static boolean isBaseTypeObject(Object obj); static boolean isSimpleType(Class<?> clazz); static boolean isSimpleTypeObject(Object obj); static Collection<Class<?>> getSimpleClass(); static boolean isTimeClass(Class<?> clazz); static boolean isTimeObject(Object obj); static boolean isMap(Object obj); static boolean isMapClass(Class clazz); static boolean isCollectionClass(Class clazz); static boolean isIterableClass(Class clazz); static boolean isArrayClass(Class clazz); static Class getObjectClass(Object obj); static Throwable getCauseServiceException(Throwable e); static Throwable getFinalCauseException(Throwable e); static T getCauseException(Throwable e, Class<T> root); static Throwable getFirstNotJFishThrowable(Throwable e); static boolean isJFishThrowable(Throwable t); static ServiceException asServiceException(Exception e); static ServiceException asServiceException(String msg, Exception e); static void throwServiceException(Exception e); static void throwServiceException(String msg); static void throwServiceException(String msg, Exception e); static void throwBaseException(Exception e); static void throwBaseException(String msg); static void throwBaseException(String msg, Exception e); static BaseException asBaseException(Exception e); static BaseException asBaseException(String msg); static BaseException asBaseException(String msg, Throwable e); static void throwIfNull(Object value, String msg); static T block(ArgsReturnableClosure block, Object...objects); static void print(String str); static void print(boolean print, String str); static void println(String str, Object...objects); static void debug(String str, Object...objects); static void printlnNamedArgs(String str, Object...objects); static void comment(String str, Object...objects); static void comment(boolean print, String str, Object...objects); static void println(boolean print, String str, Object...objects); static String toString(String str, Object...objects); static String toStringWith(boolean print, String str, boolean named, Object...objects); static String toString(Throwable e, boolean detail); static String toString(Object obj); static String newString(byte[] bytes); static String newString(byte[] bytes, String charset); static String changeCharset(String str, String toCharset); static String changeCharset(String str, String fromCharset, String toCharset); static byte[] getBytes(String source); static byte[] getBytes(String source, String charset); static String toHex(byte[] bytes); static byte[] hex2Bytes(String str); static boolean isHexString(String str); static String append(Object... strings); static String repeatString(int count, String op); static String appendNotBlank(Object... strings); static String appendWith(boolean ignoreBlank, Object... strings); static String strings(String... strings); static int size(Object obj); static int size(Object obj, boolean reflectSizeMethod); static boolean isBlank(Object obj); static boolean isMultiple(Object obj); static boolean isMultipleAndNotEmpty(Object obj); static boolean isMultipleObjectClass(Class clazz); static boolean isArray(Object obj); static Object judgeType(Object obj, TypeJudge executor); static Object judgeType(Object obj, Class type, TypeJudge executor); static T getFirst(Object obj); static Optional<T> getFirstOptional(Object obj); static T getFirstOrCreate(Object obj, Class<T> clazz); static List<T> emptyIfNull(List<T> list); static Map<K, V> emptyIfNull(Map<K, V> map); static List defIfEmpty(List list, List def); static T notNullValue(Object obj, Class<T> type); static Map asMap(Object... params); static List<T> asList(Object array); static List<T> asList(Object array, boolean trimNull); static String getRadomString(int length); static String getRadomNumberString(int length); static void printMemory(); static void printMemory(String unit); static String statisticsMemory(String unit); static void closeIO(Closeable io); static boolean hasNotElement(Object obj); static boolean hasElement(Object obj); static boolean isEmpty(Object obj); static boolean hasElement(Object[] obj); static boolean hasElement(Map map); static boolean isEmpty(Map map); static boolean isNotEmpty(Map map); static boolean hasElement(Collection col); static boolean isEmpty(Collection col); static boolean isEmpty(Object[] arrays); static boolean isNullOrEmptyObject(Object object); static boolean isNotEmpty(Collection col); static boolean equals(Object obj1, Object obj2); static boolean equalsIgnoreCase(String str1, String str2); static BufferedReader asBufferedReader(InputStream in); static void asc(List<T> list); static void desc(List<T> list); static void asc(T[] array); static void desc(T[] array); static void await(long seconds); static void awaitInMillis(long millis); static boolean isIterable(Object object); static Iterator convertIterator(Object value); static T fetchNotNull(T...args); static void filterMap(Map map, String...patterns); static T strCastTo(String str, Class<T> toType); static T tryCastTo(Object val, Class<T> toType); static boolean isMapContainsInstance(Map<String, Object> model, Class<?> clazz); static void remove(Collection<?> collection, int start, int end); static Map<String, String> toMap(Properties prop); static Map mapListToMap(List<? extends Map> datas, String keyName, String valueName); static List<T> asListWithType(Class<T> clazz, Object...objects); static Collection<T> stripNull(Collection<T> collections); static List<T> newArrayList(); static List<T> newArrayList(int size); static List<T> newList(T...objs); static List<T> newArrayList(T...objs); static HashSet<T> newHashSet(); static Map<K, V> newMap(); static Map<K, V> newHashMap(Object... params); static Map<K, V> newHashMap(int size); static BusinessException asBusinessException(Exception e); static BusinessException asBusinessException(String msg, Exception e); static String getBaseExceptonCode(Exception e); static boolean isError(Exception e, String errorCode); static boolean matche(boolean cache, String regex, String str); static String generateToken(String... strs); static boolean isWord(String str); static Object formatValue(Object value, String dataFormat); static String format(Number num, String pattern); static String format(Number num); static String padLeft(String s, int taotalLength, String append); static String padRight(String s, int taotalLength, String append); static String pad(String s, int taotalLength, char append, boolean padLeft); static String fixedLengthString(String str, int length, String padString); static Object safeGetValue(Object elemetns, int index); static Object safeGetValue(Object elemetns, int index, Object def); static Long hexToLong(String hexStr); static String decToHexString(String decStr); static String decToHexString(String decStr, int length); static String decToRadixString(String decStr, int radix, int length); static String encodeUrl(String url); static String decodeUrl(String url); static String safeUrlEncode(String base64); static String safeUrlEncode(String base64, boolean bouncyCastle); static String safeUrldecode(String base64, boolean bouncyCastle); static boolean isDigitString(String str); static T cast(Object obj, Class<T> clazz); static Object firstNotNull(Object...objects); static T[] makeGenericArray(Class<T> cls, int length); static void lockAction(Lock lock, Runnable action); static boolean isNumberObject(Object val); static boolean isNumberType(Class<?> clazz); static boolean isIntegralType(Class<?> clazz); static Optional<String> getPid(); static String getOsName(); static boolean isWindowsOS(); static String converTo36Radix(String str); static long getCrc32(String str); static long getCrc32(byte[] bytes); static String randomUUID(); static String sensitive(String sensitive, int sensitiveIndex); static String sensitive(String sensitive, int keepPlainTextSize, String replaceStr); static String sensitiveFromRight(String sensitive, int keepPlainTextSize, String replaceStr); static String sensitiveFromLeft(String sensitive, int keepPlainTextSize, String replaceStr); static final String UTF8; static final Pattern DIGIT; static final Pattern AWORD; static final String EMPTY_STRING; static final Object EMPTY_OBJECT; static final Object[] EMPTY_ARRAY; static final String[] EMPTY_STRING_ARRAY; static final Class[] Empty_CLASSES; static final Consoler CONSOLE; static final char[] WORD_CHARS; static final Comparator AscBeanOrderComparator; static final Comparator DescBeanOrderComparator; static TypeJudge FirstObject; static TypeJudge NotNullObject; static final double MB_SIZE; static final double KB_SIZE; }
@Test public void testHasElement(){ Assert.assertTrue(LangUtils.isEmpty(Collections.EMPTY_MAP)); Assert.assertTrue(LangUtils.isEmpty(Collections.EMPTY_LIST)); Assert.assertTrue(LangUtils.isEmpty(Collections.EMPTY_SET)); Assert.assertFalse(LangUtils.hasElement(Collections.EMPTY_MAP)); Assert.assertFalse(LangUtils.hasElement(Collections.EMPTY_LIST)); Assert.assertFalse(LangUtils.hasElement(Collections.EMPTY_SET)); int[] ints = new int[]{}; Assert.assertTrue(LangUtils.isEmpty(ints)); Assert.assertFalse(LangUtils.hasElement(ints)); ints = new int[]{0}; Assert.assertTrue(LangUtils.hasElement(ints)); Object obj = new Object[]{"aaa", Long.valueOf(1L)}; Assert.assertFalse(LangUtils.isEmpty(obj)); Assert.assertTrue(LangUtils.hasElement(obj)); obj = new Object[]{null, null}; Assert.assertFalse(LangUtils.isEmpty(obj)); Assert.assertTrue(LangUtils.hasNotElement(obj)); }