Class CodeHelpers
- java.lang.Object
-
- org.opendaylight.yangtools.yang.binding.CodeHelpers
-
public final class CodeHelpers extends Object
Helper methods for generated binding code. This class concentrates useful primitives generated code may call to perform specific shared functions. This allows for generated classes to be leaner. Methods in this class follows general API stability requirements of the Binding Specification.- Author:
- Robert Varga
-
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static void
appendValue(MoreObjects.ToStringHelper helper, @NonNull String name, @Nullable Object value)
Append a named value to a ToStringHelper.static void
appendValue(MoreObjects.ToStringHelper helper, String name, byte[] value)
Append a named value to a ToStringHelper.static void
checkPattern(String value, Pattern[] patterns, String[] regexes)
Check whether a specified string value matches specified patterns.static void
checkPattern(String value, Pattern pattern, String regex)
Check whether a specified string value matches a specified pattern.static @Nullable Uint16
compatUint(@Nullable Integer value)
Deprecated.This method is provided for migration purposes only, do not use it outside of deprecated compatibility methods.static @Nullable Uint32
compatUint(@Nullable Long value)
Deprecated.This method is provided for migration purposes only, do not use it outside of deprecated compatibility methods.static @Nullable Uint8
compatUint(@Nullable Short value)
Deprecated.This method is provided for migration purposes only, do not use it outside of deprecated compatibility methods.static @Nullable Uint64
compatUint(@Nullable BigInteger value)
Deprecated.This method is provided for migration purposes only, do not use it outside of deprecated compatibility methods.static Pattern @NonNull []
compilePatterns(@NonNull List<String> patterns)
Compile a list of pattern regular expressions and return them as an array.static <T> @NonNull List<T>
nonnull(@Nullable List<T> input)
Check whether specified List is null and if so return an immutable list instead.static <T> @NonNull T
nonNullValue(@Nullable T value, @NonNull String name)
Require an argument being received.static void
requireValue(@Nullable Object value)
A shortcut forObjects.requireNonNull(value, "Supplied value may not be null")
.static void
throwInvalidLength(String expected, byte[] actual)
Throw an IllegalArgument exception describing a length violation.static void
throwInvalidLength(String expected, Object actual)
Throw an IllegalArgument exception describing a length violation.static void
throwInvalidRange(Object[] expected, Object actual)
Throw an IllegalArgument exception describing a range violation.static void
throwInvalidRange(String expected, int actual)
Throw an IllegalArgument exception describing a range violation.static void
throwInvalidRange(String expected, long actual)
Throw an IllegalArgument exception describing a range violation.static void
throwInvalidRange(String expected, Object actual)
Throw an IllegalArgument exception describing a range violation.static void
throwInvalidRangeUnsigned(String expected, long actual)
Throw an IllegalArgument exception describing a range violation of an Uint64 type.static void
validValue(boolean expression, Object value, String options)
Require that an a value-related expression is true.static int
wrapperHashCode(byte @Nullable [] obj)
Return hash code of a single-property wrapper class.static int
wrapperHashCode(@Nullable Object obj)
Return hash code of a single-property wrapper class.
-
-
-
Method Detail
-
validValue
public static void validValue(boolean expression, Object value, String options)
Require that an a value-related expression is true.- Parameters:
expression
- Expression to evaluatevalue
- Value being validatedoptions
- Valid value options checked- Throws:
IllegalArgumentException
- if expression is false
-
nonNullValue
public static <T> @NonNull T nonNullValue(@Nullable T value, @NonNull String name)
Require an argument being received. This is similar toObjects.requireNonNull(Object)
, but throws an IllegalArgumentException.Implementation note: we expect argName to be a string literal or a constant, so that it's non-nullness can be quickly discovered for a call site (where we are going to be inlined).
- Parameters:
value
- Value itselfname
- Symbolic name- Returns:
- non-null value
- Throws:
IllegalArgumentException
- if value is nullNullPointerException
- if name is null
-
requireValue
public static void requireValue(@Nullable Object value)
A shortcut forObjects.requireNonNull(value, "Supplied value may not be null")
.- Parameters:
value
- Value itself- Throws:
NullPointerException
- if value is null
-
appendValue
public static void appendValue(MoreObjects.ToStringHelper helper, @NonNull String name, @Nullable Object value)
Append a named value to a ToStringHelper. If the value is null, this method does nothing.- Parameters:
helper
- Helper to append toname
- Name of the valuevalue
- Value to append- Throws:
NullPointerException
- if the name or helper is null
-
appendValue
public static void appendValue(MoreObjects.ToStringHelper helper, String name, byte[] value)
Append a named value to a ToStringHelper. If the value is null, this method does nothing.- Parameters:
helper
- Helper to append toname
- Name of the valuevalue
- Value to append- Throws:
NullPointerException
- if the name or helper is null
-
compilePatterns
public static Pattern @NonNull [] compilePatterns(@NonNull List<String> patterns)
Compile a list of pattern regular expressions and return them as an array. The list must hold at least two expressions.- Parameters:
patterns
- Patterns to compile- Returns:
- Compiled patterns in an array
- Throws:
NullPointerException
- if the list or any of its elements is nullVerifyException
- if the list has fewer than two elements
-
checkPattern
public static void checkPattern(String value, Pattern pattern, String regex)
Check whether a specified string value matches a specified pattern. This method handles the distinction between modeled XSD expression and enforcementPattern
which may reflect negation.- Parameters:
value
- Value to be checked.pattern
- Enforcement patternregex
- Source regular expression, as defined in YANG model- Throws:
IllegalArgumentException
- if the value does not match the patternNullPointerException
- if any of the arguments are null
-
checkPattern
public static void checkPattern(String value, Pattern[] patterns, String[] regexes)
Check whether a specified string value matches specified patterns. This method handles the distinction between modeled XSD expression and enforcementPattern
which may reflect negation.- Parameters:
value
- Value to be checked.patterns
- Enforcement patternsregexes
- Source regular expression, as defined in YANG model. Size and order must match patterns.- Throws:
IllegalArgumentException
- if the value does not match the patternNullPointerException
- if any of the arguments are nullVerifyException
- if the size of patterns and regexes does not match
-
throwInvalidLength
public static void throwInvalidLength(String expected, Object actual)
Throw an IllegalArgument exception describing a length violation.- Parameters:
expected
- String describing expected lengthsactual
- Actual observed object- Throws:
IllegalArgumentException
- always
-
throwInvalidLength
public static void throwInvalidLength(String expected, byte[] actual)
Throw an IllegalArgument exception describing a length violation.- Parameters:
expected
- String describing expected lengthsactual
- Actual observed byte array- Throws:
IllegalArgumentException
- always
-
throwInvalidRange
public static void throwInvalidRange(String expected, Object actual)
Throw an IllegalArgument exception describing a range violation.- Parameters:
expected
- String describing expected rangesactual
- Actual observed object- Throws:
IllegalArgumentException
- always
-
throwInvalidRange
public static void throwInvalidRange(String expected, int actual)
Throw an IllegalArgument exception describing a range violation.- Parameters:
expected
- String describing expected rangesactual
- Actual observed value- Throws:
IllegalArgumentException
- always
-
throwInvalidRange
public static void throwInvalidRange(String expected, long actual)
Throw an IllegalArgument exception describing a range violation.- Parameters:
expected
- String describing expected rangesactual
- Actual observed value- Throws:
IllegalArgumentException
- always
-
throwInvalidRange
public static void throwInvalidRange(Object[] expected, Object actual)
Throw an IllegalArgument exception describing a range violation.- Parameters:
expected
- Objects describing expected rangesactual
- Actual observed byte array- Throws:
IllegalArgumentException
- always
-
throwInvalidRangeUnsigned
public static void throwInvalidRangeUnsigned(String expected, long actual)
Throw an IllegalArgument exception describing a range violation of an Uint64 type.- Parameters:
expected
- String describing expected rangesactual
- Actual observed value- Throws:
IllegalArgumentException
- always
-
nonnull
public static <T> @NonNull List<T> nonnull(@Nullable List<T> input)
Check whether specified List is null and if so return an immutable list instead. This method supports non-null default getter methods.- Parameters:
input
- input list, may be null- Returns:
- Input list or an empty list.
-
wrapperHashCode
public static int wrapperHashCode(@Nullable Object obj)
Return hash code of a single-property wrapper class. Since the wrapper is not null, we really want to discern this object being present, henceObject.hashCode()
is not really useful we would end up with0
for both non-present and present-with-null objects.- Parameters:
obj
- Internal object to hash- Returns:
- Wrapper object hash code
-
wrapperHashCode
public static int wrapperHashCode(byte @Nullable [] obj)
Return hash code of a single-property wrapper class. Since the wrapper is not null, we really want to discern this object being present, henceObject.hashCode()
is not really useful we would end up with0
for both non-present and present-with-null objects.- Parameters:
obj
- Internal object to hash- Returns:
- Wrapper object hash code
-
compatUint
@Deprecated public static @Nullable Uint8 compatUint(@Nullable Short value)
Deprecated.This method is provided for migration purposes only, do not use it outside of deprecated compatibility methods.- Parameters:
value
- Legacy value- Returns:
- Converted value
- Throws:
IllegalArgumentException
- if the value does not fit an Uint8
-
compatUint
@Deprecated public static @Nullable Uint16 compatUint(@Nullable Integer value)
Deprecated.This method is provided for migration purposes only, do not use it outside of deprecated compatibility methods.- Parameters:
value
- Legacy value- Returns:
- Converted value
- Throws:
IllegalArgumentException
- if the value does not fit an Uint16
-
compatUint
@Deprecated public static @Nullable Uint32 compatUint(@Nullable Long value)
Deprecated.This method is provided for migration purposes only, do not use it outside of deprecated compatibility methods.- Parameters:
value
- Legacy value- Returns:
- Converted value
- Throws:
IllegalArgumentException
- if the value does not fit an Uint32
-
compatUint
@Deprecated public static @Nullable Uint64 compatUint(@Nullable BigInteger value)
Deprecated.This method is provided for migration purposes only, do not use it outside of deprecated compatibility methods.- Parameters:
value
- Legacy value- Returns:
- Converted value
- Throws:
IllegalArgumentException
- if the value does not fit an Uint64
-
-