ObjectComparer allows you to compare primitive types and some others using IComparable interface whenever possible, and performing type conversions to get the best possible result.
Inheritance: IComparer
コード例 #1
0
        /// <summary>   Calculates the value of the logical expression
        /// *
        /// arg1 == arg2
        /// *
        /// All class types are supported.   Uses equals() to
        /// determine equivalence.  This should work as we represent
        /// with the types we already support, and anything else that
        /// implements equals() to mean more than identical references.
        /// *
        /// *
        /// </summary>
        /// <param name="context"> internal context used to evaluate the LHS and RHS
        /// </param>
        /// <returns>true if equivalent, false if not equivalent,
        /// false if not compatible arguments, or false
        /// if either LHS or RHS is null
        ///
        /// </returns>
        public override bool evaluate(InternalContextAdapter context)
        {
            Object left  = jjtGetChild(0).Value(context);
            Object right = jjtGetChild(1).Value(context);

            /*
             * for equality, they are allowed to be null references
             */
            try
            {
                if (ObjectComparer.CompareObjects(left, right) == 0)
                {
                    return(true);
                }
            }
            catch
            {
                // Ignore, we can't compare decently by value, but we honestly don't give a sh*t
            }

            // They are not equal by value, try a reference comparison
            // reference equal => definitely equal objects ;)
            // For operator overloaded types, this will not really be a reference comp, but that's ok.
            return(left == right);
        }
コード例 #2
0
ファイル: ASTLTNode.cs プロジェクト: leon-wu/NVelocity
        public override bool Evaluate(IInternalContextAdapter context)
        {
            // get the two args
            Object left  = GetChild(0).Value(context);
            Object right = GetChild(1).Value(context);

            // if either is null, lets log and bail
            if (left == null || right == null)
            {
                runtimeServices.Error(
                    string.Format(
                        "{0} side ({1}) of '<' operation has null value. Operation not possible. {2} [line {3}, column {4}]",
                        (left == null ? "Left" : "Right"), GetChild((left == null ? 0 : 1)).Literal, context.CurrentTemplateName, Line,
                        Column));
                return(false);
            }

            try
            {
                return(ObjectComparer.CompareObjects(left, right) == ObjectComparer.Smaller);
            }
            catch (ArgumentException argumentException)
            {
                runtimeServices.Error(argumentException.Message);

                return(false);
            }
        }
コード例 #3
0
 static ObjectComparer()
 {
     ObjectComparer.comparers = new Hashtable();
     ObjectComparer.instance  = new ObjectComparer();
     new ObjectComparer.DoubleComparer().Register(ObjectComparer.comparers);
     new ObjectComparer.FloatComparer().Register(ObjectComparer.comparers);
     new ObjectComparer.ULongComparer().Register(ObjectComparer.comparers);
 }
コード例 #4
0
        public override bool Evaluate(IInternalContextAdapter context)
        {
            object obj  = base.GetChild(0).Value(context);
            object obj2 = base.GetChild(1).Value(context);
            bool   result;

            try
            {
                result = (ObjectComparer.CompareObjects(obj, obj2) != 0);
                return(result);
            }
            catch
            {
            }
            result = (obj != obj2);
            return(result);
        }
コード例 #5
0
        public override bool evaluate(InternalContextAdapter context)
        {
            Object left  = jjtGetChild(0).Value(context);
            Object right = jjtGetChild(1).Value(context);

            try
            {
                return(ObjectComparer.CompareObjects(left, right) != 0);
            }
            catch
            {
                // Ignore, we can't compare decently by value, but we honestly don't give a sh*t
            }

            // If we can't actually compare the objects, try the operator as fallback
            // For operator overloaded types, this will not really be a reference comp, but that's ok.
            return(left != right);
        }
コード例 #6
0
        /// <seealso cref="org.apache.velocity.runtime.parser.node.SimpleNode.evaluate(org.apache.velocity.context.InternalContextAdapter)">
        /// </seealso>
        public override bool Evaluate(IInternalContextAdapter context)
        {
            /*
             *  get the two args
             */

            object left  = GetChild(0).Value(context);
            object right = GetChild(1).Value(context);

            /*
             *  if either is null, lets log and bail
             */

            if (left == null || right == null)
            {
                System.String msg = (left == null ? "Left" : "Right") + " side (" + GetChild((left == null ? 0 : 1)).Literal + ") of '>=' operation has null value at " + Log.FormatFileString(this);

                if (rsvc.GetBoolean(RuntimeConstants.RUNTIME_REFERENCES_STRICT, false))
                {
                    throw new VelocityException(msg);
                }

                log.Error(msg);
                return(false);
            }


            try
            {
                return(ObjectComparer.CompareObjects(left, right) >= 0);
            }
            catch (ArgumentException ae)
            {
                log.Error(ae.Message);

                return(false);
            }
        }
コード例 #7
0
ファイル: ASTGENode.cs プロジェクト: SkelletonX/DDTServer
        public override bool Evaluate(IInternalContextAdapter context)
        {
            object obj  = base.GetChild(0).Value(context);
            object obj2 = base.GetChild(1).Value(context);
            bool   result;

            if (obj == null || obj2 == null)
            {
                this.rsvc.Error(string.Concat(new object[]
                {
                    (obj == null) ? "Left" : "Right",
                    " side (",
                    base.GetChild((obj == null) ? 0 : 1).Literal,
                    ") of '>=' operation has null value. Operation not possible. ",
                    context.CurrentTemplateName,
                    " [line ",
                    base.Line,
                    ", column ",
                    base.Column,
                    "]"
                }));
                result = false;
            }
            else
            {
                try
                {
                    result = (ObjectComparer.CompareObjects(obj, obj2) >= 0);
                }
                catch (ArgumentException ex)
                {
                    this.rsvc.Error(ex.Message);
                    result = false;
                }
            }
            return(result);
        }