Class ProtoUtils


  • public final class ProtoUtils
    extends Object
    Some conveniences for protoc-generated classes.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static Predicate<com.google.protobuf.Descriptors.FieldDescriptor> NOT_EXTENSION  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <I extends com.google.protobuf.MessageLiteOrBuilder,​O extends com.google.protobuf.MessageLite.Builder>
      O
      builder​(I msg)
      Given a message-or-builder, return a builder, invoking toBuilder() if necessary.
      static <I extends com.google.protobuf.MessageLiteOrBuilder,​O extends com.google.protobuf.MessageLite>
      O
      built​(I msg)
      Given a message-or-builder, returns a message, invoking the builder if necessary.
      static <M extends com.google.protobuf.MessageLiteOrBuilder>
      List<M>
      filter​(List<M> objs, Predicate<M> filter)
      Runs a filter through a sequence of objects.
      static <M extends com.google.protobuf.Message>
      M
      filter​(M msg, boolean clearEmpty, Predicate<com.google.protobuf.Descriptors.FieldDescriptor> filter)
      Returns a copy of a Message that contains only fields that pass a filter.
      protected static boolean filterValue​(boolean clearEmpty, Predicate<com.google.protobuf.Descriptors.FieldDescriptor> filter, com.google.protobuf.Message.Builder builder, Map.Entry<com.google.protobuf.Descriptors.FieldDescriptor,​Object> entry)  
      static <B extends com.google.protobuf.MessageLite.Builder>
      boolean
      update​(Iterable<B> objs, Function<B,​Boolean> updater)
      Updates every builder from a sequence.
    • Field Detail

      • NOT_EXTENSION

        public static final Predicate<com.google.protobuf.Descriptors.FieldDescriptor> NOT_EXTENSION
    • Method Detail

      • built

        public static <I extends com.google.protobuf.MessageLiteOrBuilder,​O extends com.google.protobuf.MessageLite> O built​(@Nullable
                                                                                                                                   I msg)
        Given a message-or-builder, returns a message, invoking the builder if necessary.
      • builder

        public static <I extends com.google.protobuf.MessageLiteOrBuilder,​O extends com.google.protobuf.MessageLite.Builder> O builder​(@Nullable
                                                                                                                                             I msg)
        Given a message-or-builder, return a builder, invoking toBuilder() if necessary.
      • update

        public static <B extends com.google.protobuf.MessageLite.Builder> boolean update​(Iterable<B> objs,
                                                                                         Function<B,​Boolean> updater)
        Updates every builder from a sequence.
        Parameters:
        objs - List of builders to update
        updater - Update function. The apply() method can decide or not to update each object, and it's expected to return true if some update was made
        Returns:
        true if at least one object was updated
      • filter

        public static <M extends com.google.protobuf.MessageLiteOrBuilder> List<M> filter​(List<M> objs,
                                                                                          Predicate<M> filter)
        Runs a filter through a sequence of objects.
        Parameters:
        objs - Message-or-builder objects
        filter - Function that returns true to retain an object, false to discard
        Returns:
        Retained objects. If some elements are retained and others are discarded, this will be a new, mutable List that contains only the retained elements. If all elements are retained, returns the same, unmodified input sequence. If all elements are discarded, returns an immutable, empty sequence
      • filter

        @Nullable
        public static <M extends com.google.protobuf.Message> M filter​(M msg,
                                                                       boolean clearEmpty,
                                                                       Predicate<com.google.protobuf.Descriptors.FieldDescriptor> filter)
        Returns a copy of a Message that contains only fields that pass a filter. This will be executed recursively for fields which are child messages.
        Parameters:
        msg - Message object
        clearEmpty - true will cause null to be returned if all fields from msg are removed; false will return an "empty" message in that case
        filter - Function that returns true to retain a field, false to discard
        Returns:
        Message with the retained fieldsfrom msg. If some fields are retained and others discarded, returns a new message object. If all fields are retained, returns the same msg object. If all fields are discarded, returns null if clearEmpty==true or a default instance of msg's message type if clearEmpty==false
      • filterValue

        protected static boolean filterValue​(boolean clearEmpty,
                                             Predicate<com.google.protobuf.Descriptors.FieldDescriptor> filter,
                                             com.google.protobuf.Message.Builder builder,
                                             Map.Entry<com.google.protobuf.Descriptors.FieldDescriptor,​Object> entry)