All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.protobuf.ListFieldSchemaFull Maven / Gradle / Ivy

Go to download

Core Protocol Buffers library. Protocol Buffers are a way of encoding structured data in an efficient yet extensible format.

The newest version!
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc.  All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd

package com.google.protobuf;

import com.google.protobuf.Internal.ProtobufList;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Utility class that aids in properly manipulating list fields for either the lite or full runtime.
 */
@CheckReturnValue
final class ListFieldSchemaFull implements ListFieldSchema {

  private static final Class UNMODIFIABLE_LIST_CLASS =
      Collections.unmodifiableList(Collections.emptyList()).getClass();

  @Override
  public  List mutableListAt(Object message, long offset) {
    return mutableListAt(message, offset, AbstractProtobufList.DEFAULT_CAPACITY);
  }

  @SuppressWarnings("unchecked")
  private static  List mutableListAt(Object message, long offset, int additionalCapacity) {
    List list = getList(message, offset);
    if (list.isEmpty()) {
      if (list instanceof LazyStringList) {
        list = (List) new LazyStringArrayList(additionalCapacity);
      } else if (list instanceof PrimitiveNonBoxingCollection && list instanceof ProtobufList) {
        list = ((ProtobufList) list).mutableCopyWithCapacity(additionalCapacity);
      } else {
        list = new ArrayList(additionalCapacity);
      }
      UnsafeUtil.putObject(message, offset, list);
    } else if (UNMODIFIABLE_LIST_CLASS.isAssignableFrom(list.getClass())) {
      ArrayList newList = new ArrayList(list.size() + additionalCapacity);
      newList.addAll(list);
      list = newList;
      UnsafeUtil.putObject(message, offset, list);
    } else if (list instanceof UnmodifiableLazyStringList) {
      LazyStringArrayList newList = new LazyStringArrayList(list.size() + additionalCapacity);
      newList.addAll((UnmodifiableLazyStringList) list);
      list = (List) newList;
      UnsafeUtil.putObject(message, offset, list);
    } else if (list instanceof PrimitiveNonBoxingCollection
        && list instanceof ProtobufList
        && !((ProtobufList) list).isModifiable()) {
      list = ((ProtobufList) list).mutableCopyWithCapacity(list.size() + additionalCapacity);
      UnsafeUtil.putObject(message, offset, list);
    }
    return list;
  }

  @Override
  public void makeImmutableListAt(Object message, long offset) {
    List list = (List) UnsafeUtil.getObject(message, offset);
    Object immutable = null;
    if (list instanceof LazyStringList) {
      immutable = ((LazyStringList) list).getUnmodifiableView();
    } else if (UNMODIFIABLE_LIST_CLASS.isAssignableFrom(list.getClass())) {
      // already immutable
      return;
    } else if (list instanceof PrimitiveNonBoxingCollection && list instanceof ProtobufList) {
      if (((ProtobufList) list).isModifiable()) {
        ((ProtobufList) list).makeImmutable();
      }
      return;
    } else {
      immutable = Collections.unmodifiableList((List) list);
    }
    UnsafeUtil.putObject(message, offset, immutable);
  }

  @Override
  public  void mergeListsAt(Object msg, Object otherMsg, long offset) {
    List other = getList(otherMsg, offset);
    List mine = mutableListAt(msg, offset, other.size());

    int size = mine.size();
    int otherSize = other.size();
    if (size > 0 && otherSize > 0) {
      mine.addAll(other);
    }

    List merged = size > 0 ? mine : other;
    UnsafeUtil.putObject(msg, offset, merged);
  }

  @SuppressWarnings("unchecked")
  static  List getList(Object message, long offset) {
    return (List) UnsafeUtil.getObject(message, offset);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy