通用工具类,包含java常见变量类型判空,字符格式转换(驼峰和大小写)等,比较杂,都是常用的。
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import com.alibaba.fastjson.JSONArray;
public class AegisCommonUtils {
/**
* @name 判空
*/
@SuppressWarnings("rawtypes")
public static boolean isNull(Object obj) {
boolean isNullFlag = true;
if (obj != null) {
if (obj instanceof List<?>) {
isNullFlag = isNull((List<?>) obj);
} else if (obj instanceof Set<?>) {
isNullFlag = isNull((Set<?>) obj);
} else if (obj instanceof Object[]) {
isNullFlag = isNull((Object[]) obj);
} else if (obj instanceof Map) {
isNullFlag = isNull((Map) obj);
} else if (obj instanceof String) {
isNullFlag = isNull((String) obj);
} else if (obj instanceof Integer) {
isNullFlag = isNull((Integer) obj);
} else if (obj instanceof JSONArray) {
isNullFlag = isNull((JSONArray) obj);
} else if (obj instanceof Boolean) {
isNullFlag = isNull((Boolean) obj);
} else if (obj instanceof StringBuffer) {
isNullFlag = isNull((StringBuffer) obj);
} else {
isNullFlag = false;
}
}
return isNullFlag;
}
/**
* 判断列表是否为空
*/
public static boolean isNull(List<?> list) {
return list == null || list.size() == 0 || list.get(0) == null;
}
/**
* 判断列表是否为空
*/
public static boolean isNull(Set<?> set) {
return set == null || set.size() == 0;
}
/**
* @name 判断数组是否为空
*/
public static boolean isNull(Object[] objects) {
return objects == null || objects.length == 0;
}
/**
* @name 判断Map是否为空
*/
public static boolean isNull(Map<?, ?> map) {
return map == null || map.isEmpty();
}
/**
* 判断字符串是否为空
*/
public static boolean isNull(String str) {
return str == null || "".equals(str.trim()) || "null".equalsIgnoreCase(str.trim())
|| "undefined".equalsIgnoreCase(str.trim());
}
/**
* 判断整数是否为空
*/
public static boolean isNull(Integer integer) {
return integer == null;
}
/**
* 判断jsonArray是否为空
*/
public static boolean isNull(JSONArray jsonarray) {
return jsonarray == null || "".equals(jsonarray.toString()) || jsonarray.size() == 0;
}
/**
* 判断Boolean是否为空
*/
public static boolean isNull(Boolean bol) {
return bol == null;
}
/**
* 判断jsonArray是否为空
*/
public static boolean isNull(StringBuffer buffer) {
return buffer == null || buffer.length() == 0;
}
/**
* @name 驼峰转大写下划线
*/
public static String getUpUnderLineStrByHumpStr(String str) {
if (str == null || "".equals(str.trim())) {
return "";
}
final int len = str.length();
final StringBuilder buffer = new StringBuilder();
buffer.append(Character.toUpperCase(str.charAt(0)));
for (int i = 1; i < len; i++) {
final char c = str.charAt(i);
final char cBefore = str.charAt(i - 1);
if (Character.isUpperCase(c) || (Character.isDigit(c) && Character.isLetter(cBefore))) {
buffer.append('_');
buffer.append(c);
} else {
buffer.append(Character.toUpperCase(c));
}
}
return buffer.toString();
}
/**
* @name 含下划线的字符串转驼峰字符串
*/
public static String getHumpStrByUpUnderLineStr(String str) {
String tmp = str.toLowerCase();
int ind = tmp.indexOf("_");
while (ind >= 0) {
tmp = tmp.substring(0, ind) + tmp.substring(ind + 1, ind + 2).toUpperCase() + tmp.substring(ind + 2);
ind = tmp.indexOf("_");
}
return tmp;
}
/**
* @name 将Map里面的key是大写加下划线格式转换成key为驼峰格式的Map
*/
public static Map<String, Object> getHumpMapByUpUnderLineMap(Map<String, Object> map) throws Exception {
final Map<String, Object> resMap = new HashMap<String, Object>();
if (!isNull(map)) {
final Iterator<String> keyIt = map.keySet().iterator();
while (keyIt.hasNext()) {
String key = keyIt.next();
final Object value = (Object) map.get(key);
key = getHumpStrByUpUnderLineStr(key);
resMap.put(key, value);
}
}
return resMap;
}
/**
* @name List<Map < String, OBject>中的key转换驼峰
*/
public static List<Map<String, Object>> getHumpListByUpUnderLineList(List<Map<String, Object>> list)
throws Exception {
final List<Map<String, Object>> resList = new ArrayList<Map<String, Object>>();
if (!isNull(list)) {
for (Map<String, Object> map : list) {
final Map<String, Object> resMap = getHumpMapByUpUnderLineMap(map);
resList.add(resMap);
}
}
return resList;
}
/**
* @name Map<String, Object>里面Key全是驼峰字段的转换成大写下划线的key
*/
public static Map<String, Object> getUpUnderLineMapByHumpMap(Map<String, Object> map) throws Exception {
final Map<String, Object> resMap = new HashMap<String, Object>();
if (!isNull(map)) {
final Iterator<String> keyIt = map.keySet().iterator();
while (keyIt.hasNext()) {
String key = keyIt.next();
final Object value = map.get(key);
key = getUpUnderLineStrByHumpStr(key);
if (!isNull(value)) {
resMap.put(key, value.toString());
} else {
resMap.put(key, "");
}
}
}
return resMap;
}
/**
* @name List<Map<String,Object>里面的KEy 驼峰转大写下划线
*/
public static List<Map<String, Object>> getUpUnderLineListByHumpList(List<Map<String, Object>> list)
throws Exception {
final List<Map<String, Object>> resList = new ArrayList<Map<String, Object>>();
if (!isNull(list)) {
for (Map<String, Object> map : list) {
final Map<String, Object> resMap = getUpUnderLineMapByHumpMap(map);
resList.add(resMap);
}
}
return resList;
}
/**
* @name 获取UUID
*/
public static String generateRandomString() throws Exception {
return UUID.randomUUID().toString().replace("-", "");
}
public static String getHostIp() {
String sIP = "";
InetAddress ip = null;
try {
boolean bFindIP = false;
final Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
if (bFindIP) {
break;
}
final NetworkInterface ni = netInterfaces.nextElement();
final Enumeration<InetAddress> ips = ni.getInetAddresses();
while (ips.hasMoreElements()) {
ip = ips.nextElement();
if (!ip.isLoopbackAddress() && ip.getHostAddress().matches("(\\d{1,3}\\.){3}\\d{1,3}")) {
bFindIP = true;
break;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
if (null != ip) {
sIP = ip.getHostAddress();
}
return sIP;
}
/**
* List按照关键Key对Map分组
*/
public static Map<String, List<Map<String, Object>>> getMapByListWithKey(List<Map<String, Object>> list, String key)
throws Exception {
final Map<String, List<Map<String, Object>>> resMap = new HashMap<String, List<Map<String, Object>>>();
if (!AegisCommonUtils.isNull(list)) {
for (Map<String, Object> map : list) {
String value = (String) map.get(key);
if (AegisCommonUtils.isNull(value)) {
value = "null";
}
if (AegisCommonUtils.isNull(resMap.get(value))) {
final List<Map<String, Object>> resList = new ArrayList<Map<String, Object>>();
resList.add(map);
resMap.put(value, resList);
} else {
final List<Map<String, Object>> resList = resMap.get(value);
resList.add(map);
resMap.put(value, resList);
}
}
}
return resMap;
}
@SuppressWarnings("rawtypes")
public static String getLocalHostLANAddress() {
try {
InetAddress candidateAddress = null;
// 遍历所有的网络接口
for (final Enumeration ifaces = NetworkInterface.getNetworkInterfaces(); ifaces.hasMoreElements();) {
final NetworkInterface iface = (NetworkInterface) ifaces.nextElement();
// 在所有的接口下再遍历IP
for (final Enumeration inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) {
final InetAddress inetAddr = (InetAddress) inetAddrs.nextElement();
if (!inetAddr.isLoopbackAddress()) { // 排除loopback类型地址
if (inetAddr.isSiteLocalAddress()) {
// 如果是site-local地址,就是它了
final String ip = inetAddr.toString().split("/")[1];
return ip;
} else if (candidateAddress == null) {
// site-local类型的地址未被发现,先记录候选地址
candidateAddress = inetAddr;
}
}
}
}
if (candidateAddress != null) {
final String ip = candidateAddress.toString().split("/")[1];
return ip;
}
// 如果没有发现 non-loopback地址.只能用最次选的方案
final InetAddress jdkSuppliedAddress = InetAddress.getLocalHost();
final String ip = jdkSuppliedAddress.toString().split("/")[1];
return ip;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@SuppressWarnings("rawtypes")
public static Map<String, String> getHeadersInfo(HttpServletRequest req) {
final Map<String, String> map = new HashMap<String, String>();
final Enumeration headerNames = req.getHeaderNames();
while (headerNames.hasMoreElements()) {
final String key = (String) headerNames.nextElement();
final String value = req.getHeader(key);
map.put(key, value);
}
return map;
}
/**
* @name list转树
*/
@SuppressWarnings("unchecked")
public static List<Map<String, Object>> listToTree(List<Map<String, Object>> tree, String k, String pk) {
final Map<String, Object> nodeMap = new HashMap<String, Object>();
for (int i = 0; i < tree.size(); i++) {
final Map<String, Object> node = tree.get(i);
final Map<String, Object> map = new HashMap<String, Object>();
map.put("isRoot", true);
map.put("node", node);
nodeMap.put((String) node.get(k), map);
}
for (String key : nodeMap.keySet()) {
final Map<String, Object> map = (Map<String, Object>) nodeMap.get(key);
final Map<String, Object> node = (Map<String, Object>) map.get("node");
final String pcode = (String) node.get(pk);
if (!AegisCommonUtils.isNull(pcode) && !"-1".equals(pcode)) {
final Map<String, Object> pMap = (Map<String, Object>) nodeMap.get(pcode);
if (!AegisCommonUtils.isNull(pMap)) {
final Map<String, Object> pnode = (Map<String, Object>) pMap.get("node");
List<Map<String, Object>> children = (List<Map<String, Object>>) pnode.get("children");
if (AegisCommonUtils.isNull(children)) {
children = new ArrayList<Map<String, Object>>();
pnode.put("children", children);
}
children.add(node);
}
map.put("isRoot", false); // 如果没有找到父节点,当前节点也标记为非根节点,排除结果树之外
}
}
final List<Map<String, Object>> rootList = new ArrayList<Map<String, Object>>();
for (Map<String, Object> node : tree) {
final Map<String, Object> map = (Map<String, Object>) nodeMap.get(node.get(k));
final boolean isRoot = (boolean) ((Map<String, Object>) map).get("isRoot");
if (isRoot) {
final Map<String, Object> rootNode = (Map<String, Object>) map.get("node");
rootList.add(rootNode);
}
}
Collections.sort(rootList, new Comparator<Map<String, Object>>() {
@Override
public int compare(Map<String, Object> o1, Map<String, Object> o2) {
return getOrder(o1, o2);
}
});
for (Map<String, Object> node : rootList) {
final List<Map<String, Object>> children = (List<Map<String, Object>>) node.get("children");
if (!AegisCommonUtils.isNull(children)) {
for (Map<String, Object> child : children) {
final List<Map<String, Object>> c = (List<Map<String, Object>>) child.get("children");
if (!AegisCommonUtils.isNull(c)) {
Collections.sort(c, new Comparator<Map<String, Object>>() {
@Override
public int compare(Map<String, Object> o1, Map<String, Object> o2) {
return getOrder(o1, o2);
}
});
}
}
}
if (!AegisCommonUtils.isNull(children)) {
Collections.sort(children, new Comparator<Map<String, Object>>() {
@Override
public int compare(Map<String, Object> o1, Map<String, Object> o2) {
return getOrder(o1, o2);
}
});
}
}
return rootList;
}
/**
* @name list转树
*/
@SuppressWarnings("unchecked")
public static List<Map<String, Object>> orgListToTree(List<Map<String, Object>> tree, String k, String pk) {
final Map<String, Object> nodeMap = new HashMap<String, Object>();
for (int i = 0; i < tree.size(); i++) {
final Map<String, Object> node = tree.get(i);
final Map<String, Object> map = new HashMap<String, Object>();
map.put("isRoot", true);
map.put("node", node);
nodeMap.put((String) node.get(k), map);
}
for (String key : nodeMap.keySet()) {
final Map<String, Object> map = (Map<String, Object>) nodeMap.get(key);
final Map<String, Object> node = (Map<String, Object>) map.get("node");
final String pcode = (String) node.get(pk);
if (!AegisCommonUtils.isNull(pcode) && !"-1".equals(pcode)) {
final Map<String, Object> pMap = (Map<String, Object>) nodeMap.get(pcode);
if (!AegisCommonUtils.isNull(pMap)) {
final Map<String, Object> pnode = (Map<String, Object>) pMap.get("node");
List<Map<String, Object>> children = (List<Map<String, Object>>) pnode.get("children");
if (AegisCommonUtils.isNull(children)) {
children = new ArrayList<Map<String, Object>>();
pnode.put("children", children);
}
children.add(node);
}
map.put("isRoot", false); // 如果没有找到父节点,当前节点也标记为非根节点,排除结果树之外
}
}
final List<Map<String, Object>> rootList = new ArrayList<Map<String, Object>>();
for (Map<String, Object> node : tree) {
final Map<String, Object> map = (Map<String, Object>) nodeMap.get(node.get(k));
final boolean isRoot = (boolean) ((Map<String, Object>) map).get("isRoot");
if (isRoot) {
final Map<String, Object> rootNode = (Map<String, Object>) map.get("node");
rootList.add(rootNode);
}
}
Collections.sort(rootList, new Comparator<Map<String, Object>>() {
@Override
public int compare(Map<String, Object> o1, Map<String, Object> o2) {
return getOrder(o1, o2);
}
});
for (Map<String, Object> node : rootList) {
final List<Map<String, Object>> children = (List<Map<String, Object>>) node.get("children");
if (!AegisCommonUtils.isNull(children)) {
for (Map<String, Object> child : children) {
final List<Map<String, Object>> c = (List<Map<String, Object>>) child.get("children");
if (!AegisCommonUtils.isNull(c)) {
Collections.sort(c, new Comparator<Map<String, Object>>() {
@Override
public int compare(Map<String, Object> o1, Map<String, Object> o2) {
return getOrder(o1, o2);
}
});
}
}
}
if (!AegisCommonUtils.isNull(children)) {
Collections.sort(children, new Comparator<Map<String, Object>>() {
@Override
public int compare(Map<String, Object> o1, Map<String, Object> o2) {
return getOrder(o1, o2);
}
});
}
}
return rootList;
}
public static void getSelectByListAndCodeCaption(List<Map<String, Object>> list, String code, String caption)
throws Exception {
if (!AegisCommonUtils.isNull(list)) {
for (Map<String, Object> map : list) {
map.put("code", map.get(code));
map.put("caption", map.get(caption));
}
}
}
/**
* 排序
*/
private static int getOrder(Map<String, Object> o1, Map<String, Object> o2) {
Integer xsxh1 = 1;
Integer xsxh2 = 1;
final Object ordernum1 = o1.get("orderNum");
if (!AegisCommonUtils.isNull(ordernum1)) {
if (ordernum1 instanceof String) {
xsxh1 = Integer.valueOf((String) ordernum1);
} else {
xsxh1 = (Integer) ordernum1;
}
}
final Object ordernum2 = o2.get("orderNum");
if (!AegisCommonUtils.isNull(ordernum2)) {
if (ordernum2 instanceof String) {
xsxh2 = Integer.valueOf((String) ordernum2);
} else {
xsxh2 = (Integer) ordernum2;
}
}
return xsxh1.compareTo(xsxh2);
}
/**
* @name 获取ip
*/
public static String getUserIpAddress(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_CLIENT_IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
}