以下代码提供了 java 开发中常用的工具函数,其中 inAnyString(), getMin(), getMax(), betWeen() 使用了泛型对象设计。
public static int compare(Object o1, Object o2) {
return compare(o1, o2, true);
}
public static int compare(Object o1, Object o2, boolean ignoreNullAndEmpty) {
if (o1 == null && o2 == null) {
return 0;
} else if (o1 == null && o2 != null) {
return ignoreNullAndEmpty && "".equals(o2) ? 0 : -1;
} else if (o1 != null && o2 == null) {
return ignoreNullAndEmpty && "".equals(o1) ? 0 : 1;
} else if (o1.getClass() == o2.getClass() && o1 instanceof Comparable) {
return ((Comparable)o1).compareTo(o2);
} else if (Number.class.isAssignableFrom(o1.getClass())) {
BigDecimal b1 = Convert.toBigDecimal(o1, (BigDecimal)null);
BigDecimal b2 = Convert.toBigDecimal(o2, (BigDecimal)null);
return b1.compareTo(b2);
} else if (BigDecimal.class.isAssignableFrom(o1.getClass())) {
return ((BigDecimal)o1).compareTo((BigDecimal)o2);
} else if (Date.class.isAssignableFrom(o1.getClass())) {
return ((Date)o1).compareTo((Date)o2);
} else if (java.sql.Date.class.isAssignableFrom(o1.getClass())) {
return ((java.sql.Date)o1).compareTo((java.sql.Date)o2);
} else if (Time.class.isAssignableFrom(o1.getClass())) {
return ((Time)o1).compareTo((Time)o2);
} else if (Timestamp.class.isAssignableFrom(o1.getClass())) {
return ((Timestamp)o1).compareTo((Timestamp)o2);
} else if (Short.class.isAssignableFrom(o1.getClass())) {
return ((Short)o1).compareTo((Short)o2);
} else if (Integer.class.isAssignableFrom(o1.getClass())) {
return ((Integer)o1).compareTo((Integer)o2);
} else if (Long.class.isAssignableFrom(o1.getClass())) {
return ((Long)o1).compareTo((Long)o2);
} else if (Float.class.isAssignableFrom(o1.getClass())) {
return ((Float)o1).compareTo((Float)o2);
} else if (Double.class.isAssignableFrom(o1.getClass())) {
return ((Double)o1).compareTo((Double)o2);
} else {
return ByRef.class.isAssignableFrom(o1.getClass()) ? compare(((ByRef)o1).value, ((ByRef)o2).value) : String.valueOf(o1).compareTo(String.valueOf(o2));
}
}
// 判断输入数字 大于零
public static boolean isGtZero(BigDecimal d) {
return compare(toAmount(d), BigDecimal.ZERO) > 0;
}
// 判断输入数字 大于等于零
public static boolean isGeZero(BigDecimal d) {
return compare(toAmount(d), BigDecimal.ZERO) >= 0;
}
// 判断输入数字 小于零
public static boolean isLtZero(BigDecimal d) {
return compare(toAmount(d), BigDecimal.ZERO) < 0;
}
// 判断输入数字 小于等于零
public static boolean isLeZero(BigDecimal d) {
return compare(toAmount(d), BigDecimal.ZERO) <= 0;
}
// 判断输入数字 是零金额数字
public static boolean chkZeroAmt(BigDecimal d) {
return compare(toAmount(d).abs(), BigDecimal.ZERO) == 0;
}
// 判断输入数字 为非零金额
public static boolean chkNotZero(BigDecimal d) {
return compare(toAmount(d).abs(), BigDecimal.ZERO) != 0;
}
public static boolean anyIsZero(BigDecimal... cols) {
for (BigDecimal d : cols) {
if (chkZeroAmt(d)) return true;
}
return false;
}
public static boolean anyGtZero(BigDecimal... cols) {
for (BigDecimal d : cols) {
if (isGtZero(d)) return true;
}
return false;
}
public static boolean anyNotZero(BigDecimal... cols) {
for (BigDecimal d : cols) {
if (chkNotZero(d)) return true;
}
return false;
}
public static <T> T getMin(T... cols) {
T ret = null;
for (T d : cols) {
if (null == d) continue;
if (null == ret) ret = d;
if (compare(d, ret) < 0) ret = d;
}
return ret;
}
public static <T> T getMax(T... cols) {
T ret = null;
for (T d : cols) {
if (null == d) continue;
if (ret == null) ret = d;
if (compare(d, ret) > 0) ret = d;
}
return ret;
}
public static boolean inAnyString(String v, String... chks) {
if (null == v) return false;
for (String s : chks) {
if (compare(v, s) == 0) return true;
}
return false;
}
/**
* 比较 v 在 o1 -- o2 之间
*
* @param v 待检查变量
* @param beg 下区间
* @param end 上区间
* @return 在区间中
*/
public static <T> boolean betWeen(T v, Object beg, Object end) {
if ((!v.getClass().isAssignableFrom(beg.getClass())) || (!v.getClass().isAssignableFrom(end.getClass()))) {
throw new IllegalArgumentException("Between函数参数类型必须相同!");
}
if (anyIsNull(v, beg, end)) return false;
return compare(v, beg) >= 0 && compare(v, end) <= 0;
}