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

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

There is a newer version: 4.0.0-preview.1
Show newest version
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc.  All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

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
abstract class ListFieldSchema {
  // Disallow construction.
  private ListFieldSchema() {}

  private static final ListFieldSchema FULL_INSTANCE = new ListFieldSchemaFull();
  private static final ListFieldSchema LITE_INSTANCE = new ListFieldSchemaLite();

  abstract  List mutableListAt(Object msg, long offset);

  abstract void makeImmutableListAt(Object msg, long offset);

  abstract  void mergeListsAt(Object msg, Object otherMsg, long offset);

  static ListFieldSchema full() {
    return FULL_INSTANCE;
  }

  static ListFieldSchema lite() {
    return LITE_INSTANCE;
  }

  /** Implementation for the full runtime. */
  private static final class ListFieldSchemaFull extends ListFieldSchema {

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

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

    @Override
    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);
    }

    @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
     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);
    }
  }

  /** Implementation for the lite runtime. */
  private static final class ListFieldSchemaLite extends ListFieldSchema {

    @Override
     List mutableListAt(Object message, long offset) {
      ProtobufList list = getProtobufList(message, offset);
      if (!list.isModifiable()) {
        int size = list.size();
        list =
            list.mutableCopyWithCapacity(
                size == 0 ? AbstractProtobufList.DEFAULT_CAPACITY : size * 2);
        UnsafeUtil.putObject(message, offset, list);
      }
      return list;
    }

    @Override
    void makeImmutableListAt(Object message, long offset) {
      ProtobufList list = getProtobufList(message, offset);
      list.makeImmutable();
    }

    @Override
     void mergeListsAt(Object msg, Object otherMsg, long offset) {
      ProtobufList mine = getProtobufList(msg, offset);
      ProtobufList other = getProtobufList(otherMsg, offset);

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

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

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy