Populate model/ hierarchy
[yangtools.git] / model / yang-model-api / src / main / java / org / opendaylight / yangtools / yang / model / api / stmt / PatternExpression.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, 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.model.api.stmt;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import org.eclipse.jdt.annotation.NonNullByDefault;
14 import org.eclipse.jdt.annotation.Nullable;
15 import org.opendaylight.yangtools.concepts.Immutable;
16
17 /**
18  * An intermediate capture of the argument to {@code pattern} statement. It exposes both XSD regular expression, as well
19  * as a {@link java.util.regex.Pattern} pattern.
20  */
21 @Beta
22 @NonNullByDefault
23 public final class PatternExpression implements Immutable {
24     private final String pattern;
25     private final String regex;
26
27     private PatternExpression(final String regex, final String pattern) {
28         this.regex = requireNonNull(regex);
29         this.pattern = requireNonNull(pattern);
30     }
31
32     public static PatternExpression of(final String regex, final String pattern) {
33         return new PatternExpression(regex, pattern);
34     }
35
36     /**
37      * Returns a Java {@link java.util.regex.Pattern}-compatible regular expression (pattern). Returned string performs
38      * equivalent  matching in terms of enforcement, but it may have a structure completely different from the one in
39      * YANG model.
40      *
41      * @return string Java Pattern regular expression
42      */
43     // FIXME: should we be providing a Pattern instance? This, along with the other method is treading the fine
44     //        balance between usability of the effective model, the purity of effective view model and memory
45     //        overhead. We pick usability and memory footprint and expose both methods from effective model.
46     public String getJavaPatternString() {
47         return pattern;
48     }
49
50     /**
51      * Returns a raw regular expression as it was declared in a source. This string conforms to XSD regular expression
52      * syntax, which is notably different from Java's Pattern string.
53      *
54      * @return argument of pattern statement as it was declared in YANG model.
55      */
56     public String getRegularExpressionString() {
57         return regex;
58     }
59
60     @Override
61     public int hashCode() {
62         return regex.hashCode();
63     }
64
65     @Override
66     public boolean equals(final @Nullable Object obj) {
67         if (this == obj) {
68             return true;
69         }
70         if (!(obj instanceof PatternExpression)) {
71             return false;
72         }
73         final PatternExpression other = (PatternExpression) obj;
74         return regex.equals(other.regex) && pattern.equals(other.pattern);
75     }
76
77     @Override
78     public String toString() {
79         return regex;
80     }
81 }