Beispiel #1
0
        /// <summary>
        /// Compares this Node to another Node
        /// </summary>
        /// <param name="other">Other Node</param>
        /// <returns></returns>
        /// <remarks>
        /// Unless Virtual Equality (equality based on the Virtual RDF Provider and Virtual ID) can be determined or the Nodes are of different types then the Nodes value will have to be materialised in order to perform comparison.
        /// </remarks>
        public int CompareTo(INode other)
        {
            if (ReferenceEquals(this, other))
            {
                return(0);
            }
            if (other == null)
            {
                return(1);
            }
            bool areEqual;

            if (this.TryVirtualEquality(other, out areEqual) && areEqual)
            {
                return(0);
            }

            MaterialiseValue();
            switch (this._value.NodeType)
            {
            case NodeType.Blank:
                if (other.NodeType == NodeType.Variable)
                {
                    //Blank Nodes are greater than variables
                    return(1);
                }
                else if (other.NodeType == NodeType.Blank)
                {
                    //Compare Blank Node appropriately
                    return(ComparisonHelper.CompareBlankNodes((IBlankNode)this, (IBlankNode)other));
                }
                else
                {
                    //Blank Nodes are less than everything else
                    return(-1);
                }

            case NodeType.GraphLiteral:
                if (other.NodeType == NodeType.GraphLiteral)
                {
                    //Compare Graph Literals appropriately
                    return(ComparisonHelper.CompareGraphLiterals((IGraphLiteralNode)this, (IGraphLiteralNode)other));
                }
                else
                {
                    //Graph Literals are greater than everything else
                    return(1);
                }

            case NodeType.Literal:
                if (other.NodeType == NodeType.GraphLiteral)
                {
                    //Literals are less than Graph Literals
                    return(-1);
                }
                else if (other.NodeType == NodeType.Literal)
                {
                    //Compare Literals appropriately
                    return(ComparisonHelper.CompareLiterals((ILiteralNode)this, (ILiteralNode)other));
                }
                else
                {
                    //Literals are greater than anything else (i.e. Blanks, Variables and URIs)
                    return(1);
                }

            case NodeType.Uri:
                if (other.NodeType == NodeType.GraphLiteral || other.NodeType == NodeType.Literal)
                {
                    //URIs are less than Literals and Graph Literals
                    return(-1);
                }
                else if (other.NodeType == NodeType.Uri)
                {
                    //Compare URIs appropriately
                    return(ComparisonHelper.CompareUris((IUriNode)this, (IUriNode)other));
                }
                else
                {
                    //URIs are greater than anything else (i.e. Blanks and Variables)
                    return(1);
                }

            case NodeType.Variable:
                if (other.NodeType == NodeType.Variable)
                {
                    //Compare Variables accordingly
                    return(ComparisonHelper.CompareVariables((IVariableNode)this, (IVariableNode)other));
                }
                else
                {
                    //Variables are less than anything else
                    return(-1);
                }

            default:
                //Things are always greater than unknown node types
                return(1);
            }
        }