Files
paradigms/java/expression/Const.java
2026-04-08 21:25:17 +03:00

85 lines
1.8 KiB
Java

package expression;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
/**
* @author Doschennikov Nikita (me@fymio.us)
*/
public class Const extends AbstractExpression {
private final int intValue;
private final BigInteger biValue;
private final BigDecimal bdValue;
private final String repr;
public Const(int value) {
this.intValue = value;
this.biValue = BigInteger.valueOf(value);
this.bdValue = BigDecimal.valueOf(value);
this.repr = Integer.toString(value);
}
public Const(BigInteger value) {
this.intValue = value.intValueExact();
this.biValue = value;
this.bdValue = new BigDecimal(value);
this.repr = value.toString();
}
public Const(BigDecimal value) {
this.bdValue = value;
this.biValue = value.toBigIntegerExact();
this.intValue = biValue.intValueExact();
this.repr = value.toPlainString();
}
@Override
public int evaluate(int x) {
return intValue;
}
@Override
public int evaluate(int x, int y, int z) {
return intValue;
}
@Override
public int evaluate(List<Integer> vars) {
return intValue;
}
@Override
public BigInteger evaluateBi(List<BigInteger> vars) {
return biValue;
}
@Override
public BigDecimal evaluateBd(List<BigDecimal> vars) {
return bdValue;
}
@Override
public String toString() {
return repr;
}
@Override
public String toMiniString() {
return repr;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof Const)) return false;
return repr.equals(((Const) obj).repr);
}
@Override
public int hashCode() {
return repr.hashCode();
}
}