public int compareTo()

in xmlschema-walker/src/main/java/org/apache/ws/commons/schema/docpath/XmlSchemaPathFinder.java [101:200]


        public int compareTo(PathSegment<U, V> o) {
            if (this == o) {
                return 0;
            }

            /*
             * Paths which end in a wildcard element are of lower rank (higher
             * order) than those that end in elements.
             */
            if (!end.getStateMachineNode().getNodeType()
                .equals(o.getEnd().getStateMachineNode().getNodeType())) {

                if (end.getStateMachineNode().getNodeType().equals(XmlSchemaStateMachineNode.Type.ANY)) {
                    return 1;

                } else if (o.getEnd().getStateMachineNode().getNodeType()
                    .equals(XmlSchemaStateMachineNode.Type.ANY)) {
                    return -1;

                } else {
                    throw new IllegalStateException("The end nodes do not have the same machine node type, so one "
                                                        + "should be an ELEMENT and the other should be an ANY.  "
                                                        + "However, this end node is a "
                                                        + end.getStateMachineNode().getNodeType()
                                                        + " while the other has an end node of type "
                                                        + o.getEnd().getStateMachineNode().getNodeType()
                                                        + ".");
                }
            }

            final int thisLength = getLength();
            final int thatLength = o.getLength();

            /*
             * Paths that walk through earlier sequence group children are
             * preferred over paths that walk through later sequence group
             * children. They provide more options later. Shorter paths are also
             * preferred over longer ones.
             */
            if ((thisLength > 0) && (thatLength > 0)) {
                // Both paths have more than just one element.
                XmlSchemaPathNode<U, V> thisIter = afterStart;
                XmlSchemaPathNode<U, V> thatIter = o.getAfterStart();

                while ((thisIter != null) && (thatIter != null)) {
                    if (thisIter.getDirection().getRank() < thatIter.getDirection().getRank()) {
                        return -1;
                    } else if (thisIter.getDirection().getRank() > thatIter.getDirection().getRank()) {
                        return 1;
                    }

                    if (thisIter.getIndexOfNextNodeState() < thatIter.getIndexOfNextNodeState()) {

                        return -1;

                    } else if (thisIter.getIndexOfNextNodeState() > thatIter.getIndexOfNextNodeState()) {

                        return 1;
                    }

                    thisIter = thisIter.getNext();
                    thatIter = thatIter.getNext();
                }

                if ((thisIter == null) && (thatIter != null)) {
                    // This path is shorter.
                    return -1;
                } else if ((thisIter != null) && (thatIter == null)) {
                    // That path is shorter.
                    return 1;
                }

            } else if ((thisLength == 0) && (thatLength > 0)) {
                // This path is shorter.
                return -1;

            } else if ((thisLength > 0) && (thatLength == 0)) {
                // That path is shorter.
                return 1;

            } else {
                // Both paths have exactly one element.
                if (end.getIndexOfNextNodeState() < o.getEnd().getIndexOfNextNodeState()) {

                    return -1;

                } else if (end.getIndexOfNextNodeState() > o.getEnd().getIndexOfNextNodeState()) {

                    return 1;
                }
            }

            /*
             * If all of our different heuristics do not differentiate the
             * paths, we will return equality. This is fine because
             * Collections.sort(List<T>) is stable, and will preserve the
             * ordering.
             */
            return 0;
        }