ObjectUtil
大约 4 分钟
package org.groges.handler.util;
import org.groges.handler.exception.HandlerException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.Map;
/**
*对象工具类
*/
public final class ObjectUtil {
private ObjectUtil() {
}
/**
* Checks that the given argument is not null. If it is, throws {@link NullPointerException}.
* Otherwise, returns the argument.
*/
public static <T> T checkNotNull(T arg, String text) {
if (arg == null) {
throw new NullPointerException(text);
}
return arg;
}
/**
* Checks that the given argument is strictly positive. If it is not, throws {@link IllegalArgumentException}.
* Otherwise, returns the argument.
*/
public static int checkPositive(int i, String name) {
if (i <= 0) {
throw new IllegalArgumentException(name + ": " + i + " (expected: > 0)");
}
return i;
}
/**
* Checks that the given argument is strictly positive. If it is not, throws {@link IllegalArgumentException}.
* Otherwise, returns the argument.
*/
public static long checkPositive(long i, String name) {
if (i <= 0) {
throw new IllegalArgumentException(name + ": " + i + " (expected: > 0)");
}
return i;
}
/**
* Checks that the given argument is positive or zero. If it is not , throws {@link IllegalArgumentException}.
* Otherwise, returns the argument.
*/
public static int checkPositiveOrZero(int i, String name) {
if (i < 0) {
throw new IllegalArgumentException(name + ": " + i + " (expected: >= 0)");
}
return i;
}
/**
* Checks that the given argument is positive or zero. If it is not, throws {@link IllegalArgumentException}.
* Otherwise, returns the argument.
*/
public static long checkPositiveOrZero(long i, String name) {
if (i < 0) {
throw new IllegalArgumentException(name + ": " + i + " (expected: >= 0)");
}
return i;
}
/**
* Checks that the given argument is neither null nor empty.
* If it is, throws {@link NullPointerException} or {@link IllegalArgumentException}.
* Otherwise, returns the argument.
*/
public static <T> T[] checkNonEmpty(T[] array, String name) {
checkNotNull(array, name);
checkPositive(array.length, name + ".length");
return array;
}
/**
* Checks that the given argument is neither null nor empty.
* If it is, throws {@link NullPointerException} or {@link IllegalArgumentException}.
* Otherwise, returns the argument.
*/
public static <T extends Collection<?>> T checkNonEmpty(T collection, String name) {
checkNotNull(collection, name);
checkPositive(collection.size(), name + ".size");
return collection;
}
/**
* Resolves a possibly null Integer to a primitive int, using a default value.
*
* @param wrapper the wrapper
* @param defaultValue the default value
* @return the primitive value
*/
public static int intValue(Integer wrapper, int defaultValue) {
return wrapper != null ? wrapper : defaultValue;
}
/**
* Resolves a possibly null Long to a primitive long, using a default value.
*
* @param wrapper the wrapper
* @param defaultValue the default value
* @return the primitive value
*/
public static long longValue(Long wrapper, long defaultValue) {
return wrapper != null ? wrapper : defaultValue;
}
public static Byte castToByte(Object value) {
if (value == null) {
return null;
}
if (value instanceof BigDecimal) {
return byteValue((BigDecimal) value);
}
if (value instanceof Number) {
return ((Number) value).byteValue();
}
if (value instanceof String) {
String strVal = (String) value;
if (strVal.length() == 0 //
|| "null".equals(strVal) //
|| "NULL".equals(strVal)) {
return null;
}
return Byte.parseByte(strVal);
}
throw new HandlerException("can not cast to byte, value : " + value);
}
public static Short castToShort(Object value) {
if (value == null) {
return null;
}
if (value instanceof BigDecimal) {
return shortValue((BigDecimal) value);
}
if (value instanceof Number) {
return ((Number) value).shortValue();
}
if (value instanceof String) {
String strVal = (String) value;
if (strVal.length() == 0 //
|| "null".equals(strVal) //
|| "NULL".equals(strVal)) {
return null;
}
return Short.parseShort(strVal);
}
throw new HandlerException("can not cast to short, value : " + value);
}
public static BigDecimal castToBigDecimal(Object value) {
if (value == null) {
return null;
}
if (value instanceof BigDecimal) {
return (BigDecimal) value;
}
if (value instanceof BigInteger) {
return new BigDecimal((BigInteger) value);
}
String strVal = value.toString();
if (strVal.length() == 0) {
return null;
}
if (value instanceof Map && ((Map) value).size() == 0) {
return null;
}
return new BigDecimal(strVal);
}
public static BigInteger castToBigInteger(Object value) {
if (value == null) {
return null;
}
if (value instanceof BigInteger) {
return (BigInteger) value;
}
if (value instanceof Float || value instanceof Double) {
return BigInteger.valueOf(((Number) value).longValue());
}
if (value instanceof BigDecimal) {
BigDecimal decimal = (BigDecimal) value;
int scale = decimal.scale();
if (scale > -1000 && scale < 1000) {
return ((BigDecimal) value).toBigInteger();
}
}
String strVal = value.toString();
if (strVal.length() == 0 //
|| "null".equals(strVal) //
|| "NULL".equals(strVal)) {
return null;
}
return new BigInteger(strVal);
}
public static Float castToFloat(Object value) {
if (value == null) {
return null;
}
if (value instanceof Number) {
return ((Number) value).floatValue();
}
if (value instanceof String) {
String strVal = value.toString();
if (strVal.length() == 0 //
|| "null".equals(strVal) //
|| "NULL".equals(strVal)) {
return null;
}
if (strVal.indexOf(',') != 0) {
strVal = strVal.replaceAll(",", "");
}
return Float.parseFloat(strVal);
}
throw new HandlerException("can not cast to float, value : " + value);
}
public static Double castToDouble(Object value) {
if (value == null) {
return null;
}
if (value instanceof Number) {
return ((Number) value).doubleValue();
}
if (value instanceof String) {
String strVal = value.toString();
if (strVal.length() == 0 //
|| "null".equals(strVal) //
|| "NULL".equals(strVal)) {
return null;
}
if (strVal.indexOf(',') != 0) {
strVal = strVal.replaceAll(",", "");
}
return Double.parseDouble(strVal);
}
throw new HandlerException("can not cast to double, value : " + value);
}
public static Date castToDate(Object value) {
return castToDate(value, null);
}
public static Date castToDate(Object value, String format) {
if (value == null) {
return null;
}
if (value instanceof Date) { // 使用频率最高的,应优先处理
return (Date) value;
}
if (value instanceof Calendar) {
return ((Calendar) value).getTime();
}
throw new HandlerException("can not cast to date, value : " + value);
}
public static Long castToLong(Object value) {
if (value == null) {
return null;
}
if (value instanceof BigDecimal) {
return longValue((BigDecimal) value);
}
if (value instanceof Number) {
return ((Number) value).longValue();
}
if (value instanceof String) {
String strVal = (String) value;
if (strVal.length() == 0 //
|| "null".equals(strVal) //
|| "NULL".equals(strVal)) {
return null;
}
if (strVal.indexOf(',') != 0) {
strVal = strVal.replaceAll(",", "");
}
try {
return Long.parseLong(strVal);
} catch (NumberFormatException ex) {
}
}
throw new HandlerException("can not cast to long, value : " + value);
}
public static byte byteValue(BigDecimal decimal) {
if (decimal == null) {
return 0;
}
int scale = decimal.scale();
if (scale >= -100 && scale <= 100) {
return decimal.byteValue();
}
return decimal.byteValueExact();
}
public static short shortValue(BigDecimal decimal) {
if (decimal == null) {
return 0;
}
int scale = decimal.scale();
if (scale >= -100 && scale <= 100) {
return decimal.shortValue();
}
return decimal.shortValueExact();
}
public static int intValue(BigDecimal decimal) {
if (decimal == null) {
return 0;
}
int scale = decimal.scale();
if (scale >= -100 && scale <= 100) {
return decimal.intValue();
}
return decimal.intValueExact();
}
public static long longValue(BigDecimal decimal) {
if (decimal == null) {
return 0;
}
int scale = decimal.scale();
if (scale >= -100 && scale <= 100) {
return decimal.longValue();
}
return decimal.longValueExact();
}
public static Integer castToInt(Object value) {
if (value == null) {
return null;
}
if (value instanceof Integer) {
return (Integer) value;
}
if (value instanceof BigDecimal) {
return intValue((BigDecimal) value);
}
if (value instanceof Number) {
return ((Number) value).intValue();
}
if (value instanceof String) {
String strVal = (String) value;
if (strVal.length() == 0 //
|| "null".equals(strVal) //
|| "NULL".equals(strVal)) {
return null;
}
if (strVal.indexOf(',') != 0) {
strVal = strVal.replaceAll(",", "");
}
return Integer.parseInt(strVal);
}
if (value instanceof Boolean) {
return ((Boolean) value).booleanValue() ? 1 : 0;
}
throw new HandlerException("can not cast to int, value : " + value);
}
public static byte[] castToBytes(Object value) {
if (value instanceof byte[]) {
return (byte[]) value;
}
if (value instanceof String) {
return value.toString().getBytes();
}
throw new HandlerException("can not cast to byte[], value : " + value);
}
public static Boolean castToBoolean(Object value) {
if (value == null) {
return null;
}
if (value instanceof Boolean) {
return (Boolean) value;
}
if (value instanceof BigDecimal) {
return intValue((BigDecimal) value) == 1;
}
if (value instanceof Number) {
return ((Number) value).intValue() == 1;
}
if (value instanceof String) {
String strVal = (String) value;
if (strVal.length() == 0 //
|| "null".equals(strVal) //
|| "NULL".equals(strVal)) {
return null;
}
if ("true".equalsIgnoreCase(strVal) //
|| "1".equals(strVal)) {
return Boolean.TRUE;
}
if ("false".equalsIgnoreCase(strVal) //
|| "0".equals(strVal)) {
return Boolean.FALSE;
}
}
throw new HandlerException("can not cast to boolean, value : " + value);
}
}