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 org.eclipse.jdt.annotation.NonNullByDefault;
14 import org.eclipse.jdt.annotation.Nullable;
15 import org.opendaylight.yangtools.concepts.Immutable;
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.
23 public final class PatternExpression implements Immutable {
24 private final String pattern;
25 private final String regex;
27 private PatternExpression(final String regex, final String pattern) {
28 this.regex = requireNonNull(regex);
29 this.pattern = requireNonNull(pattern);
32 public static PatternExpression of(final String regex, final String pattern) {
33 return new PatternExpression(regex, pattern);
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
41 * @return string Java Pattern regular expression
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() {
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.
54 * @return argument of pattern statement as it was declared in YANG model.
56 public String getRegularExpressionString() {
61 public int hashCode() {
62 return regex.hashCode();
66 public boolean equals(final @Nullable Object obj) {
70 if (!(obj instanceof PatternExpression)) {
73 final PatternExpression other = (PatternExpression) obj;
74 return regex.equals(other.regex) && pattern.equals(other.pattern);
78 public String toString() {