public class MessageUtil extends Object
Modifier and Type | Method and Description |
---|---|
static Optional<Object> |
coerce(PDescriptor valueType,
Object value)
Coerce value to match the given type descriptor.
|
static Optional<Object> |
coerceStrict(PDescriptor valueType,
Object value)
Coerce value to match the given type descriptor using struct type
checking.
|
static <T> Optional<T> |
getInMessage(PMessageOrBuilder<?> message,
PField<?>... fields)
Look up a key in the message structure.
|
static <M extends PMessage<M>,B extends PMessageBuilder<M>> |
getTargetModifications(PMessageOrBuilder<M> source,
PMessageOrBuilder<M> target)
Make a builder of the target message with all differences between
source and target marked as modifications.
|
static String |
keyPath(PField<?>... fields)
Append field to the given path.
|
static String |
keyPathAppend(String path,
PField<?> field)
Append field to the given path.
|
static PField<?>[] |
keyPathToFields(PMessageDescriptor<?> rootDescriptor,
String key)
Convert a key path to a list of consecutive fields for recursive lookup.
|
static Map<String,Object> |
messageToMap(PMessageOrBuilder<?> message)
Transform a message into a native map structure.
|
static <T> 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>> |
toBuilderAll(Collection<V> messages)
Mutate all items of the collection containing messages.
|
static <M extends PMessage<M>,B extends PMessageBuilder<M>> |
toBuilderIfNonNull(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>> |
toBuilderValues(Map<K,V> messages)
Mutate all values of the map containing message-or-builder values.
|
static Map<String,Object> |
toMap(PMessageOrBuilder<?> message)
Deprecated.
Use
messageToMap(PMessageOrBuilder) . Will be removed in future major release.
Function is renamed t |
static <M extends PMessage<M>,V extends PMessageOrBuilder<M>> |
toMessageAll(Collection<V> builders)
Build all items of the collection containing message-or-builders.
|
static <M extends PMessage<M>> |
toMessageIfNotNull(PMessageOrBuilder<M> mob)
Build the message from builder if it is not null.
|
static <M extends PMessage<M>,MB extends PMessageOrBuilder<M>> |
toMessageOrBuilders(Collection<MB> items)
Casting utility to make into a collection of message-or-builders.
|
static <K,M extends PMessage<M>,MB extends PMessageOrBuilder<M>> |
toMessageOrBuilderValues(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>> |
toMessageValues(Map<K,V> messages)
Mutate all items of the collection containing messages.
|
public static <M extends PMessage<M>,V extends PMessageOrBuilder<M>> List<M> toMessageAll(Collection<V> builders)
M
- The message type.V
- The actual value type.builders
- List of message-or-builders.public static <M extends PMessage<M>,V extends PMessageOrBuilder<M>,B extends PMessageBuilder<M>> List<B> toBuilderAll(Collection<V> messages)
M
- The message type.V
- The actual value type.B
- The builder type.messages
- List of messagespublic static <K,M extends PMessage<M>,V extends PMessageOrBuilder<M>,B extends PMessageBuilder<M>> Map<K,B> toBuilderValues(Map<K,V> messages)
K
- The map key type.M
- The message type.V
- The actual value type.B
- The builder type.messages
- Map with message-or-builder values.public static <K,M extends PMessage<M>,V extends PMessageOrBuilder<M>> Map<K,M> toMessageValues(Map<K,V> messages)
K
- The map key type.M
- The message type.V
- The actual value type.messages
- List of messagespublic static <M extends PMessage<M>> M toMessageIfNotNull(PMessageOrBuilder<M> mob)
M
- The message type.mob
- The builder to build.public static <M extends PMessage<M>,B extends PMessageBuilder<M>> B toBuilderIfNonNull(PMessageOrBuilder<M> mob)
M
- The message type.B
- The builder type.mob
- Message or builder to mutate.public static <M extends PMessage<M>,MB extends PMessageOrBuilder<M>> Collection<PMessageOrBuilder<M>> toMessageOrBuilders(@Nonnull Collection<MB> items)
M
- Message type.MB
- Message-or-builder type.items
- Collection of items to be generic cast.public static <K,M extends PMessage<M>,MB extends PMessageOrBuilder<M>> Map<K,PMessageOrBuilder<M>> toMessageOrBuilderValues(@Nonnull Map<K,MB> items)
K
- Map key type.M
- Message type.MB
- Message or builder type.items
- Map of items to be generic cast.public static <M extends PMessage<M>,B extends PMessageBuilder<M>> B getTargetModifications(PMessageOrBuilder<M> source, PMessageOrBuilder<M> target)
M
- The message type.B
- The builder result type.source
- The source message for changes.target
- The message to apply said changes to.@Nonnull public static PField<?>[] keyPathToFields(@Nonnull PMessageDescriptor<?> rootDescriptor, @Nonnull String key)
rootDescriptor
- The root message descriptor.key
- The '.' joined field name key.public static String keyPath(@Nonnull PField<?>... fields)
fields
- Fields to make key path of.public static String keyPathAppend(@Nullable String path, @Nonnull PField<?> field)
path
- The path to be appended to.field
- The field who's name should be appended.@Nonnull public static <T> Optional<T> getInMessage(@Nullable PMessageOrBuilder<?> message, @Nonnull PField<?>... fields)
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);
T
- The expected leaf value type.message
- The message to look up into.fields
- Field to get in order.IllegalArgumentException
- When unable to get value from message.@Nonnull public static <T> Optional<T> optionalInMessage(@Nullable PMessageOrBuilder<?> message, @Nonnull PField<?>... fields)
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);
T
- The expected leaf value type.message
- The message to start looking up field values in.fields
- Fields to look up in the message.@Nonnull @Deprecated public static Map<String,Object> toMap(@Nonnull PMessageOrBuilder<?> message)
messageToMap(PMessageOrBuilder)
. Will be removed in future major release.
Function is renamed tTreeMap
s,
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.
message
- The message to be transformed.@Nonnull public static Map<String,Object> messageToMap(@Nonnull PMessageOrBuilder<?> message)
TreeMap
s,
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.
message
- The message to be transformed.public static Optional<Object> coerce(@Nonnull PDescriptor valueType, Object value)
valueType
- The value type to coerce to.value
- The value to be coerced.public static Optional<Object> coerceStrict(@Nonnull PDescriptor valueType, Object value)
valueType
- The value type to coerce to.value
- The value to be coerced.Copyright © 2015–2020 morimekta.net. All rights reserved.