Package io.nem.symbol.catapult.builders
Class GeneratorUtils
- java.lang.Object
-
- io.nem.symbol.catapult.builders.GeneratorUtils
-
public final class GeneratorUtils extends java.lang.ObjectGenerator utility class.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceGeneratorUtils.ThrowingConsumer<T,E extends java.lang.Exception>Throwing consumer interface.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidaddPadding(int size, java.io.DataOutputStream dataOutputStream)This method writes 0 into the dataOutputStream.static <E extends java.lang.RuntimeException>
java.lang.RuntimeExceptiongetExceptionToPropagate(java.lang.Exception exception)Gets a runtime exception to propagates from an exception.static <E extends java.lang.RuntimeException>
java.lang.RuntimeExceptiongetExceptionToPropagate(java.lang.Exception exception, java.util.function.Function<java.lang.Exception,E> wrap)Gets a runtime exception to propagates from an exception.static intgetPadding(int size)It calcualtes the padding that needs to be added/skipped when processing inner transactions.static intgetSize(java.nio.ByteBuffer buffer)Returns the size of the buffer.static intgetSize(java.util.Collection<?> collection)Returns the size of the collectionstatic byte[]hexToBytes(java.lang.String hex)Basic from hex to byte array function.static voidisFalse(boolean expression, java.lang.String message, java.lang.Object... values)Throws if the value is not false.static voidisTrue(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 builderstatic <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> voidnotNull(T object, java.lang.String message, java.lang.Object... values)Throws if the object is null.static <T> Tpropagate(java.util.concurrent.Callable<T> callable)Propagates checked exceptions as a runtime exception.static <T,E extends java.lang.RuntimeException>
Tpropagate(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.ByteBufferreadByteBuffer(java.io.DataInputStream stream, int size)Read aByteBufferof the given size form the stremstatic byte[]serialize(GeneratorUtils.ThrowingConsumer<java.io.DataOutputStream,java.lang.Exception> consumer)Serializes data using a helper function to write to the stream.static voidskipPadding(int size, java.io.DataInputStream dataInputStream)It moves the output stream pointer the padding size calculated from the payload sizestatic java.lang.StringtoHex(byte[] bytes)Basic to hex function that converts a byte array to an hexstatic <T extends java.lang.Enum<T> & BitMaskable>
longtoLong(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 inttoUnsignedInt(byte value)Converts to an int by an unsigned conversion.static inttoUnsignedInt(short value)Converts to an int by an unsigned conversion.static <T extends Serializer>
TwriteBuilderToFile(T builder, java.lang.String file)It writes the builder into a file for future unit testing.static voidwriteEntity(java.io.DataOutputStream dataOutputStream, Serializer entity)Write a serializer into the writer.static voidwriteList(java.io.DataOutputStream dataOutputStream, java.util.List<? extends Serializer> entities)Write a list of catbuffer entities into the writer.
-
-
-
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 paddingdataInputStream- 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 paddingdataOutputStream- 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 builderstream- the streamcount- 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 builderstream- the stream to read frompayloadSize- 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.IOExceptionWrite a list of catbuffer entities into the writer.- Parameters:
dataOutputStream- the stream to serialize intoentities- 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.IOExceptionWrite a serializer into the writer.- Parameters:
dataOutputStream- the stream to serialize intoentity- 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.IOExceptionRead aByteBufferof the given size form the strem- Parameters:
stream- the streamsize- 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.
-
toHex
public static java.lang.String toHex(byte[] bytes)
Basic to hex function that converts a byte array to an hex- Parameters:
bytes- the bytes- Returns:
- the hex representation.
-
hexToBytes
public static byte[] hexToBytes(java.lang.String hex)
Basic from hex to byte array function.- Parameters:
hex- the hex string- Returns:
- the byte array.
-
writeBuilderToFile
public static <T extends Serializer> T writeBuilderToFile(T builder, java.lang.String file)
It writes the builder into a file for future unit testing.- Type Parameters:
T- the type of the builder.- Parameters:
builder- the builder.file- the file to append.- Returns:
- the builder
-
-