Class GeneratorUtils


  • public final class GeneratorUtils
    extends java.lang.Object
    Generator utility class.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static interface  GeneratorUtils.ThrowingConsumer<T,​E extends java.lang.Exception>
      Throwing consumer interface.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void addPadding​(int size, java.io.DataOutputStream dataOutputStream)
      This method writes 0 into the dataOutputStream.
      static <E extends java.lang.RuntimeException>
      java.lang.RuntimeException
      getExceptionToPropagate​(java.lang.Exception exception)
      Gets a runtime exception to propagates from an exception.
      static <E extends java.lang.RuntimeException>
      java.lang.RuntimeException
      getExceptionToPropagate​(java.lang.Exception exception, java.util.function.Function<java.lang.Exception,​E> wrap)
      Gets a runtime exception to propagates from an exception.
      static int getPadding​(int size)
      It calcualtes the padding that needs to be added/skipped when processing inner transactions.
      static int getSize​(java.nio.ByteBuffer buffer)
      Returns the size of the buffer.
      static int getSize​(java.util.Collection<?> collection)
      Returns the size of the collection
      static void isFalse​(boolean expression, java.lang.String message, java.lang.Object... values)
      Throws if the value is not false.
      static void isTrue​(boolean expression, java.lang.String message, java.lang.Object... values)
      Throws if the value is not true.
      static <T> java.util.List<T> loadFromBinaryArray​(java.util.function.Function<java.io.DataInputStream,​T> builder, java.io.DataInputStream stream, long count)
      It reads count elements from the stream and creates a list using the builder
      static <T extends Serializer>
      java.util.List<T>
      loadFromBinaryArrayRemaining​(java.util.function.Function<java.io.DataInputStream,​T> builder, java.io.DataInputStream stream, int payloadSize)
      It reads all the remaining entities using the total payload size.
      static <T> void notNull​(T object, java.lang.String message, java.lang.Object... values)
      Throws if the object is null.
      static <T> T propagate​(java.util.concurrent.Callable<T> callable)
      Propagates checked exceptions as a runtime exception.
      static <T,​E extends java.lang.RuntimeException>
      T
      propagate​(java.util.concurrent.Callable<T> callable, java.util.function.Function<java.lang.Exception,​E> wrap)
      Propagates checked exceptions as a specific runtime exception.
      static java.nio.ByteBuffer readByteBuffer​(java.io.DataInputStream stream, int size)
      Read a ByteBuffer of the given size form the strem
      static byte[] serialize​(GeneratorUtils.ThrowingConsumer<java.io.DataOutputStream,​java.lang.Exception> consumer)
      Serializes data using a helper function to write to the stream.
      static void skipPadding​(int size, java.io.DataInputStream dataInputStream)
      It moves the output stream pointer the padding size calculated from the payload size
      static <T extends java.lang.Enum<T> & BitMaskable>
      long
      toLong​(java.lang.Class<T> enumClass, java.util.Set<T> enumSet)
      Creates a bitwise representation for an Set.
      static <T extends java.lang.Enum<T> & BitMaskable>
      java.util.EnumSet<T>
      toSet​(java.lang.Class<T> enumClass, long bitMaskValue)
      Creates a EnumSet from from a bit representation.
      static int toUnsignedInt​(byte value)
      Converts to an int by an unsigned conversion.
      static int toUnsignedInt​(short value)
      Converts to an int by an unsigned conversion.
      static void writeEntity​(java.io.DataOutputStream dataOutputStream, Serializer entity)
      Write a serializer into the writer.
      static void writeList​(java.io.DataOutputStream dataOutputStream, java.util.List<? extends Serializer> entities)
      Write a list of catbuffer entities into the writer.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • notNull

        public static <T> void notNull​(T object,
                                       java.lang.String message,
                                       java.lang.Object... values)
        Throws if the object is null.
        Type Parameters:
        T - Type of object.
        Parameters:
        object - Object to to check.
        message - Format string message.
        values - Format values.
      • isTrue

        public static void isTrue​(boolean expression,
                                  java.lang.String message,
                                  java.lang.Object... values)
        Throws if the value is not true.
        Parameters:
        expression - Expression to check.
        message - Format string message.
        values - Format values.
      • isFalse

        public static void isFalse​(boolean expression,
                                   java.lang.String message,
                                   java.lang.Object... values)
        Throws if the value is not false.
        Parameters:
        expression - Expression to check.
        message - Format string message.
        values - Format values.
      • toUnsignedInt

        public static int toUnsignedInt​(byte value)
        Converts to an int by an unsigned conversion.
        Parameters:
        value - Signed byte.
        Returns:
        Positive integer.
      • toUnsignedInt

        public static int toUnsignedInt​(short value)
        Converts to an int by an unsigned conversion.
        Parameters:
        value - Signed short.
        Returns:
        Positive integer.
      • toLong

        public static <T extends java.lang.Enum<T> & BitMaskable> long toLong​(java.lang.Class<T> enumClass,
                                                                              java.util.Set<T> enumSet)
        Creates a bitwise representation for an Set.
        Type Parameters:
        T - Type of enum.
        Parameters:
        enumClass - Enum type.
        enumSet - EnumSet to convert to bit representation.
        Returns:
        Long value of the EnumSet.
      • toSet

        public static <T extends java.lang.Enum<T> & BitMaskable> java.util.EnumSet<T> toSet​(java.lang.Class<T> enumClass,
                                                                                             long bitMaskValue)
        Creates a EnumSet from from a bit representation.
        Type Parameters:
        T - Enum type.
        Parameters:
        enumClass - Enum class.
        bitMaskValue - Bitmask value.
        Returns:
        EnumSet representing the long value.
      • getExceptionToPropagate

        public static <E extends java.lang.RuntimeException> java.lang.RuntimeException getExceptionToPropagate​(java.lang.Exception exception,
                                                                                                                java.util.function.Function<java.lang.Exception,​E> wrap)
        Gets a runtime exception to propagates from an exception.
        Type Parameters:
        E - Specific exception type.
        Parameters:
        exception - Exception to propagate.
        wrap - Function that wraps an exception in a runtime exception.
        Returns:
        RuntimeException to throw.
      • getExceptionToPropagate

        public static <E extends java.lang.RuntimeException> java.lang.RuntimeException getExceptionToPropagate​(java.lang.Exception exception)
        Gets a runtime exception to propagates from an exception.
        Type Parameters:
        E - Specific exception type.
        Parameters:
        exception - Exception to propagate.
        Returns:
        RuntimeException to throw.
      • propagate

        public static <T,​E extends java.lang.RuntimeException> T propagate​(java.util.concurrent.Callable<T> callable,
                                                                                 java.util.function.Function<java.lang.Exception,​E> wrap)
        Propagates checked exceptions as a specific runtime exception.
        Type Parameters:
        T - Return type.
        E - Specific exception type.
        Parameters:
        callable - Function to call.
        wrap - Function that wraps an exception in a runtime exception.
        Returns:
        Function result.
      • propagate

        public static <T> T propagate​(java.util.concurrent.Callable<T> callable)
        Propagates checked exceptions as a runtime exception.
        Type Parameters:
        T - Function return type.
        Parameters:
        callable - Function to call.
        Returns:
        Function result.
      • serialize

        public static byte[] serialize​(GeneratorUtils.ThrowingConsumer<java.io.DataOutputStream,​java.lang.Exception> consumer)
        Serializes data using a helper function to write to the stream.
        Parameters:
        consumer - Helper function that writes data to DataOutputStream.
        Returns:
        Byte array of data written.
      • skipPadding

        public static void skipPadding​(int size,
                                       java.io.DataInputStream dataInputStream)
        It moves the output stream pointer the padding size calculated from the payload size
        Parameters:
        size - the payload size used to calcualted the padding
        dataInputStream - the input stream that will be moved the calcauted padding size
      • addPadding

        public static void addPadding​(int size,
                                      java.io.DataOutputStream dataOutputStream)
        This method writes 0 into the dataOutputStream. The amount of 0s is the calculated padding size from provided payload size.
        Parameters:
        size - the payload size used to calcualted the padding
        dataOutputStream - used to write the 0s.
      • getPadding

        public static int getPadding​(int size)
        It calcualtes the padding that needs to be added/skipped when processing inner transactions.
        Parameters:
        size - the size of the payload using to calculate the padding
        Returns:
        the padding to be added/skipped.
      • loadFromBinaryArray

        public static <T> java.util.List<T> loadFromBinaryArray​(java.util.function.Function<java.io.DataInputStream,​T> builder,
                                                                java.io.DataInputStream stream,
                                                                long count)
        It reads count elements from the stream and creates a list using the builder
        Type Parameters:
        T - the the type to be returned
        Parameters:
        builder - the builder
        stream - the stream
        count - the elements to be read
        Returns:
        a list of T.
      • loadFromBinaryArrayRemaining

        public static <T extends Serializer> java.util.List<T> loadFromBinaryArrayRemaining​(java.util.function.Function<java.io.DataInputStream,​T> builder,
                                                                                            java.io.DataInputStream stream,
                                                                                            int payloadSize)
                                                                                     throws java.io.IOException
        It reads all the remaining entities using the total payload size.
        Type Parameters:
        T - the type of the entity
        Parameters:
        builder - the entity builder
        stream - the stream to read from
        payloadSize - the payload size
        Returns:
        a list of entities
        Throws:
        java.io.IOException - when data cannot be loaded.
      • writeList

        public static void writeList​(java.io.DataOutputStream dataOutputStream,
                                     java.util.List<? extends Serializer> entities)
                              throws java.io.IOException
        Write a list of catbuffer entities into the writer.
        Parameters:
        dataOutputStream - the stream to serialize into
        entities - the entities to be serialized
        Throws:
        java.io.IOException - when data cannot be written.
      • writeEntity

        public static void writeEntity​(java.io.DataOutputStream dataOutputStream,
                                       Serializer entity)
                                throws java.io.IOException
        Write a serializer into the writer.
        Parameters:
        dataOutputStream - the stream to serialize into
        entity - the entities to be serialized
        Throws:
        java.io.IOException - when data cannot be written.
      • readByteBuffer

        public static java.nio.ByteBuffer readByteBuffer​(java.io.DataInputStream stream,
                                                         int size)
                                                  throws java.io.IOException
        Read a ByteBuffer of the given size form the strem
        Parameters:
        stream - the stream
        size - the size of the buffer to read
        Returns:
        the buffer
        Throws:
        java.io.IOException - when data cannot be read
      • getSize

        public static int getSize​(java.nio.ByteBuffer buffer)
        Returns the size of the buffer.
        Parameters:
        buffer - the buffer
        Returns:
        its size
      • getSize

        public static int getSize​(java.util.Collection<?> collection)
        Returns the size of the collection
        Parameters:
        collection - the collecion
        Returns:
        the size.