Class CodeHelpers
java.lang.Object
org.opendaylight.yangtools.binding.lib.CodeHelpers
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.
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
appendAugmentations
(MoreObjects.ToStringHelper helper, String name, Augmentable<?> augmentable) Append augmentation map of an Augmentable to a ToStringHelper.static void
appendBit
(MoreObjects.ToStringHelper helper, @NonNull String name, boolean value) Append abits
individual value.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 <T extends BindingContract<?>>
@Nullable TUtility method for checking whether a target object is a compatibleBindingContract
.static <T extends EnumTypeObject>
@NonNull TcheckEnum
(@Nullable T obj, int value) Check that the specifiedEnumTypeObject
object is notnull
.static <T extends EnumTypeObject>
@NonNull TCheck that the specifiedEnumTypeObject
object is notnull
.static <T> @Nullable T
checkFieldCast
(@NonNull Class<T> requiredClass, @NonNull String fieldName, @Nullable Object obj) Utility method for checking whether a target object is compatible.static <T> @Nullable List
<T> checkListFieldCast
(@NonNull Class<T> requiredClass, @NonNull String fieldName, @Nullable List<?> list) Utility method for checking whether the items of target list is compatible.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 <T> @Nullable Set
<T> checkSetFieldCast
(@NonNull Class<T> requiredClass, @NonNull String fieldName, @Nullable Set<?> set) Utility method for checking whether the items of target set is compatible.static Pattern @NonNull []
compilePatterns
(@NonNull List<String> patterns) Compile a list of pattern regular expressions and return them as an array.static <T> @Nullable List
<T> emptyToNull
(@Nullable List<T> input) Check whether specified List is empty and if so returnnull
, otherwise return input list.static <K,
V> @Nullable Map <K, V> emptyToNull
(@Nullable Map<K, V> input) Check whether specified Map is empty and if so returnnull
, otherwise return input map.static <T> @NonNull List
<T> Check whether specified List isnull
and if so return an immutable list instead.static <K,
V> @NonNull Map <K, V> Check whether specified Map isnull
and if so return an immutable map instead.static <T> @NonNull T
Return value and check whether specified value isnull
and if so throws exception.static <T> @NonNull T
requireKeyProp
(@Nullable T value, @NonNull String name) A shortcut forPreconditions.checkNotNull(value, "Key component \"%s\" must not be null", name)
.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 Details
-
validValue
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
-
require
Return value and check whether specified value isnull
and if so throws exception. This method supports require default getter methods.- Parameters:
value
- Value itselfname
- Name of the value- Returns:
- Non-null value
- Throws:
NoSuchElementException
- if value isnull
-
requireKeyProp
A shortcut forPreconditions.checkNotNull(value, "Key component \"%s\" must not be null", name)
.- Parameters:
value
- Value itselfname
- Name of the value- Returns:
- Non-null value
- Throws:
NullPointerException
- if value isnull
-
requireValue
A shortcut forObjects.requireNonNull(value, "Supplied value may not be null")
.- Parameters:
value
- Value itself- Throws:
NullPointerException
- if value isnull
-
appendBit
public static void appendBit(MoreObjects.ToStringHelper helper, @NonNull String name, boolean value) Append abits
individual value. If the value isfalse
, this method does nothing.- Parameters:
helper
- Helper to append toname
- Name of the bitvalue
- Value to append- Throws:
NullPointerException
- if any argument isnull
-
appendValue
public static void appendValue(MoreObjects.ToStringHelper helper, @NonNull String name, @Nullable Object value) Append a named value to a ToStringHelper. If the value isnull
, this method does nothing.- Parameters:
helper
- Helper to append toname
- Name of the valuevalue
- Value to append- Throws:
NullPointerException
- if the name or helper isnull
-
appendValue
Append a named value to a ToStringHelper. If the value isnull
, this method does nothing.- Parameters:
helper
- Helper to append toname
- Name of the valuevalue
- Value to append- Throws:
NullPointerException
- if the name or helper isnull
-
appendAugmentations
public static void appendAugmentations(MoreObjects.ToStringHelper helper, String name, Augmentable<?> augmentable) Append augmentation map of an Augmentable to a ToStringHelper. If augmentations arenull
or empty, this method does nothing.- Parameters:
helper
- Helper to append toname
- Name of the augmentation valueaugmentable
- Augmentable object to- Throws:
NullPointerException
- if any argument isnull
-
compilePatterns
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 isnull
VerifyException
- if the list has fewer than two elements
-
checkPattern
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 isnull
-
checkPattern
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 isnull
VerifyException
- if the size of patterns and regexes does not match
-
throwInvalidLength
Throw an IllegalArgument exception describing a length violation.- Parameters:
expected
- String describing expected lengthsactual
- Actual observed object- Throws:
IllegalArgumentException
- always
-
throwInvalidLength
Throw an IllegalArgument exception describing a length violation.- Parameters:
expected
- String describing expected lengthsactual
- Actual observed byte array- Throws:
IllegalArgumentException
- always
-
throwInvalidRange
Throw an IllegalArgument exception describing a range violation.- Parameters:
expected
- String describing expected rangesactual
- Actual observed object- Throws:
IllegalArgumentException
- always
-
throwInvalidRange
Throw an IllegalArgument exception describing a range violation.- Parameters:
expected
- String describing expected rangesactual
- Actual observed value- Throws:
IllegalArgumentException
- always
-
throwInvalidRange
Throw an IllegalArgument exception describing a range violation.- Parameters:
expected
- String describing expected rangesactual
- Actual observed value- Throws:
IllegalArgumentException
- always
-
throwInvalidRange
Throw an IllegalArgument exception describing a range violation.- Parameters:
expected
- Objects describing expected rangesactual
- Actual observed byte array- Throws:
IllegalArgumentException
- always
-
throwInvalidRangeUnsigned
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
Check whether specified List isnull
and if so return an immutable list instead. This method supports non-null default getter methods.- Type Parameters:
T
- list element type- Parameters:
input
- input list, may benull
- Returns:
- Input list or an empty list.
-
nonnull
Check whether specified Map isnull
and if so return an immutable map instead. This method supports non-null default getter methods.- Type Parameters:
K
- key typeV
- value type- Parameters:
input
- input map, may benull
- Returns:
- Input map or an empty map.
-
emptyToNull
Check whether specified List is empty and if so returnnull
, otherwise return input list. This method supports Builder/implementation list handover.- Type Parameters:
T
- list element type- Parameters:
input
- input list, may benull
- Returns:
- Input list or
null
.
-
emptyToNull
Check whether specified Map is empty and if so returnnull
, otherwise return input map. This method supports Builder/implementation list handover.- Type Parameters:
K
- key typeV
- value type- Parameters:
input
- input map, may benull
- Returns:
- Input map or
null
.
-
wrapperHashCode
Return hash code of a single-property wrapper class. Since the wrapper is notnull
, 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 notnull
, 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
-
checkEnum
Check that the specifiedEnumTypeObject
object is notnull
. This method is meant to be used withofName(String)
andofValue(int)
static factory methods.- Parameters:
obj
- enumeration object, possiblynull
name
- User-supplied enumeration name- Returns:
- Enumeration object
- Throws:
IllegalArgumentException
- ifobj
isnull
-
checkEnum
Check that the specifiedEnumTypeObject
object is notnull
. This method is meant to be used withofName(String)
andofValue(int)
static factory methods.- Parameters:
obj
- enumeration object, possiblynull
value
- User-supplied enumeration value- Returns:
- Enumeration object
- Throws:
IllegalArgumentException
- ifobj
isnull
-
checkCast
public static <T extends BindingContract<?>> @Nullable T checkCast(@NonNull Class<T> requiredClass, @Nullable Object obj) Utility method for checking whether a target object is a compatibleBindingContract
.- Parameters:
requiredClass
- Required BindingContract classobj
- Object to check, may benull
- Returns:
- Object cast to required class, if its implemented class matches requirement,
null
otherwise - Throws:
NullPointerException
- ifrequiredClass
isnull
-
checkFieldCast
public static <T> @Nullable T checkFieldCast(@NonNull Class<T> requiredClass, @NonNull String fieldName, @Nullable Object obj) Utility method for checking whether a target object is compatible.- Parameters:
requiredClass
- Required classfieldName
- name of the field being filledobj
- Object to check, may benull
- Returns:
- Object cast to required class, if its class matches requirement, or
null
- Throws:
IllegalArgumentException
- ifobj
is not an instance ofrequiredClass
NullPointerException
- ifrequiredClass
orfieldName
isnull
-
checkListFieldCast
public static <T> @Nullable List<T> checkListFieldCast(@NonNull Class<T> requiredClass, @NonNull String fieldName, @Nullable List<?> list) Utility method for checking whether the items of target list is compatible.- Parameters:
requiredClass
- Required item classfieldName
- name of the field being filledlist
- List, which items should be checked- Returns:
- Type-checked List
- Throws:
IllegalArgumentException
- if a list item is not instance ofrequiredClass
NullPointerException
- ifrequiredClass
orfieldName
isnull
-
checkSetFieldCast
public static <T> @Nullable Set<T> checkSetFieldCast(@NonNull Class<T> requiredClass, @NonNull String fieldName, @Nullable Set<?> set) Utility method for checking whether the items of target set is compatible.- Parameters:
requiredClass
- Required item classfieldName
- name of the field being filledset
- Set, which items should be checked- Returns:
- Type-checked Set
- Throws:
IllegalArgumentException
- if a set item is not instance ofrequiredItemClass
NullPointerException
- ifrequiredClass
orfieldName
isnull
-