} // getComparable() public int Compare(object o1, object o2) { CsNumber n1 = toNumber(o1); CsNumber n2 = toNumber(o2); if (n1 == null && n2 == null) { return(0); } if (n1 == null) { return(-1); } if (n2 == null) { return(1); } if (n1 is CsNumber && n2 is CsNumber) { return((n1.asBigInteger()).CompareTo(n2.asBigInteger())); } //if (n1 is BigDecimal && n2 is BigDecimal) { // return ((BigDecimal)n1).compareTo((BigDecimal)n2); //} if (NumberComparator.IsIntegerType(n1) && NumberComparator.IsIntegerType(n2)) { return(CsInteger.ValueOf(n1).CompareTo(n2.asLong())); } return(CsNumber.ValueOf(n1).CompareTo(n2.asLong())); } // Compare()
} // convertFromString() private static CsDate convertFromNumber(CsNumber value) { CsNumber numberValue = (CsNumber)value; long long_value = numberValue.asLong(); CsNumber n = new CsNumber(long_value); String string_value = n.ToString(); // test if the number is actually a format of the type yyyyMMdd if (string_value.Length == 8 && (string_value.StartsWith("1") || string_value.StartsWith("2"))) { try { string format = "yyyyMMdd"; DateTimeFormatInfo dtfi = CsDateUtils.createDateFormat(format); return(new CsDate(string_value, format, dtfi)); } catch (Exception e) { // do nothing, proceed to next method of conversion } } // test if the number is actually a format of the type yyMMdd if (string_value.Length == 6) { try { string format = "yyMMdd"; DateTimeFormatInfo dtfi = CsDateUtils.createDateFormat(format); return(new CsDate(string_value, format, dtfi)); } #pragma warning disable 0168 catch (Exception e) { // do nothing, proceed to next method of conversion } #pragma warning restore 0168 } if (long_value > 5000000) { // Java: this number is most probably amount of milliseconds since 1970 // C#: this number is most probably amount of milliseconds since 1900 CsDate d = new CsDate(DateTime.Now); return(new CsDate(long_value)); } else { // Java: this number is most probably amount of milliseconds since 1970 // C#: this number is most probably amount of milliseconds since 1900 return(new CsDate(long_value * 1000 * 60 * 60 * 24)); } } // convertFromNumber()
} // _Number_Comparable_Impl_ public static IComparable <object> getComparable(object o) { CsNumber n = toNumber(o); return(new _Number_Comparable_Impl_(_instance, n)); } // getComparable()
public _Number_Comparable_Impl_(IComparer <object> instance_arg, CsNumber value_arg) { _value_ = value_arg; _instance_ = instance_arg; } // constructor
} // isIntegerType() public static CsNumber toNumber(object value) { String string_value; if (value == null) { return(null); } else if (value is CsNumber) { return((CsNumber)value); } else if (value is CsBool) { if (bool.TrueString.Equals(value.ToString())) { return(CsInteger.ONE); } else { return(CsInteger.ZERO); } } else { string_value = value.ToString().Trim(); if (string_value.IsEmpty()) { return(null); } try { return(new CsNumber(CsNumber.ParseInt(string_value))); } catch (FormatException e) { } try { return(CsNumber.ParseLong(string_value)); } catch (FormatException e) { } try { return(new CsNumber(CsNumber.ParseDouble(string_value))); } catch (FormatException e) { } } //[J2Cs: Weird syntax block referring a variable from the previous if/else block] // note: Boolean.parseBoolean does not throw NumberFormatException - // it just returns false in case of invalid values. { if ("true".Equals(string_value, StringComparison.CurrentCultureIgnoreCase)) { return(CsInteger.ONE); } if ("false".Equals(string_value, StringComparison.CurrentCultureIgnoreCase)) { return(CsInteger.ZERO); } } logger.warn("Could not convert '{}' to number, returning null", value); return(null); } // toNumber()
} // Compare() /** * Determines if a particular number is an integer-type number such as * {@link Byte}, {@link Short}, {@link Integer}, {@link Long}, * {@link AtomicInteger} or {@link AtomicLong}. * * Note that {@link BigInteger} is not included in this set of number * classes since treatment of {@link BigInteger} requires different logic. * * @param n * @return */ public static bool IsIntegerType(CsNumber n) { return(n is CsInteger || n is CsAtomicInteger || n is CsAtomicLong); } // isIntegerType()