📄 calcs.java
字号:
if (obj instanceof Integer) return new Integer(- ((Integer)obj).intValue()); else if (obj instanceof Long) return new Long(- ((Long)obj).longValue()); else if (obj instanceof Double) return new Double(- ((Double)obj).doubleValue()); else if (obj instanceof Date) return new Long(- ((Date)obj).getTime()); else //BigDecimal return ((BigDecimal)obj).negate(); }*/ /** Plus two objects. If one of them is not an numeric object, * {@link #catenate} is called instead. * * <p>When adding a big decimal with a double, the scale will be * that of big decimal -- not maximal of them. * * <p>Note: Date - Long is Date, Date - Date is Long, Long + Date is Long. * * <p>The returned result is either Integer, Long, Double or BigDecimal. *//* public static final Object plus(Object o1, Object o2) { return plus0(valueOf(o1), valueOf(o2)); }*/ /** Plus two objects. If one of them is not an numeric object, * {@link #catenate} is called instead. * * <p>When adding a big decimal with a double, the scale will be * that of big decimal -- not maximal of them. * * <p>Note: Date - Long is Date, Date - Date is Long, Long + Date is Long. * * <p>The returned result is either Integer, Long, Double or BigDecimal. *//* public static final Object plusOrCatenate(Object o1, Object o2) { if ((o1 instanceof String) || (o2 instanceof String)) return catenate(o1, o2); try { final Object temp = valueOf(o1); o2 = valueOf(o2); o1 = temp; } catch (NumberFormatException ex) { return catenate(o1, o2); } return plus0(o1, o2); }*/ /** A common utility of plus. *//* private static final Object plus0(Object o1, Object o2) { o1 = promote(o1, o2); o2 = promote(o2, o1); if (o1 instanceof Integer) { return new Integer( ((Integer)o1).intValue() + ((Integer)o2).intValue()); } else if (o1 instanceof Long) { if (o2 instanceof Date) return new Long( ((Long)o1).longValue() + ((Date)o2).getTime()); else return new Long( ((Long)o1).longValue() + ((Long)o2).longValue()); } else if (o1 instanceof BigDecimal) { return ((BigDecimal)o1).add((BigDecimal)o2); } else if (o1 instanceof Double) { return new Double( ((Double)o1).doubleValue() + ((Double)o2).doubleValue()); } else {//Date if (o2 instanceof Date) //Date + Date return new Long(((Date)o1).getTime() + ((Date)o2).getTime()); else return new Date(((Date)o1).getTime() + ((Long)o2).longValue()); } }*/ /** Subtracts two objects. * * <p>It accepts Boolean, Byte, Character, Short, Integer, * Long, Float, Double, BigDecimal, and String. * All other types will be converted to String first. * Then, converted to either Integer, Long, Double or BigDecimal, * depending its format. * * <p>Note: Date - Long is Date, Date - Date is Long, Long - Date is Long. * * <p>The returned result is either Integer, Long, Double or BigDecimal. *//* public static final Object minus(Object o1, Object o2) { o1 = valueOf(o1); o2 = valueOf(o2); o1 = promote(o1, o2); o2 = promote(o2, o1); if (o1 instanceof Integer) { return new Integer( ((Integer)o1).intValue() - ((Integer)o2).intValue()); } else if (o1 instanceof Long) { if (o2 instanceof Date) return new Long( ((Long)o1).longValue() - ((Date)o2).getTime()); else return new Long( ((Long)o1).longValue() - ((Long)o2).longValue()); } else if (o1 instanceof BigDecimal) { return ((BigDecimal)o1).subtract((BigDecimal)o2); } else if (o1 instanceof Double) { return new Double( ((Double)o1).doubleValue() - ((Double)o2).doubleValue()); } else {//Date if (o2 instanceof Date) //Date - Date return new Long(((Date)o1).getTime() - ((Date)o2).getTime()); else return new Date(((Date)o1).getTime() - ((Long)o2).longValue()); } }*/ /** Multiplies two objects. * * <p>It accepts Boolean, Byte, Character, Short, Integer, * Long, Float, Double, BigDecimal, and String. * All other types will be converted to String first. * Then, converted to either Integer, Long, Double or BigDecimal, * depending its format. * * <p>The returned result is either Integer, Long, Double or BigDecimal. * * <p>Note: Date * Long, Date * Date, Long * Date are all Long. *//* public static final Object times(Object o1, Object o2) { o1 = valueOf(o1); o2 = valueOf(o2); o1 = promote(o1, o2); o2 = promote(o2, o1); if (o1 instanceof Integer) { return new Integer( ((Integer)o1).intValue() * ((Integer)o2).intValue()); } else if (o1 instanceof Long) { if (o2 instanceof Date) return new Long( ((Long)o1).longValue() * ((Date)o2).getTime()); else return new Long( ((Long)o1).longValue() * ((Long)o2).longValue()); } else if (o1 instanceof BigDecimal) { return ((BigDecimal)o1).multiply((BigDecimal)o2); } else if (o1 instanceof Double) { return new Double( ((Double)o1).doubleValue() * ((Double)o2).doubleValue()); } else {//Date if (o2 instanceof Date) //Date * Date return new Long(((Date)o1).getTime() * ((Date)o2).getTime()); else return new Long(((Date)o1).getTime() * ((Long)o2).longValue()); } }*/ /** Divides two objects. * * <p>It accepts Boolean, Byte, Character, Short, Integer, * Long, Float, Double, BigDecimal, and String. * All other types will be converted to String first. * Then, converted to either Integer, Long, Double or BigDecimal, * depending its format. * * <p>Note: Date * Long, Date * Date, Long * Date are all Long. * * @param roundingMode the rounding mode; used only if at least one * operand is BigDecimal *//* public static final Object divide(Object o1, Object o2, int roundingMode) { o1 = valueOf(o1); o2 = valueOf(o2); o1 = promote(o1, o2); o2 = promote(o2, o1); if (o1 instanceof Integer) { return new Integer( ((Integer)o1).intValue() / ((Integer)o2).intValue()); } else if (o1 instanceof Long) { if (o2 instanceof Date) return new Long( ((Long)o1).longValue() / ((Date)o2).getTime()); else return new Long( ((Long)o1).longValue() / ((Long)o2).longValue()); } else if (o1 instanceof BigDecimal) { return ((BigDecimal)o1).divide((BigDecimal)o2, BigDecimals.FINE_NUMBER_SCALE, roundingMode); } else if (o1 instanceof Double) { return new Double( ((Double)o1).doubleValue() / ((Double)o2).doubleValue()); } else {//Date if (o2 instanceof Date) //Date / Date return new Long(((Date)o1).getTime() / ((Date)o2).getTime()); else return new Long(((Date)o1).getTime() / ((Long)o2).longValue()); } }*/ /** Divides two objects with the default rounding mode, * {@link BigDecimals#getRoundingMode}. *//* public static final Object divide(Object o1, Object o2) { return divide(o1, o2, BigDecimals.getRoundingMode()); }*/ /** Remainder (modulus) two objects. * * <p>It accepts Boolean, Byte, Character, Short, Integer, * Long, Float, Double, BigDecimal, and String. * All other types will be converted to String first. * Then, converted to either Integer, Long, Double or BigDecimal, * depending its format. * * <p>Note: Date % Long, Date % Date, Long % Date, Double % Double are * all Integer. *//* public static final Object remainder(Object o1, Object o2) { o1 = valueOf(o1); o2 = valueOf(o2); o1 = promote(o1, o2); o2 = promote(o2, o1); if (o1 instanceof Integer) { return new Integer( ((Integer)o1).intValue() % ((Integer)o2).intValue()); } else if (o1 instanceof Long) { if (o2 instanceof Date) return new Long( ((Long)o1).longValue() % ((Date)o2).getTime()); else return new Long( ((Long)o1).longValue() % ((Long)o2).longValue()); } else if (o1 instanceof BigDecimal) { return ((BigDecimal)o1).toBigInteger() .remainder(((BigDecimal)o2).toBigInteger()); } else if (o1 instanceof Double) { return new Integer( (int)(((Double)o1).longValue() % ((Double)o2).longValue())); } else {//Date if (o2 instanceof Date) //Date % Date return new Integer( (int)(((Date)o1).getTime() % ((Date)o2).getTime())); else return new Integer( (int)(((Date)o1).getTime() % ((Long)o2).longValue())); } }*//* private static final int compare(long o1, long o2) { return o1 > o2 ? 1: o1 == o2 ? 0: -1; }*/ /** Compares two objects. * * <ol> * <li>If both are the same class implementing Comparable, * Comparable.compareTo() is used. Example, String and Date.</li> * <li>Otherwise, if they are both Number, they are 'promoted' to the same * class and compare them.</li> * <li>If one is null and the other is a number, null is considered as 0.</li> * <li>If one is null and the other is a string, null is considered as "".</li> * <li>If both null, 0 is returned.</li> * <li>If one of them is number, the other is tried to convert to a number. * If failed to convert, an exception is thrown.</li> * <li>If neither a number nor the same class, an exception is thrown.</li> * </ol> * * <p>Note: {@link #equals}: null != 0 and null != "", while * null >= 0. * * <p>Reason to promotes null to 0 for compare with a number: * it is more like arithmetic operator than others. * * @param ignoreCase true to ignore case when one or two operands * are string * @return 1 if o1>o2; 0 if o1=o2; -1 if o1<o2 *//* public static final int compare(Object o1, Object o2, boolean ignoreCase) { if (o1 == Objects.UNKNOWN) o1 = null; if (o2 == Objects.UNKNOWN) o2 = null; if (o1 == null && o2 == null) return 0; if (o1 == null) o1 = nullForCompare(o2); if (o2 == null) o2 = nullForCompare(o1); //first, use Comaparable if same class (including String, Number) if ((o1 instanceof Comparable) && o2 != null && o1.getClass().equals(o2.getClass())) { if (ignoreCase && (o1 instanceof String)) return ((String)o1).compareToIgnoreCase((String)o2); return ((Comparable)o1).compareTo(o2); } //now: o1.class != o2.class or o1 is not comparable if (!(o1 instanceof Number) && !(o2 instanceof Number)) throw new ClassCastException("Unable to compare two different objects (unless one of them is number): o1="+o1+" o2="+o2); o1 = valueOf(o1); o2 = valueOf(o2); o1 = promote(o1, o2); o2 = promote(o2, o1); //assert D.OFF || o1.getClass().equals(o2.getClass()); return ((Comparable)o1).compareTo(o2); }*/ /** Returns the object representing null based on the specified object. *//* private static Object nullForCompare(Object ref) { if (ref instanceof Number) return new Integer(0); if (ref instanceof String) return ""; return null; }*/ /** Compares two objects. A shortcut of compare(o1, o2, false). *//* public static final int compare(Object o1, Object o2) { return compare(o1, o2, false); }*/ /** Compares two objects, igoring case if both of them are string. * A shortcut of compare(o1, o2, true). *//* public static final int compareIgnoreCase(Object o1, Object o2) { return compare(o1, o2, true); }*/ /** Tests whether two objects equal. * * <ol> * <li>Object.equals is used first.</li> * <li>If false but they are both Number, they are 'promoted' to the same * class and compare again.</li> * <li>If one of them is null, false is returned.<br> * Note: unlike compare(), 0 != null and "" != null.</li> * <li>If one of them is number, the other is tried to convert to a number. * If failed to convert, false is returned.</li> * </ol> * @see #compare(Object,Object) *//* public static final boolean equals(Object o1, Object o2) { if (o1 == Objects.UNKNOWN) o1 = null; if (o2 == Objects.UNKNOWN) o2 = null; if (Objects.equals(o1, o2)) return true; if (o1 == null || o2 == null) return false; if (!(o1 instanceof Number) && !(o2 instanceof Number)) return false; try { o1 = valueOf(o1); o2 = valueOf(o2); o1 = promote(o1, o2); o2 = promote(o2, o1); assert D.OFF || o1.getClass().equals(o2.getClass()); return Objects.equals(o1, o2); //don't use o1.equals(o2) because Objects.equals handles BigDecimal } catch (NumberFormatException ex) { //ignore it return false; } }*/ /** Catenate two objects into a single string. * * <p>Since StringBuffer is not used, the performance might not be * good enough. *//* public static final String catenate(Object o1, Object o2) { if (o1 == Objects.UNKNOWN) o1 = null; if (o2 == Objects.UNKNOWN) o2 = null; //Unlike Javascript, null is converted to empty string instead of "null" //Reason: it is more readable String s1 = Objects.toString(o1); if (s1 == null) s1 = ""; String s2 = Objects.toString(o2); if (s2 == null) s2 = ""; if (s1.length() == 0) return s2; if (s2.length() == 0) return s1; return s1 + s2; }*/}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -