1 // Protocol Buffers - Google's data interchange format 2 // Copyright 2008 Google Inc. All rights reserved. 3 // 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file or at 6 // https://developers.google.com/open-source/licenses/bsd 7 8 package com.google.protobuf; 9 10 import com.google.protobuf.Internal.ProtobufList; 11 import java.util.List; 12 13 /** 14 * Utility class that aids in properly manipulating list fields for either the lite or full runtime. 15 */ 16 final class ListFieldSchemaLite implements ListFieldSchema { 17 18 @Override mutableListAt(Object message, long offset)19 public <L> List<L> mutableListAt(Object message, long offset) { 20 ProtobufList<L> list = getProtobufList(message, offset); 21 if (!list.isModifiable()) { 22 int size = list.size(); 23 list = 24 list.mutableCopyWithCapacity( 25 size == 0 ? AbstractProtobufList.DEFAULT_CAPACITY : size * 2); 26 UnsafeUtil.putObject(message, offset, list); 27 } 28 return list; 29 } 30 31 @Override makeImmutableListAt(Object message, long offset)32 public void makeImmutableListAt(Object message, long offset) { 33 ProtobufList<?> list = getProtobufList(message, offset); 34 list.makeImmutable(); 35 } 36 37 @Override mergeListsAt(Object msg, Object otherMsg, long offset)38 public <E> void mergeListsAt(Object msg, Object otherMsg, long offset) { 39 ProtobufList<E> mine = getProtobufList(msg, offset); 40 ProtobufList<E> other = getProtobufList(otherMsg, offset); 41 42 int size = mine.size(); 43 int otherSize = other.size(); 44 if (size > 0 && otherSize > 0) { 45 if (!mine.isModifiable()) { 46 mine = mine.mutableCopyWithCapacity(size + otherSize); 47 } 48 mine.addAll(other); 49 } 50 51 ProtobufList<E> merged = size > 0 ? mine : other; 52 UnsafeUtil.putObject(msg, offset, merged); 53 } 54 55 @SuppressWarnings("unchecked") getProtobufList(Object message, long offset)56 static <E> ProtobufList<E> getProtobufList(Object message, long offset) { 57 return (ProtobufList<E>) UnsafeUtil.getObject(message, offset); 58 } 59 } 60