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

io.github.vmzakharov.ecdataframe.dsl.value.DecimalValue Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
package io.github.vmzakharov.ecdataframe.dsl.value;

import io.github.vmzakharov.ecdataframe.dsl.ArithmeticOp;
import io.github.vmzakharov.ecdataframe.dsl.PredicateOp;
import io.github.vmzakharov.ecdataframe.dsl.UnaryOp;

import java.math.BigDecimal;

import static io.github.vmzakharov.ecdataframe.util.ExceptionFactory.*;

public record DecimalValue(BigDecimal value)
implements NumberValue
{
    public DecimalValue
    {
        this.throwExceptionIfNull(value);
    }

    public DecimalValue(long unscaledValue, int scale)
    {
        this(BigDecimal.valueOf(unscaledValue, scale));
    }

    @Override
    public String asStringLiteral()
    {
        return "[" + this.value.unscaledValue() + ',' + this.value.scale() + ']';
    }

    @Override
    public Value apply(Value another, ArithmeticOp operation)
    {
        return operation.applyDecimal(this.decimalValue(), ((NumberValue) another).decimalValue());
    }

    @Override
    public Value apply(UnaryOp operation)
    {
        return operation.applyDecimal(this.value);
    }

    @Override
    public BooleanValue applyPredicate(Value another, PredicateOp operation)
    {
        return operation.applyDecimal(this.decimalValue(), ((DecimalValue) another).decimalValue());
    }

    @Override
    public ValueType getType()
    {
        return ValueType.DECIMAL;
    }

    @Override
    public BigDecimal decimalValue()
    {
        return this.value;
    }

    @Override
    public double doubleValue()
    {
        throw exceptionByKey("DSL_NO_DEC_TO_FLOAT_CONVERSION").get();
    }

    @Override
    public int compareTo(Value other)
    {
        this.checkSameTypeForComparison(other);

        return other.isVoid() ? 1 : this.decimalValue().compareTo(((DecimalValue) other).decimalValue());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy