Class MessageUtil


  • public class MessageUtil
    extends java.lang.Object
    Convenience methods for handling providence messages.
    • Method Detail

      • 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 descriptor,
                                               @Nonnull
                                               java.lang.String key)
        Convert a key path to a list of consecutive fields for recursive lookup.
        Parameters:
        descriptor - The message descriptor.
        key - The '.' joined field name key.
        Returns:
        Array of fields.
      • 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
        public static java.util.Map<java.lang.String,​java.lang.Object> toMap​(@Nonnull
                                                                                   PMessageOrBuilder<?> message)
        Transform a message into a native map structure. This will make messages into TreeMaps, 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.