/** * Copyright (c) 2015-2016, Chill Zhuang 庄骞 (smallchill@163.com). *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *

* http://www.apache.org/licenses/LICENSE-2.0 *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.nis.util; import cn.hutool.log.Log; import java.lang.reflect.Array; import java.util.*; /** * 高频方法集合类 */ public class ToolUtil { public final static Log logger = Log.get(); /** * 字符连接符 */ private static final char SEPARATOR = '_'; /** * 下划线命名 转 驼峰命名 首字母小写 * * @param s the s * @return String toCamelCase("hello_world") == "helloWorld" toCapitalizeCamelCase("hello_world") == "HelloWorld" toUnderScoreCase("helloWorld") = "hello_world" */ public static String toCamelCase(String s) { if (s == null) { return null; } String ls = s.toLowerCase(); StringBuilder sb = new StringBuilder(ls.length()); boolean upperCase = false; for (int i = 0; i < ls.length(); i++) { char c = ls.charAt(i); if (c == SEPARATOR) { upperCase = true; } else if (upperCase) { sb.append(Character.toUpperCase(c)); upperCase = false; } else { sb.append(c); } } return sb.toString(); } /** * 比较两个对象是否相等。
* 相同的条件有两个,满足其一即可:
* 1. obj1 == null && obj2 == null; 2. obj1.equals(obj2) * * @param obj1 对象1 * @param obj2 对象2 * @return 是否相等 */ public static boolean equals(Object obj1, Object obj2) { return (obj1 != null) ? (obj1.equals(obj2)) : (obj2 == null); } /** * 计算对象长度,如果是字符串调用其length函数,集合类调用其size函数,数组调用其length属性,其他可遍历对象遍历计算长度 * * @param obj 被计算长度的对象 * @return 长度 */ public static int length(Object obj) { if (obj == null) { return 0; } if (obj instanceof CharSequence) { return ((CharSequence) obj).length(); } if (obj instanceof Collection) { return ((Collection) obj).size(); } if (obj instanceof Map) { return ((Map) obj).size(); } int count; if (obj instanceof Iterator) { Iterator iter = (Iterator) obj; count = 0; while (iter.hasNext()) { count++; iter.next(); } return count; } if (obj instanceof Enumeration) { Enumeration enumeration = (Enumeration) obj; count = 0; while (enumeration.hasMoreElements()) { count++; enumeration.nextElement(); } return count; } if (obj.getClass().isArray() == true) { return Array.getLength(obj); } return -1; } /** * 对象中是否包含元素 * * @param obj 对象 * @param element 元素 * @return 是否包含 */ public static boolean contains(Object obj, Object element) { if (obj == null) { return false; } if (obj instanceof String) { if (element == null) { return false; } return ((String) obj).contains(element.toString()); } if (obj instanceof Collection) { return ((Collection) obj).contains(element); } if (obj instanceof Map) { return ((Map) obj).values().contains(element); } if (obj instanceof Iterator) { Iterator iter = (Iterator) obj; while (iter.hasNext()) { Object o = iter.next(); if (equals(o, element)) { return true; } } return false; } if (obj instanceof Enumeration) { Enumeration enumeration = (Enumeration) obj; while (enumeration.hasMoreElements()) { Object o = enumeration.nextElement(); if (equals(o, element)) { return true; } } return false; } if (obj.getClass().isArray() == true) { int len = Array.getLength(obj); for (int i = 0; i < len; i++) { Object o = Array.get(obj, i); if (equals(o, element)) { return true; } } } return false; } /** * 对象是否不为空(新增) * * @param o String,List,Map,Object[],int[],long[] * @return */ public static boolean isNotEmpty(Object o) { return !isEmpty(o); } /** * 对象是否为空 * * @param o String,List,Map,Object[],int[],long[] * @return */ @SuppressWarnings("rawtypes") public static boolean isEmpty(Object o) { if (o == null) { return true; } if (o instanceof String) { if (o.toString().trim().equals("")) { return true; } } else if (o instanceof List) { if (((List) o).size() == 0) { return true; } } else if (o instanceof Map) { if (((Map) o).size() == 0) { return true; } } else if (o instanceof Set) { if (((Set) o).size() == 0) { return true; } } else if (o instanceof Object[]) { if (((Object[]) o).length == 0) { return true; } } else if (o instanceof int[]) { if (((int[]) o).length == 0) { return true; } } else if (o instanceof long[]) { if (((long[]) o).length == 0) { return true; } } return false; } /** * 对象组都不是empty */ public static boolean isNotAllEmpty(Object... obj){ for (Object o:obj){ if (isEmpty(o)){ return false; } } return true; } /** * 如果为空, 则调用默认值 * * @param str * @return */ public static Object getValue(Object str, Object defaultValue) { if (isEmpty(str)) { return defaultValue; } return str; } /** * 强转->string,并去掉多余空格 * * @param str * @return */ public static String toStr(Object str) { return toStr(str, ""); } /** * 强转->string,并去掉多余空格 * * @param str * @param defaultValue * @return */ public static String toStr(Object str, String defaultValue) { if (null == str) { return defaultValue; } return str.toString().trim(); } /** * JSON字符串特殊字符处理 * * @param s * @return String */ public static String string2Json(String s) { if (isEmpty(s)) { return ""; } StringBuffer sb = new StringBuffer(); for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); switch (c) { case '\"': sb.append("\\\""); break; case '\\': sb.append("\\\\"); break; case '/': sb.append("\\/"); break; case '\b': sb.append("\\b"); break; case '\f': sb.append("\\f"); break; case '\n': sb.append("\\n"); break; case '\r': sb.append("\\r"); break; case '\t': sb.append("\\t"); break; default: sb.append(c); } } return sb.toString(); } }