Exemplo n.º 1
0
        } // _Number_Comparable_Impl_


        //public static IComparable<object> getComparable(object o)
        //{
        //    NNumber n = toNumber(o);
        //    return new _Number_Comparable_Impl_(_instance, n);
        //} // getComparable()


        public int Compare(object o1, object o2)
        {
            NNumber n1 = toNumber(o1);
            NNumber n2 = toNumber(o2);

            if (n1 == null && n2 == null)
            {
                return(0);
            }
            if (n1 == null)
            {
                return(-1);
            }
            if (n2 == null)
            {
                return(1);
            }

            if (n1 is NNumber && n2 is NNumber)
            {
                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(NInteger.ValueOf(n1).CompareTo(n2.asLong()));
            }

            return(NNumber.ValueOf(n1).CompareTo(n2.asLong()));
        } // Compare()
Exemplo n.º 2
0
        public void TestCompareSimple()
        {
            IComparer <object> comparer = NumberComparator.getComparator();
            var result = comparer.Compare(1, 2);

            Assert.Equal(1, result);
        }
        // @SuppressWarnings("unchecked")
        public int compare(object o1, object o2)
        {
            logger.debug("compare({},{})", o1, o2);
            if (o1 == null && o2 == null)
            {
                return(0);
            }
            if (o1 == null)
            {
                return(-1);
            }
            if (o2 == null)
            {
                return(1);
            }
            if (o1 is CsNumber && o1 is CsNumber)
            {
                return(NumberComparator.getComparator().Compare(o1, o2));
            }
            if (TimeComparator.isTimeBased(o1) && TimeComparator.isTimeBased(o2))
            {
                return(TimeComparator.getComparator().Compare(o1, o2));
            }
            if (BooleanComparator.isBoolean(o1) && BooleanComparator.isBoolean(o2))
            {
                return(BooleanComparator.getComparator().Compare(o1, o2));
            }
            if (o1 is IComparable && o2 is IComparable)
            {
                //@SuppressWarnings("rawtypes")

                IComparable c1 = (IComparable)o1;
                //@SuppressWarnings("rawtypes")

                IComparable c2 = (IComparable)o2;
                // We can only count on using the comparable interface if o1 and o2
                // are within of the same class or if one is a subclass of the other
                if (c1.GetType().IsAssignableFrom(c2.GetType()))
                {
                    return(c1.CompareTo(o2));
                }
                if (o2.GetType().IsAssignableFrom(c1.GetType()))
                {
                    return(-1 * c2.CompareTo(o1));
                }
            }
            logger.debug("Using ToStringComparator because no apparent better comparison method could be found");
            return(ToStringComparator.getComparator().Compare(o1, o2));
        } // compare()