All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.deephaven.engine.table.impl.SortingOrder Maven / Gradle / Ivy

There is a newer version: 0.37.1
Show newest version
/**
 * Copyright (c) 2016-2022 Deephaven Data Labs and Patent Pending
 */
package io.deephaven.engine.table.impl;

import org.jetbrains.annotations.NotNull;

import java.util.Comparator;

/**
 * Enum value for ascending vs. descending sorts.
 */
public enum SortingOrder {
    Ascending(1, getAscendingComparable()), Descending(-1, getDescendingComparator());

    public final int direction;
    private final Comparator comparator;

    SortingOrder(int direction, Comparator comparator) {
        this.direction = direction;
        this.comparator = comparator;
    }

    public int getDirection() {
        return direction;
    }

    public Comparator getComparator() {
        return comparator;
    }

    public boolean isAscending() {
        return direction == 1;
    }

    public boolean isDescending() {
        return direction == -1;
    }

    @NotNull
    private static Comparator getAscendingComparable() {
        return (o1, o2) -> {
            if (o1 == o2) {
                return 0;
            } else if (o1 == null) {
                return -1;
            } else if (o2 == null) {
                return 1;
            }
            // noinspection unchecked
            return o1.compareTo(o2);
        };
    }

    @NotNull
    private static Comparator getDescendingComparator() {
        return (o1, o2) -> {
            if (o1 == o2) {
                return 0;
            } else if (o1 == null) {
                return 1;
            } else if (o2 == null) {
                return -1;
            }
            // noinspection unchecked
            return -o1.compareTo(o2);
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy