2 * Copyright (c) 2018 Pantheon Technologies, s.r.o. 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.binding;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Verify.verify;
12 import static java.util.Objects.requireNonNull;
14 import com.google.common.base.VerifyException;
15 import java.util.List;
16 import java.util.regex.Pattern;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.eclipse.jdt.annotation.Nullable;
21 * Helper methods for generated binding code. This class concentrates useful primitives generated code may call
22 * to perform specific shared functions. This allows for generated classes to be leaner. Methods in this class follows
23 * general API stability requirements of the Binding Specification.
25 * @author Robert Varga
27 public final class CodeHelpers {
28 private CodeHelpers() {
33 * Require that an a value-related expression is true.
35 * @param expression Expression to evaluate
36 * @param value Value being validated
37 * @param options Valid value options checked
38 * @throws IllegalArgumentException if expression is false
40 public static void validValue(final boolean expression, final Object value, final String options) {
41 checkArgument(expression, "expected one of: %s \n%but was: %s", options, value);
45 * Require an argument being received. This is similar to {@link java.util.Objects#requireNonNull(Object)}, but
46 * throws an IllegalArgumentException.
49 * Implementation note: we expect argName to be a string literal or a constant, so that it's non-nullness can be
50 * quickly discovered for a call site (where we are going to be inlined).
52 * @param value Value itself
53 * @param name Symbolic name
54 * @return non-null value
55 * @throws IllegalArgumentException if value is null
56 * @throws NullPointerException if name is null
58 // FIXME: another advantage is that it is JDT-annotated, but we could live without that. At some point we should
59 // schedule a big ISE-to-NPE conversion and just use Objects.requireNonNull() instead.
60 public static <T> @NonNull T nonNullValue(@Nullable final T value, final @NonNull String name) {
62 checkArgument(value != null, "%s must not be null", name);
67 * Compile a list of pattern regular expressions and return them as an array. The list must hold at least two
70 * @param patterns Patterns to compile
71 * @return Compiled patterns in an array
72 * @throws NullPointerException if the list or any of its elements is null
73 * @throws VerifyException if the list has fewer than two elements
75 public static @NonNull Pattern[] compilePatterns(final @NonNull List<String> patterns) {
76 final int size = patterns.size();
77 verify(size > 1, "Patterns has to have at least 2 elements");
78 final @NonNull Pattern[] result = new Pattern[size];
79 for (int i = 0; i < size; ++i) {
80 result[i] = Pattern.compile(patterns.get(i));
86 * Check whether a specified string value matches a specified pattern. This method handles the distinction between
87 * modeled XSD expression and enforcement {@link Pattern} which may reflect negation.
89 * @param value Value to be checked.
90 * @param pattern Enforcement pattern
91 * @param regex Source regular expression, as defined in YANG model
92 * @throws IllegalArgumentException if the value does not match the pattern
93 * @throws NullPointerException if any of the arguments are null
95 public static void checkPattern(final String value, final Pattern pattern, final String regex) {
96 if (!pattern.matcher(value).matches()) {
97 final String match = BindingMapping.isNegatedPattern(pattern) ? "matches forbidden"
98 : "does not match required";
99 throw new IllegalArgumentException("Supplied value \"" + value + "\" " + match + " pattern \""
105 * Check whether a specified string value matches specified patterns. This method handles the distinction between
106 * modeled XSD expression and enforcement {@link Pattern} which may reflect negation.
108 * @param value Value to be checked.
109 * @param patterns Enforcement patterns
110 * @param regexes Source regular expression, as defined in YANG model. Size and order must match patterns.
111 * @throws IllegalArgumentException if the value does not match the pattern
112 * @throws NullPointerException if any of the arguments are null
113 * @throws VerifyException if the size of patterns and regexes does not match
115 public static void checkPattern(final String value, final Pattern[] patterns, final String[] regexes) {
116 verify(patterns.length == regexes.length, "Patterns and regular expression lengths have to match");
117 for (int i = 0; i < patterns.length; ++i) {
118 checkPattern(value, patterns[i], regexes[i]);