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

org.erjia.lang.range.Range Maven / Gradle / Ivy

Go to download

Java 开发工具包 ErJia2,是 ErJia 的升级版,向 Kotlin 迁移。“ErJia”,是以作者生活的村落”二甲村”而来,为了纪念这座美好的小村庄,特意将该工具包以“二甲”的拼音命名。

The newest version!
package org.erjia.lang.range;

import java.io.Serializable;
import java.util.Comparator;

public class Range> implements Serializable {

    private static final long serialVersionUID = 1L;

    public static > Range between(T minimum, T maximum) {
        return new Range<>(minimum, maximum);
    }

    public static > Range between(T minimum, T maximum, Comparator comparator) {
        return new Range<>(minimum, maximum, comparator);
    }

    /**
     * The maximum value in this range (inclusive).
     */
    private final T maximum;

    /**
     * The minimum value in this range (inclusive).
     */
    private final T minimum;

    private Comparator comparator = Comparable::compareTo;

    private Range(T minimum, T maximum) {
        this(minimum, maximum, null);
    }

    private Range(T minimum, T maximum, Comparator comparator) {
        if (minimum.compareTo(maximum) <= 0) {
            this.minimum = minimum;
            this.maximum = maximum;
        } else {
            this.minimum = maximum;
            this.maximum = minimum;
        }
        if (comparator != null) {
            this.comparator = comparator;
        }
    }

    public T minimum() {
        return minimum;
    }

    public T maximum() {
        return maximum;
    }

    public boolean contains(T element) {
        return comparator.compare(element, minimum) > -1 && comparator.compare(element, maximum) < 1;
    }

    @Override
    public boolean equals(final Object obj) {
        if (obj == this) {
            return true;
        } else if (obj == null || obj.getClass() != getClass()) {
            return false;
        } else {
            @SuppressWarnings("unchecked")
            final Range range = (Range) obj;
            return minimum.equals(range.minimum) && maximum.equals(range.maximum);
        }
    }

    @Override
    public String toString() {
        return "Range { " + "maximum=" + maximum + ", minimum=" + minimum + " }";
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy