2 * Copyright (c) 2019 Pantheon Technologies, s.r.o. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.xpath.api;
10 import java.io.Serial;
11 import java.math.BigDecimal;
13 final class BigDecimalXPathMathSupport extends AbstractYangXPathMathSupport<BigDecimalNumberExpr> {
14 static final BigDecimalXPathMathSupport INSTANCE = new BigDecimalXPathMathSupport();
17 private static final long serialVersionUID = 1L;
18 private static final BigDecimalNumberExpr ZERO = BigDecimalNumberExpr.of(BigDecimal.ZERO);
19 private static final BigDecimalNumberExpr ONE = BigDecimalNumberExpr.of(BigDecimal.ONE);
20 private static final BigDecimalNumberExpr TEN = BigDecimalNumberExpr.of(BigDecimal.TEN);
22 private BigDecimalXPathMathSupport() {
23 super(BigDecimalNumberExpr.class);
27 public BigDecimalNumberExpr createNumber(final String str) {
32 default -> BigDecimalNumberExpr.of(new BigDecimal(str));
37 public BigDecimalNumberExpr createNumber(final int value) {
38 return switch (value) {
42 default -> BigDecimalNumberExpr.of(BigDecimal.valueOf(value));
47 protected BigDecimalNumberExpr doNegateNumber(final BigDecimalNumberExpr number) {
48 return BigDecimalNumberExpr.of(number.getNumber().negate());
52 protected YangExpr doEvaluate(final YangBinaryOperator operator, final BigDecimalNumberExpr left,
53 final BigDecimalNumberExpr right) {
54 final var l = left.getNumber();
55 final var r = right.getNumber();
57 return switch (operator) {
58 case DIV -> BigDecimalNumberExpr.of(l.divide(r));
59 case EQUALS -> YangBooleanConstantExpr.of(l.equals(r));
60 case GT -> YangBooleanConstantExpr.of(l.compareTo(r) > 0);
61 case GTE -> YangBooleanConstantExpr.of(l.compareTo(r) >= 0);
62 case LT -> YangBooleanConstantExpr.of(l.compareTo(r) < 0);
63 case LTE -> YangBooleanConstantExpr.of(l.compareTo(r) <= 0);
64 case MINUS -> BigDecimalNumberExpr.of(l.subtract(r));
65 case MOD -> BigDecimalNumberExpr.of(l.remainder(r));
66 case MUL -> BigDecimalNumberExpr.of(l.multiply(r));
67 case NOT_EQUALS -> YangBooleanConstantExpr.of(!l.equals(r));
68 case PLUS -> BigDecimalNumberExpr.of(l.add(r));
73 protected Object readResolve() {