MapEntryLite.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 
33 import java.io.IOException;
34 import java.util.AbstractMap;
35 import java.util.Map;
36 
45 public class MapEntryLite<K, V> {
46 
47  static class Metadata<K, V> {
48  public final WireFormat.FieldType keyType;
49  public final K defaultKey;
50  public final WireFormat.FieldType valueType;
51  public final V defaultValue;
52 
53  public Metadata(
54  WireFormat.FieldType keyType,
55  K defaultKey,
56  WireFormat.FieldType valueType,
57  V defaultValue) {
58  this.keyType = keyType;
59  this.defaultKey = defaultKey;
60  this.valueType = valueType;
61  this.defaultValue = defaultValue;
62  }
63  }
64 
65  private static final int KEY_FIELD_NUMBER = 1;
66  private static final int VALUE_FIELD_NUMBER = 2;
67 
68  private final Metadata<K, V> metadata;
69  private final K key;
70  private final V value;
71 
73  private MapEntryLite(
74  WireFormat.FieldType keyType, K defaultKey, WireFormat.FieldType valueType, V defaultValue) {
75  this.metadata = new Metadata<K, V>(keyType, defaultKey, valueType, defaultValue);
76  this.key = defaultKey;
77  this.value = defaultValue;
78  }
79 
81  private MapEntryLite(Metadata<K, V> metadata, K key, V value) {
82  this.metadata = metadata;
83  this.key = key;
84  this.value = value;
85  }
86 
87  public K getKey() {
88  return key;
89  }
90 
91  public V getValue() {
92  return value;
93  }
94 
103  WireFormat.FieldType keyType, K defaultKey, WireFormat.FieldType valueType, V defaultValue) {
104  return new MapEntryLite<K, V>(keyType, defaultKey, valueType, defaultValue);
105  }
106 
107  static <K, V> void writeTo(CodedOutputStream output, Metadata<K, V> metadata, K key, V value)
108  throws IOException {
109  FieldSet.writeElement(output, metadata.keyType, KEY_FIELD_NUMBER, key);
110  FieldSet.writeElement(output, metadata.valueType, VALUE_FIELD_NUMBER, value);
111  }
112 
113  static <K, V> int computeSerializedSize(Metadata<K, V> metadata, K key, V value) {
114  return FieldSet.computeElementSize(metadata.keyType, KEY_FIELD_NUMBER, key)
115  + FieldSet.computeElementSize(metadata.valueType, VALUE_FIELD_NUMBER, value);
116  }
117 
118  @SuppressWarnings("unchecked")
119  static <T> T parseField(
120  CodedInputStream input,
121  ExtensionRegistryLite extensionRegistry,
122  WireFormat.FieldType type,
123  T value)
124  throws IOException {
125  switch (type) {
126  case MESSAGE:
127  MessageLite.Builder subBuilder = ((MessageLite) value).toBuilder();
128  input.readMessage(subBuilder, extensionRegistry);
129  return (T) subBuilder.buildPartial();
130  case ENUM:
131  return (T) (java.lang.Integer) input.readEnum();
132  case GROUP:
133  throw new RuntimeException("Groups are not allowed in maps.");
134  default:
135  return (T) FieldSet.readPrimitiveField(input, type, true);
136  }
137  }
138 
144  public void serializeTo(CodedOutputStream output, int fieldNumber, K key, V value)
145  throws IOException {
146  output.writeTag(fieldNumber, WireFormat.WIRETYPE_LENGTH_DELIMITED);
147  output.writeUInt32NoTag(computeSerializedSize(metadata, key, value));
148  writeTo(output, metadata, key, value);
149  }
150 
156  public int computeMessageSize(int fieldNumber, K key, V value) {
157  return CodedOutputStream.computeTagSize(fieldNumber)
158  + CodedOutputStream.computeLengthDelimitedFieldSize(
159  computeSerializedSize(metadata, key, value));
160  }
161 
166  public Map.Entry<K, V> parseEntry(ByteString bytes, ExtensionRegistryLite extensionRegistry)
167  throws IOException {
168  return parseEntry(bytes.newCodedInput(), metadata, extensionRegistry);
169  }
170 
171  static <K, V> Map.Entry<K, V> parseEntry(
172  CodedInputStream input, Metadata<K, V> metadata, ExtensionRegistryLite extensionRegistry)
173  throws IOException {
174  K key = metadata.defaultKey;
175  V value = metadata.defaultValue;
176  while (true) {
177  int tag = input.readTag();
178  if (tag == 0) {
179  break;
180  }
181  if (tag == WireFormat.makeTag(KEY_FIELD_NUMBER, metadata.keyType.getWireType())) {
182  key = parseField(input, extensionRegistry, metadata.keyType, key);
183  } else if (tag == WireFormat.makeTag(VALUE_FIELD_NUMBER, metadata.valueType.getWireType())) {
184  value = parseField(input, extensionRegistry, metadata.valueType, value);
185  } else {
186  if (!input.skipField(tag)) {
187  break;
188  }
189  }
190  }
191  return new AbstractMap.SimpleImmutableEntry<K, V>(key, value);
192  }
193 
198  public void parseInto(
200  throws IOException {
201  int length = input.readRawVarint32();
202  final int oldLimit = input.pushLimit(length);
203  K key = metadata.defaultKey;
204  V value = metadata.defaultValue;
205 
206  while (true) {
207  int tag = input.readTag();
208  if (tag == 0) {
209  break;
210  }
211  if (tag == WireFormat.makeTag(KEY_FIELD_NUMBER, metadata.keyType.getWireType())) {
212  key = parseField(input, extensionRegistry, metadata.keyType, key);
213  } else if (tag == WireFormat.makeTag(VALUE_FIELD_NUMBER, metadata.valueType.getWireType())) {
214  value = parseField(input, extensionRegistry, metadata.valueType, value);
215  } else {
216  if (!input.skipField(tag)) {
217  break;
218  }
219  }
220  }
221 
222  input.checkLastTagWas(0);
223  input.popLimit(oldLimit);
224  map.put(key, value);
225  }
226 
228  Metadata<K, V> getMetadata() {
229  return metadata;
230  }
231 }
Map
struct Map Map
Definition: php/ext/google/protobuf/protobuf.h:648
java::lang
com.google.protobuf.MapEntryLite.parseEntry
Map.Entry< K, V > parseEntry(ByteString bytes, ExtensionRegistryLite extensionRegistry)
Definition: MapEntryLite.java:166
com.google.protobuf.MapEntryLite.newDefaultInstance
static< K, V > MapEntryLite< K, V > newDefaultInstance(WireFormat.FieldType keyType, K defaultKey, WireFormat.FieldType valueType, V defaultValue)
Definition: MapEntryLite.java:102
K
#define K(t)
Definition: sha1.c:43
com.google.protobuf.MapEntryLite.value
final V value
Definition: MapEntryLite.java:70
length
GLenum GLuint GLenum GLsizei length
Definition: glcorearb.h:2695
input
std::string input
Definition: tokenizer_unittest.cc:197
com.google.protobuf.MapEntryLite.serializeTo
void serializeTo(CodedOutputStream output, int fieldNumber, K key, V value)
Definition: MapEntryLite.java:144
com.google.protobuf.MapEntryLite.VALUE_FIELD_NUMBER
static final int VALUE_FIELD_NUMBER
Definition: MapEntryLite.java:66
map
zval * map
Definition: php/ext/google/protobuf/encode_decode.c:473
com.google.protobuf.WireFormat
Definition: WireFormat.java:45
T
#define T(upbtypeconst, upbtype, ctype, default_value)
bytes
uint8 bytes[10]
Definition: coded_stream_unittest.cc:153
com.google.protobuf.CodedInputStream
Definition: CodedInputStream.java:61
com.google.protobuf.MapEntryLite.getValue
V getValue()
Definition: MapEntryLite.java:91
com.google.protobuf.CodedOutputStream.computeTagSize
static int computeTagSize(final int fieldNumber)
Definition: CodedOutputStream.java:709
com.google.protobuf.MapEntryLite.KEY_FIELD_NUMBER
static final int KEY_FIELD_NUMBER
Definition: MapEntryLite.java:65
com.google.protobuf.MapEntryLite.MapEntryLite
MapEntryLite(Metadata< K, V > metadata, K key, V value)
Definition: MapEntryLite.java:81
com.google.protobuf.MapEntryLite.MapEntryLite
MapEntryLite(WireFormat.FieldType keyType, K defaultKey, WireFormat.FieldType valueType, V defaultValue)
Definition: MapEntryLite.java:73
com.google.protobuf.ExtensionRegistryLite
Definition: ExtensionRegistryLite.java:70
com.google.protobuf.MapEntryLite.key
final K key
Definition: MapEntryLite.java:69
java
type
GLenum type
Definition: glcorearb.h:2695
com.google.protobuf.WireFormat.WIRETYPE_LENGTH_DELIMITED
static final int WIRETYPE_LENGTH_DELIMITED
Definition: WireFormat.java:57
com.google.protobuf.MapFieldLite
Definition: MapFieldLite.java:47
com.google.protobuf.MapEntryLite
Definition: MapEntryLite.java:45
com.google.protobuf.CodedOutputStream
Definition: CodedOutputStream.java:59
com.google.protobuf.MapEntryLite.computeMessageSize
int computeMessageSize(int fieldNumber, K key, V value)
Definition: MapEntryLite.java:156
value
GLsizei const GLfloat * value
Definition: glcorearb.h:3093
com.google.protobuf.WireFormat.FieldType
Definition: WireFormat.java:111
com.google.protobuf.MapEntryLite.metadata
final Metadata< K, V > metadata
Definition: MapEntryLite.java:68
output
const upb_json_parsermethod const upb_symtab upb_sink * output
Definition: ruby/ext/google/protobuf_c/upb.h:10503
google::protobuf.internal::FieldType
uint8 FieldType
Definition: extension_set.h:87
com.google.protobuf.MapEntryLite.getKey
K getKey()
Definition: MapEntryLite.java:87
com.google.protobuf.MapEntryLite.parseInto
void parseInto(MapFieldLite< K, V > map, CodedInputStream input, ExtensionRegistryLite extensionRegistry)
Definition: MapEntryLite.java:198
com.google.protobuf.ByteString
Definition: ByteString.java:67


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