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

spring.turbo.bean.NumberPair Maven / Gradle / Ivy

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *    ____             _            _____           _
 *   / ___| _ __  _ __(_)_ __   __ |_   _|   _ _ __| |__   ___
 *   \___ \| '_ \| '__| | '_ \ / _` || || | | | '__| '_ \ / _ \
 *    ___) | |_) | |  | | | | | (_| || || |_| | |  | |_) | (_) |
 *   |____/| .__/|_|  |_|_| |_|\__, ||_| \__,_|_|  |_.__/ \___/
 *         |_|                 |___/   https://github.com/yingzhuo/spring-turbo
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
package spring.turbo.bean;

import spring.turbo.lang.Immutable;
import spring.turbo.util.Asserts;
import spring.turbo.util.BigDecimalUtils;
import spring.turbo.util.StringFormatter;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Objects;

/**
 * 数字对
 *
 * @author 应卓
 * @see LongPair
 * @see BigIntegerPair
 * @see DoublePair
 * @see BigDecimalPair
 * @since 1.0.7
 */
@Immutable
public class NumberPair implements Serializable {

    private final BigDecimal left;
    private final BigDecimal right;

    public NumberPair(BigDecimal left, BigDecimal right) {
        Asserts.notNull(left);
        Asserts.notNull(right);
        this.left = left;
        this.right = right;
    }

    public final boolean isOrdered() {
        return left.compareTo(right) <= 0;
    }

    public final NumberPair toOrdered() {
        return isOrdered() ? this : new NumberPair(right, left);
    }

    public  Pair toPair(Class type) {
        return Pair.ofNonNull(getLeft(type), getRight(type));
    }

    public  T getLeft(Class type) {
        return BigDecimalUtils.getValue(left, type);
    }

    public  T getRight(Class type) {
        return BigDecimalUtils.getValue(right, type);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        NumberPair that = (NumberPair) o;
        return left.equals(that.left) && right.equals(that.right);
    }

    @Override
    public int hashCode() {
        return Objects.hash(left, right);
    }

    @Override
    public String toString() {
        return StringFormatter.format("{} - {}", left, right);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy