84c94d5ca8d517abb45010ee5e29672aa6487620
[yangtools.git] / yang / yang-xpath-api / src / main / java / org / opendaylight / yangtools / yang / xpath / api / YangNaryOperator.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others.  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 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.collect.ImmutableSet;
14 import java.util.Collection;
15 import java.util.Set;
16
17 /**
18  * YANG XPath binary operator.
19  *
20  * @author Robert Varga
21  */
22 @Beta
23 public enum YangNaryOperator {
24     /**
25      * Logical 'and' operator on operands.
26      *
27      * @see <a href="https://www.w3.org/TR/1999/REC-xpath-19991116/#NT-AndExpr">AndExpr</a>
28      */
29     AND("and"),
30     /**
31      * Logical 'or' operator on operands.
32      *
33      * @see <a href="https://www.w3.org/TR/1999/REC-xpath-19991116/#NT-OrExpr">OrExpr</a>
34      */
35     OR("or"),
36     /**
37      * Set union operator on operands.
38      *
39      * @see <a href="https://www.w3.org/TR/1999/REC-xpath-19991116/#NT-UnionExpr">UnionExpr</a>
40      */
41     UNION("|");
42
43     private final class Expr extends YangNaryExpr {
44         private static final long serialVersionUID = 1L;
45
46         Expr(final Set<YangExpr> exprs) {
47             super(exprs);
48         }
49
50         @Override
51         public YangNaryOperator getOperator() {
52             return YangNaryOperator.this;
53         }
54     }
55
56     private final String str;
57
58     YangNaryOperator(final String str) {
59         this.str = requireNonNull(str);
60     }
61
62     @Override
63     public String toString() {
64         return str;
65     }
66
67     public YangExpr exprWith(final Collection<YangExpr> exprs) {
68         final Set<YangExpr> set = ImmutableSet.copyOf(exprs);
69         return set.size() == 1 ? set.iterator().next() : new Expr(set);
70     }
71 }