Class MessageUtil
- java.lang.Object
-
- net.morimekta.providence.util.MessageUtil
-
public class MessageUtil extends java.lang.ObjectConvenience methods for handling providence messages.
-
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static java.util.Optional<java.lang.Object>coerce(PDescriptor valueType, java.lang.Object value)Coerce value to match the given type descriptor.static java.util.Optional<java.lang.Object>coerceStrict(PDescriptor valueType, java.lang.Object value)Coerce value to match the given type descriptor using struct type checking.static <T> java.util.Optional<T>getInMessage(PMessageOrBuilder<?> message, PField<?>... fields)Look up a key in the message structure.static <M extends PMessage<M>,B extends PMessageBuilder<M>>
BgetTargetModifications(PMessageOrBuilder<M> source, PMessageOrBuilder<M> target)Make a builder of the target message with all differences between source and target marked as modifications.static java.lang.StringkeyPath(PField<?>... fields)Append field to the given path.static java.lang.StringkeyPathAppend(java.lang.String path, PField<?> field)Append field to the given path.static PField<?>[]keyPathToFields(PMessageDescriptor<?> rootDescriptor, java.lang.String key)Convert a key path to a list of consecutive fields for recursive lookup.static java.util.Map<java.lang.String,java.lang.Object>messageToMap(PMessageOrBuilder<?> message)Transform a message into a native map structure.static <T> java.util.Optional<T>optionalInMessage(PMessageOrBuilder<?> message, PField<?>... fields)Get a field value from a message with optional chaining.static <M extends PMessage<M>,V extends PMessageOrBuilder<M>,B extends PMessageBuilder<M>>
java.util.List<B>toBuilderAll(java.util.Collection<V> messages)Mutate all items of the collection containing messages.static <M extends PMessage<M>,B extends PMessageBuilder<M>>
BtoBuilderIfNonNull(PMessageOrBuilder<M> mob)Mutate the message if it is not null.static <K,M extends PMessage<M>,V extends PMessageOrBuilder<M>,B extends PMessageBuilder<M>>
java.util.Map<K,B>toBuilderValues(java.util.Map<K,V> messages)Mutate all values of the map containing message-or-builder values.static java.util.Map<java.lang.String,java.lang.Object>toMap(PMessageOrBuilder<?> message)Deprecated.static <M extends PMessage<M>,V extends PMessageOrBuilder<M>>
java.util.List<M>toMessageAll(java.util.Collection<V> builders)Build all items of the collection containing message-or-builders.static <M extends PMessage<M>>
MtoMessageIfNotNull(PMessageOrBuilder<M> mob)Build the message from builder if it is not null.static <M extends PMessage<M>,MB extends PMessageOrBuilder<M>>
java.util.Collection<PMessageOrBuilder<M>>toMessageOrBuilders(java.util.Collection<MB> items)Casting utility to make into a collection of message-or-builders.static <K,M extends PMessage<M>,MB extends PMessageOrBuilder<M>>
java.util.Map<K,PMessageOrBuilder<M>>toMessageOrBuilderValues(java.util.Map<K,MB> items)Casting utility to make into a map of message-or-builders.static <K,M extends PMessage<M>,V extends PMessageOrBuilder<M>>
java.util.Map<K,M>toMessageValues(java.util.Map<K,V> messages)Mutate all items of the collection containing messages.
-
-
-
Method Detail
-
toMessageAll
public static <M extends PMessage<M>,V extends PMessageOrBuilder<M>> java.util.List<M> toMessageAll(java.util.Collection<V> builders)
Build all items of the collection containing message-or-builders. The list must not contain any null items.- Type Parameters:
M- The message type.V- The actual value type.- Parameters:
builders- List of message-or-builders.- Returns:
- List of messages or null if null input.
-
toBuilderAll
public static <M extends PMessage<M>,V extends PMessageOrBuilder<M>,B extends PMessageBuilder<M>> java.util.List<B> toBuilderAll(java.util.Collection<V> messages)
Mutate all items of the collection containing messages. The list must not contain any null items.- Type Parameters:
M- The message type.V- The actual value type.B- The builder type.- Parameters:
messages- List of messages- Returns:
- List of builders or null if null input.
-
toBuilderValues
public static <K,M extends PMessage<M>,V extends PMessageOrBuilder<M>,B extends PMessageBuilder<M>> java.util.Map<K,B> toBuilderValues(java.util.Map<K,V> messages)
Mutate all values of the map containing message-or-builder values. The map must not contain any null items.- Type Parameters:
K- The map key type.M- The message type.V- The actual value type.B- The builder type.- Parameters:
messages- Map with message-or-builder values.- Returns:
- Map with builder values or null on null input.
-
toMessageValues
public static <K,M extends PMessage<M>,V extends PMessageOrBuilder<M>> java.util.Map<K,M> toMessageValues(java.util.Map<K,V> messages)
Mutate all items of the collection containing messages. The list must not contain any null items.- Type Parameters:
K- The map key type.M- The message type.V- The actual value type.- Parameters:
messages- List of messages- Returns:
- Map with message values, or null on null input.
-
toMessageIfNotNull
public static <M extends PMessage<M>> M toMessageIfNotNull(PMessageOrBuilder<M> mob)
Build the message from builder if it is not null.- Type Parameters:
M- The message type.- Parameters:
mob- The builder to build.- Returns:
- The message or null if null input.
-
toBuilderIfNonNull
public static <M extends PMessage<M>,B extends PMessageBuilder<M>> B toBuilderIfNonNull(PMessageOrBuilder<M> mob)
Mutate the message if it is not null.- Type Parameters:
M- The message type.B- The builder type.- Parameters:
mob- Message or builder to mutate.- Returns:
- The builder or null if null input.
-
toMessageOrBuilders
public static <M extends PMessage<M>,MB extends PMessageOrBuilder<M>> java.util.Collection<PMessageOrBuilder<M>> toMessageOrBuilders(@Nonnull java.util.Collection<MB> items)
Casting utility to make into a collection of message-or-builders. This is basically a pure cast, but looks better than doing the cast all over the place.- Type Parameters:
M- Message type.MB- Message-or-builder type.- Parameters:
items- Collection of items to be generic cast.- Returns:
- The collection of message-or-builder type.
-
toMessageOrBuilderValues
public static <K,M extends PMessage<M>,MB extends PMessageOrBuilder<M>> java.util.Map<K,PMessageOrBuilder<M>> toMessageOrBuilderValues(@Nonnull java.util.Map<K,MB> items)
Casting utility to make into a map of message-or-builders. This is basically a pure cast, but looks better than doing the cast all over the place.- Type Parameters:
K- Map key type.M- Message type.MB- Message or builder type.- Parameters:
items- Map of items to be generic cast.- Returns:
- The map of message-or-builder type.
-
getTargetModifications
public static <M extends PMessage<M>,B extends PMessageBuilder<M>> B getTargetModifications(PMessageOrBuilder<M> source, PMessageOrBuilder<M> target)
Make a builder of the target message with all differences between source and target marked as modifications.- Type Parameters:
M- The message type.B- The builder result type.- Parameters:
source- The source message for changes.target- The message to apply said changes to.- Returns:
- Builder of target with marked modifications.
-
keyPathToFields
@Nonnull public static PField<?>[] keyPathToFields(@Nonnull PMessageDescriptor<?> rootDescriptor, @Nonnull java.lang.String key)
Convert a key path to a list of consecutive fields for recursive lookup.- Parameters:
rootDescriptor- The root message descriptor.key- The '.' joined field name key.- Returns:
- Array of fields.
-
keyPath
public static java.lang.String keyPath(@Nonnull PField<?>... fields)Append field to the given path.- Parameters:
fields- Fields to make key path of.- Returns:
- The new appended key path.
-
keyPathAppend
public static java.lang.String keyPathAppend(@Nullable java.lang.String path, @Nonnull PField<?> field)Append field to the given path.- Parameters:
path- The path to be appended to.field- The field who's name should be appended.- Returns:
- The new appended key path.
-
getInMessage
@Nonnull public static <T> java.util.Optional<T> getInMessage(@Nullable PMessageOrBuilder<?> message, @Nonnull PField<?>... fields)Look up a key in the message structure. If the key is not found, return the default value for that field, and iterate over the fields until the last one.This differs form
optionalInMessage(PMessageOrBuilder, PField...)by handling the fields' default values.NOTE: This method should NOT be used directly in code with constant field enums, in that case you should use optional of the getter and map until you have the last value, which should always return the same, but is compile-time type safe. E.g.:
Optional.ofNullable(message.getFirst()) .map(First::getSecond) .map(Second::getThird) .orElse(myDefault);- Type Parameters:
T- The expected leaf value type.- Parameters:
message- The message to look up into.fields- Field to get in order.- Returns:
- The value found or null.
- Throws:
java.lang.IllegalArgumentException- When unable to get value from message.
-
optionalInMessage
@Nonnull public static <T> java.util.Optional<T> optionalInMessage(@Nullable PMessageOrBuilder<?> message, @Nonnull PField<?>... fields)Get a field value from a message with optional chaining. If the field is not set, or any message in the chain leading up to the last message is missing, it will return an empty optional, otherwise the leaf field value. Note that this will only check for MESSAGE type if the message is present and needs to be looked up in.This differs from
getInMessage(PMessageOrBuilder, PField...)by explicitly NOT handling fields' default values.NOTE: This method should NOT be used directly in code with constant field enums, in that case you should use the optional getter and flatMap until you have the last value, which should always return the same, but is compile-time type safe. E.g.:
message.optionalFirst() .flatMap(First::optionalSecond) .flatMap(Second::optionalThird) .orElse(myDefault);- Type Parameters:
T- The expected leaf value type.- Parameters:
message- The message to start looking up field values in.fields- Fields to look up in the message.- Returns:
- Optional field value.
-
toMap
@Nonnull @Deprecated public static java.util.Map<java.lang.String,java.lang.Object> toMap(@Nonnull PMessageOrBuilder<?> message)Deprecated.UsemessageToMap(PMessageOrBuilder). Will be removed in future major release. Function is renamed tTransform a message into a native map structure. This will make messages intoTreeMaps, maps and collections will be made into it's native mutable counterpart, and this will deeply transform the message, so message fields will also be transformed, and values in maps and collection will too.Note that some special cases will not be transformed, like messages and containers in map keys.
- Parameters:
message- The message to be transformed.- Returns:
- The native map representing the message.
-
messageToMap
@Nonnull public static java.util.Map<java.lang.String,java.lang.Object> messageToMap(@Nonnull PMessageOrBuilder<?> message)Transform a message into a native map structure. This will make messages intoTreeMaps, maps and collections will be made into it's native mutable counterpart, and this will deeply transform the message, so message fields will also be transformed, and values in maps and collection will too.Note that some special cases will not be transformed, like messages and containers in map keys.
- Parameters:
message- The message to be transformed.- Returns:
- The native map representing the message.
-
coerce
public static java.util.Optional<java.lang.Object> coerce(@Nonnull PDescriptor valueType, java.lang.Object value)Coerce value to match the given type descriptor.- Parameters:
valueType- The value type to coerce to.value- The value to be coerced.- Returns:
- The coerced value.
-
coerceStrict
public static java.util.Optional<java.lang.Object> coerceStrict(@Nonnull PDescriptor valueType, java.lang.Object value)Coerce value to match the given type descriptor using struct type checking. This means some loose coercion transitions are not allowed.- Parameters:
valueType- The value type to coerce to.value- The value to be coerced.- Returns:
- The coerced value.
-
-