Class 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 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 evaluate
        value - Value being validated
        options - 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 to Objects.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 itself
        name - Symbolic name
        Returns:
        non-null value
        Throws:
        IllegalArgumentException - if value is null
        NullPointerException - if name 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
      • 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
      • 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 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 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 are 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.
        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, 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
      • 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.
        Compatibility utility for converting a legacy Short uint8 value to its Uint8 counterpart.
        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.
        Compatibility utility for converting a legacy Integer uint16 value to its Uint16 counterpart.
        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.
        Compatibility utility for converting a legacy Long uint32 value to its Uint32 counterpart.
        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.
        Compatibility utility for converting a legacy BigInteger uint64 value to its Uint64 counterpart.
        Parameters:
        value - Legacy value
        Returns:
        Converted value
        Throws:
        IllegalArgumentException - if the value does not fit an Uint64