package org.xbib.datastructures.immutable.order;

/**
 * An ordering that treats {@code null} as greater than all other values.
 */
final class NullsLastOrdering<T extends Object> extends Ordering<T> {
    final Ordering<? super T> ordering;

    NullsLastOrdering(Ordering<? super T> ordering) {
        this.ordering = ordering;
    }

    @Override
    public int compare(T left, T right) {
        if (left == right) {
            return 0;
        }
        if (left == null) {
            return LEFT_IS_GREATER;
        }
        if (right == null) {
            return RIGHT_IS_GREATER;
        }
        return ordering.compare(left, right);
    }

    @Override
    @SuppressWarnings("nullness") // should be safe, but not sure if we can avoid the warning
    public <S extends T> Ordering<S> reverse() {
        // ordering.reverse() might be optimized, so let it do its thing
        return ordering.reverse().nullsFirst();
    }

    @Override
    public <S extends T> Ordering<S> nullsFirst() {
        return ordering.nullsFirst();
    }

    @SuppressWarnings("unchecked") // still need the right way to explain this
    @Override
    public <S extends T> Ordering<S> nullsLast() {
        return (Ordering<S>) this;
    }

    @Override
    public boolean equals(Object object) {
        if (object == this) {
            return true;
        }
        if (object instanceof NullsLastOrdering<?> that) {
            return this.ordering.equals(that.ordering);
        }
        return false;
    }

    @Override
    public int hashCode() {
        return ordering.hashCode() ^ -921210296; // meaningless
    }

    @Override
    public String toString() {
        return ordering + ".nullsLast()";
    }
}
