Update yang-xpath-api design
[yangtools.git] / yang / yang-xpath-api / src / main / java / org / opendaylight / yangtools / yang / xpath / api / DoubleXPathMathSupport.java
1 /*
2  * Copyright (c) 2019 Pantheon Technologies, s.r.o.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.xpath.api;
9
10 final class DoubleXPathMathSupport extends AbstractYangXPathMathSupport<DoubleNumberExpr> {
11     private static final DoubleXPathMathSupport INSTANCE = new DoubleXPathMathSupport();
12
13     private DoubleXPathMathSupport() {
14         super(DoubleNumberExpr.class);
15     }
16
17     static DoubleXPathMathSupport getInstance() {
18         return INSTANCE;
19     }
20
21     @Override
22     public DoubleNumberExpr createNumber(final String str) {
23         return DoubleNumberExpr.of(Double.parseDouble(str));
24     }
25
26     @Override
27     DoubleNumberExpr doNegate(final DoubleNumberExpr number) {
28         return DoubleNumberExpr.of(-number.getValue());
29     }
30
31     @Override
32     YangExpr evaluate(final YangBinaryOperator operator, final DoubleNumberExpr left, final DoubleNumberExpr right) {
33         final double l = left.getValue();
34         final double r = right.getValue();
35
36         final double result;
37         switch (operator) {
38             case DIV:
39                 result = l / r;
40                 break;
41             case EQUALS:
42                 return YangBooleanConstantExpr.of(left.equals(right));
43             case GT:
44                 return YangBooleanConstantExpr.of(l > r);
45             case GTE:
46                 return YangBooleanConstantExpr.of(l >= r);
47             case LT:
48                 return YangBooleanConstantExpr.of(l < r);
49             case LTE:
50                 return YangBooleanConstantExpr.of(l <= r);
51             case MINUS:
52                 result = l - r;
53                 break;
54             case MOD:
55                 result = l % r;
56                 break;
57             case MUL:
58                 result = l * r;
59                 break;
60             case NOT_EQUALS:
61                 return YangBooleanConstantExpr.of(!left.equals(right));
62             case PLUS:
63                 result = l + r;
64                 break;
65             default:
66                 throw new IllegalStateException("Unhandled operator " + operator);
67         }
68
69         return DoubleNumberExpr.of(result);
70     }
71 }