Class CodeHelpers

java.lang.Object
org.opendaylight.yangtools.binding.lib.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.
  • Method Details

    • validValue

      public static void validValue(boolean expression, Object value, String options)
      Require that an a value-related expression is true.
      Parameters:
      expression - Expression to evaluate
      value - Value being validated
      options - Valid value options checked
      Throws:
      IllegalArgumentException - if expression is false
    • require

      public static <T> @NonNull T require(@Nullable T value, @NonNull String name)
      Return value and check whether specified value is null and if so throws exception. This method supports require default getter methods.
      Parameters:
      value - Value itself
      name - Name of the value
      Returns:
      Non-null value
      Throws:
      NoSuchElementException - if value is null
    • requireKeyProp

      public static <T> @NonNull T requireKeyProp(@Nullable T value, @NonNull String name)
      A shortcut for Preconditions.checkNotNull(value, "Key component \"%s\" must not be null", name).
      Parameters:
      value - Value itself
      name - Name of the value
      Returns:
      Non-null value
      Throws:
      NullPointerException - if value is null
    • requireValue

      public static void requireValue(@Nullable Object value)
      A shortcut for Objects.requireNonNull(value, "Supplied value may not be null").
      Parameters:
      value - Value itself
      Throws:
      NullPointerException - if value is null
    • appendBit

      public static void appendBit(MoreObjects.ToStringHelper helper, @NonNull String name, boolean value)
      Append a bits individual value. If the value is false, this method does nothing.
      Parameters:
      helper - Helper to append to
      name - Name of the bit
      value - Value to append
      Throws:
      NullPointerException - if any argument 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 to
      name - Name of the value
      value - 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 to
      name - Name of the value
      value - Value to append
      Throws:
      NullPointerException - if the name or helper is null
    • appendAugmentations

      public static void appendAugmentations(MoreObjects.ToStringHelper helper, String name, Augmentable<?> augmentable)
      Append augmentation map of an Augmentable to a ToStringHelper. If augmentations are null or empty, this method does nothing.
      Parameters:
      helper - Helper to append to
      name - Name of the augmentation value
      augmentable - Augmentable object to
      Throws:
      NullPointerException - if any argument 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 null
      VerifyException - 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 enforcement Pattern which may reflect negation.
      Parameters:
      value - Value to be checked.
      pattern - Enforcement pattern
      regex - Source regular expression, as defined in YANG model
      Throws:
      IllegalArgumentException - if the value does not match the pattern
      NullPointerException - if any of the arguments is 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 enforcement Pattern which may reflect negation.
      Parameters:
      value - Value to be checked.
      patterns - Enforcement patterns
      regexes - Source regular expression, as defined in YANG model. Size and order must match patterns.
      Throws:
      IllegalArgumentException - if the value does not match the pattern
      NullPointerException - if any of the arguments is null
      VerifyException - 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 lengths
      actual - 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 lengths
      actual - 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 ranges
      actual - 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 ranges
      actual - 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 ranges
      actual - 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 ranges
      actual - 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 ranges
      actual - 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.
      Type Parameters:
      T - list element type
      Parameters:
      input - input list, may be null
      Returns:
      Input list or an empty list.
    • nonnull

      public static <K, V> @NonNull Map<K,V> nonnull(@Nullable Map<K,V> input)
      Check whether specified Map is null and if so return an immutable map instead. This method supports non-null default getter methods.
      Type Parameters:
      K - key type
      V - value type
      Parameters:
      input - input map, may be null
      Returns:
      Input map or an empty map.
    • emptyToNull

      public static <T> @Nullable List<T> emptyToNull(@Nullable List<T> input)
      Check whether specified List is empty and if so return null, otherwise return input list. This method supports Builder/implementation list handover.
      Type Parameters:
      T - list element type
      Parameters:
      input - input list, may be null
      Returns:
      Input list or null.
    • emptyToNull

      public static <K, V> @Nullable Map<K,V> emptyToNull(@Nullable Map<K,V> input)
      Check whether specified Map is empty and if so return null, otherwise return input map. This method supports Builder/implementation list handover.
      Type Parameters:
      K - key type
      V - value type
      Parameters:
      input - input map, may be null
      Returns:
      Input map or null.
    • 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, hence Object.hashCode() is not really useful we would end up with 0 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, hence Object.hashCode() is not really useful we would end up with 0 for both non-present and present-with-null objects.
      Parameters:
      obj - Internal object to hash
      Returns:
      Wrapper object hash code
    • checkEnum

      public static <T extends EnumTypeObject> @NonNull T checkEnum(@Nullable T obj, String name)
      Check that the specified EnumTypeObject object is not null. This method is meant to be used with ofName(String) and ofValue(int) static factory methods.
      Parameters:
      obj - enumeration object, possibly null
      name - User-supplied enumeration name
      Returns:
      Enumeration object
      Throws:
      IllegalArgumentException - if obj is null
    • checkEnum

      public static <T extends EnumTypeObject> @NonNull T checkEnum(@Nullable T obj, int value)
      Check that the specified EnumTypeObject object is not null. This method is meant to be used with ofName(String) and ofValue(int) static factory methods.
      Parameters:
      obj - enumeration object, possibly null
      value - User-supplied enumeration value
      Returns:
      Enumeration object
      Throws:
      IllegalArgumentException - if obj is null
    • 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 compatible BindingContract.
      Parameters:
      requiredClass - Required BindingContract class
      obj - Object to check, may be null
      Returns:
      Object cast to required class, if its implemented class matches requirement, null otherwise
      Throws:
      NullPointerException - if requiredClass is null
    • 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 class
      fieldName - name of the field being filled
      obj - Object to check, may be null
      Returns:
      Object cast to required class, if its class matches requirement, or null
      Throws:
      IllegalArgumentException - if obj is not an instance of requiredClass
      NullPointerException - if requiredClass or fieldName is null
    • 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 class
      fieldName - name of the field being filled
      list - List, which items should be checked
      Returns:
      Type-checked List
      Throws:
      IllegalArgumentException - if a list item is not instance of requiredClass
      NullPointerException - if requiredClass or fieldName is null
    • 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 class
      fieldName - name of the field being filled
      set - Set, which items should be checked
      Returns:
      Type-checked Set
      Throws:
      IllegalArgumentException - if a set item is not instance of requiredItemClass
      NullPointerException - if requiredClass or fieldName is null