1 // Protocol Buffers - Google's data interchange format 2 // Copyright 2008 Google Inc. All rights reserved. 3 // https://developers.google.com/protocol-buffers/ 4 // 5 // Redistribution and use in source and binary forms, with or without 6 // modification, are permitted provided that the following conditions are 7 // met: 8 // 9 // * Redistributions of source code must retain the above copyright 10 // notice, this list of conditions and the following disclaimer. 11 // * Redistributions in binary form must reproduce the above 12 // copyright notice, this list of conditions and the following disclaimer 13 // in the documentation and/or other materials provided with the 14 // distribution. 15 // * Neither the name of Google Inc. nor the names of its 16 // contributors may be used to endorse or promote products derived from 17 // this software without specific prior written permission. 18 // 19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 31 package com.google.protobuf; 32 33 import com.google.protobuf.MapEntryLite.Metadata; 34 import java.util.Map; 35 36 class MapFieldSchemaLite implements MapFieldSchema { 37 38 @Override forMutableMapData(Object mapField)39 public Map<?, ?> forMutableMapData(Object mapField) { 40 return (MapFieldLite<?, ?>) mapField; 41 } 42 43 @Override forMapMetadata(Object mapDefaultEntry)44 public Metadata<?, ?> forMapMetadata(Object mapDefaultEntry) { 45 return ((MapEntryLite<?, ?>) mapDefaultEntry).getMetadata(); 46 } 47 48 @Override forMapData(Object mapField)49 public Map<?, ?> forMapData(Object mapField) { 50 return (MapFieldLite<?, ?>) mapField; 51 } 52 53 @Override isImmutable(Object mapField)54 public boolean isImmutable(Object mapField) { 55 return !((MapFieldLite<?, ?>) mapField).isMutable(); 56 } 57 58 @Override toImmutable(Object mapField)59 public Object toImmutable(Object mapField) { 60 ((MapFieldLite<?, ?>) mapField).makeImmutable(); 61 return mapField; 62 } 63 64 @Override newMapField(Object unused)65 public Object newMapField(Object unused) { 66 return MapFieldLite.emptyMapField().mutableCopy(); 67 } 68 69 @Override mergeFrom(Object destMapField, Object srcMapField)70 public Object mergeFrom(Object destMapField, Object srcMapField) { 71 return mergeFromLite(destMapField, srcMapField); 72 } 73 74 @SuppressWarnings("unchecked") mergeFromLite(Object destMapField, Object srcMapField)75 private static <K, V> MapFieldLite<K, V> mergeFromLite(Object destMapField, Object srcMapField) { 76 MapFieldLite<K, V> mine = (MapFieldLite<K, V>) destMapField; 77 MapFieldLite<K, V> other = (MapFieldLite<K, V>) srcMapField; 78 if (!other.isEmpty()) { 79 if (!mine.isMutable()) { 80 mine = mine.mutableCopy(); 81 } 82 mine.mergeFrom(other); 83 } 84 return mine; 85 } 86 87 @Override getSerializedSize(int fieldNumber, Object mapField, Object mapDefaultEntry)88 public int getSerializedSize(int fieldNumber, Object mapField, Object mapDefaultEntry) { 89 return getSerializedSizeLite(fieldNumber, mapField, mapDefaultEntry); 90 } 91 92 @SuppressWarnings("unchecked") getSerializedSizeLite( int fieldNumber, Object mapField, Object defaultEntry)93 private static <K, V> int getSerializedSizeLite( 94 int fieldNumber, Object mapField, Object defaultEntry) { 95 MapFieldLite<K, V> mapFieldLite = (MapFieldLite<K, V>) mapField; 96 MapEntryLite<K, V> defaultEntryLite = (MapEntryLite<K, V>) defaultEntry; 97 98 if (mapFieldLite.isEmpty()) { 99 return 0; 100 } 101 int size = 0; 102 for (Map.Entry<K, V> entry : mapFieldLite.entrySet()) { 103 size += defaultEntryLite.computeMessageSize(fieldNumber, entry.getKey(), entry.getValue()); 104 } 105 return size; 106 } 107 } 108