ProtoList.java

  1. /*
  2.  * Copyright 2022 Proto Utils Authors
  3.  *
  4.  * Licensed to the Apache Software Foundation (ASF) under one
  5.  * or more contributor license agreements. See the NOTICE file
  6.  * distributed with this work for additional information
  7.  * regarding copyright ownership. The ASF licenses this file
  8.  * to you under the Apache License, Version 2.0 (the
  9.  * "License"); you may not use this file except in compliance
  10.  * with the License. You may obtain a copy of the License at
  11.  *
  12.  *   http://www.apache.org/licenses/LICENSE-2.0
  13.  *
  14.  * Unless required by applicable law or agreed to in writing,
  15.  * software distributed under the License is distributed on an
  16.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  17.  * KIND, either express or implied. See the License for the
  18.  * specific language governing permissions and limitations
  19.  * under the License.
  20.  */
  21. package net.morimekta.proto;

  22. import com.google.protobuf.Descriptors;
  23. import com.google.protobuf.MessageOrBuilder;
  24. import net.morimekta.proto.utils.ValueUtil;

  25. import java.util.AbstractList;
  26. import java.util.RandomAccess;

  27. import static java.util.Objects.requireNonNull;

  28. /**
  29.  * A list wrapping a repeated proto field. This is an unmodifiable list.
  30.  *
  31.  * @param <T> The item type.
  32.  */
  33. public class ProtoList<T>
  34.         extends AbstractList<T>
  35.         implements RandomAccess {
  36.     private transient final MessageOrBuilder            message;
  37.     private transient final Descriptors.FieldDescriptor field;

  38.     /**
  39.      * @param message Message the field is on.
  40.      * @param field   The repeated field.
  41.      */
  42.     public ProtoList(MessageOrBuilder message, Descriptors.FieldDescriptor field) {
  43.         requireNonNull(message, "message == null");
  44.         requireNonNull(field, "field == null");
  45.         if (!field.isRepeated() || field.isMapField()) {
  46.             throw new IllegalArgumentException("Not a list type: " + field);
  47.         }
  48.         this.message = message;
  49.         this.field = field;
  50.     }

  51.     @Override
  52.     public int size() {
  53.         return message.getRepeatedFieldCount(field);
  54.     }

  55.     @Override
  56.     @SuppressWarnings("unchecked")
  57.     public T get(int i) {
  58.         return (T) ValueUtil.toJavaValue(field, message.getRepeatedField(field, i));
  59.     }

  60.     @Override
  61.     public String toString() {
  62.         return ValueUtil.asString(this);
  63.     }
  64. }