public static int compareNullLast()

in commons/src/main/java/org/apache/causeway/commons/internal/compare/_Comparators_SequenceCompare.java [37:105]


    public static int compareNullLast(
            final @Nullable String sequence1,
            final @Nullable String sequence2,
            final String separator) {

        if(_Strings.isEmpty(separator))
            throw new IllegalArgumentException("a non empty separator is required");

        if (sequence1 == null && sequence2 == null) {
            return 0;
        }

        if (sequence1 == null && sequence2 != null) {
            return +1; // non-null before null
        }
        if (sequence1 != null && sequence2 == null) {
            return -1; // non-null before null
        }

        final StringTokenizer components1 = tokenizerFor(sequence1, separator);
        final StringTokenizer components2 = tokenizerFor(sequence2, separator);

        final int length1 = components1.countTokens();
        final int length2 = components2.countTokens();

        // shouldn't happen but just in case.
        if (length1 == 0 && length2 == 0) {
            return 0;
        }

        // continue to loop until we run out of components.
        int n = 0;
        while (true) {
            final int length = n + 1;
            // check if run out of components in either side
            if (length1 < length && length2 >= length) {
                return -1; // o1 before o2
            }
            if (length2 < length && length1 >= length) {
                return +1; // o2 before o1
            }
            if (length1 < length && length2 < length) {
                // run out of components
                return 0;
            }
            // we have this component on each side

            var token1 = components1.nextToken();
            var token2 = components2.nextToken();

            int componentCompare = 0;

            var int1 = _Ints.parseInt(token1, 10);
            var int2 = _Ints.parseInt(token2, 10);

            if(int1.isPresent() && int2.isPresent()) {
                componentCompare = Integer.compare(int1.getAsInt(), int2.getAsInt());
            } else {
                // not integers compare as strings
                componentCompare = token1.compareTo(token2);
            }

            if (componentCompare != 0) {
                return componentCompare;
            }
            // this component is the same; lets look at the next
            n++;
        }
    }