fd96883182bcb2055a1f34a5adac1eaeafd1d0f7
[mdsal.git] / binding / yang-binding / src / main / java / org / opendaylight / yangtools / yang / binding / CodeHelpers.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, s.r.o.  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.binding;
9
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;
13
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;
19
20 /**
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.
24  *
25  * @author Robert Varga
26  */
27 public final class CodeHelpers {
28     private CodeHelpers() {
29         // Hidden
30     }
31
32     /**
33      * Require that an a value-related expression is true.
34      *
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
39      */
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);
42     }
43
44     /**
45      * Require an argument being received. This is similar to {@link java.util.Objects#requireNonNull(Object)}, but
46      * throws an IllegalArgumentException.
47      *
48      * <p>
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).
51      *
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
57      */
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) {
61         requireNonNull(name);
62         checkArgument(value != null, "%s must not be null", name);
63         return value;
64     }
65
66     /**
67      * Compile a list of pattern regular expressions and return them as an array. The list must hold at least two
68      * expressions.
69      *
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
74      */
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));
81         }
82         return result;
83     }
84
85     /**
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.
88      *
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
94      */
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 \""
100                     + regex + "\"");
101         }
102     }
103
104     /**
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.
107      *
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
114      */
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]);
119         }
120     }
121 }