MapFieldSchemaFull.java
Go to the documentation of this file.
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 
34 import java.util.Map;
35 
36 class MapFieldSchemaFull implements MapFieldSchema {
37  @Override
38  public Map<?, ?> forMutableMapData(Object mapField) {
39  return ((MapField<?, ?>) mapField).getMutableMap();
40  }
41 
42  @Override
43  public Map<?, ?> forMapData(Object mapField) {
44  return ((MapField<?, ?>) mapField).getMap();
45  }
46 
47  @Override
48  public boolean isImmutable(Object mapField) {
49  return !((MapField<?, ?>) mapField).isMutable();
50  }
51 
52  @Override
53  public Object toImmutable(Object mapField) {
54  ((MapField<?, ?>) mapField).makeImmutable();
55  return mapField;
56  }
57 
58  @Override
59  public Object newMapField(Object mapDefaultEntry) {
60  return MapField.newMapField((MapEntry<?, ?>) mapDefaultEntry);
61  }
62 
63  @Override
64  public Metadata<?, ?> forMapMetadata(Object mapDefaultEntry) {
65  return ((MapEntry<?, ?>) mapDefaultEntry).getMetadata();
66  }
67 
68  @Override
69  public Object mergeFrom(Object destMapField, Object srcMapField) {
70  return mergeFromFull(destMapField, srcMapField);
71  }
72 
73  @SuppressWarnings("unchecked")
74  private static <K, V> Object mergeFromFull(Object destMapField, Object srcMapField) {
75  MapField<K, V> mine = (MapField<K, V>) destMapField;
76  MapField<K, V> other = (MapField<K, V>) srcMapField;
77  if (!mine.isMutable()) {
78  mine.copy();
79  }
80  mine.mergeFrom(other);
81  return mine;
82  }
83 
84  @Override
85  public int getSerializedSize(int number, Object mapField, Object mapDefaultEntry) {
86  return getSerializedSizeFull(number, mapField, mapDefaultEntry);
87  }
88 
89  @SuppressWarnings("unchecked")
90  private static <K, V> int getSerializedSizeFull(
91  int number, Object mapField, Object defaultEntryObject) {
92  // Full runtime allocates map fields lazily.
93  if (mapField == null) {
94  return 0;
95  }
96 
97  Map<K, V> map = ((MapField<K, V>) mapField).getMap();
98  MapEntry<K, V> defaultEntry = (MapEntry<K, V>) defaultEntryObject;
99  if (map.isEmpty()) {
100  return 0;
101  }
102  int size = 0;
103  for (Map.Entry<K, V> entry : map.entrySet()) {
104  size +=
105  CodedOutputStream.computeTagSize(number)
106  + CodedOutputStream.computeLengthDelimitedFieldSize(
107  MapEntryLite.computeSerializedSize(
108  defaultEntry.getMetadata(), entry.getKey(), entry.getValue()));
109  }
110  return size;
111  }
112 }
K
#define K(t)
Definition: sha1.c:43
Map
Definition: ruby/ext/google/protobuf_c/protobuf.h:442
com.google.protobuf.MapEntryLite.Metadata
Definition: MapEntryLite.java:47
com.google.protobuf
Definition: ProtoCaliperBenchmark.java:2
map
zval * map
Definition: php/ext/google/protobuf/encode_decode.c:473
size
#define size
Definition: glcorearb.h:2944
java
size
GLsizeiptr size
Definition: glcorearb.h:2943
com.google
com.google.protobuf.MapEntryLite
Definition: MapEntryLite.java:45
com
number
double number
Definition: cJSON.h:326


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:56