2 * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others. 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.model.api.stmt;
10 import static java.util.Objects.requireNonNull;
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;
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.
25 public final class PatternExpression implements Immutable {
26 private static final Interner<PatternExpression> INTERNER = Interners.newWeakInterner();
28 private final String pattern;
29 private final String regex;
31 private PatternExpression(final String regex, final String pattern) {
32 this.regex = requireNonNull(regex);
33 this.pattern = requireNonNull(pattern);
36 public static PatternExpression of(final String regex, final String pattern) {
37 return new PatternExpression(regex, pattern);
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
45 * @return string Java Pattern regular expression
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() {
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.
58 * @return argument of pattern statement as it was declared in YANG model.
60 public String getRegularExpressionString() {
65 * Returns an interned instance of this expression.
67 * @return A shared interned instance.
69 public PatternExpression intern() {
70 return INTERNER.intern(this);
74 public int hashCode() {
75 return regex.hashCode();
79 public boolean equals(final @Nullable Object obj) {
83 if (!(obj instanceof PatternExpression)) {
86 final PatternExpression other = (PatternExpression) obj;
87 return regex.equals(other.regex) && pattern.equals(other.pattern);
91 public String toString() {