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