aboutsummaryrefslogtreecommitdiffhomepage
path: root/java/core/src/main/java/com/google/protobuf/MapEntryLite.java
blob: 12c64abbb4af3473c18c518795f1b5a39c187332 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// 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 java.io.IOException;

/**
 * Implements the lite version of map entry messages.
 * 
 * This class serves as an utility class to help do serialization/parsing of
 * map entries. It's used in generated code and also in the full version
 * MapEntry message.
 * 
 * Protobuf internal. Users shouldn't use.
 */
public class MapEntryLite<K, V>
    extends AbstractMessageLite<MapEntryLite<K, V>, MapEntryLite.Builder<K, V>> {
  private static class Metadata<K, V> {
    public final MapEntryLite<K, V> defaultInstance;
    public final WireFormat.FieldType keyType;
    public final WireFormat.FieldType valueType;
    public final Parser<MapEntryLite<K, V>> parser;
    public Metadata(
        MapEntryLite<K, V> defaultInstance,
        WireFormat.FieldType keyType,
        WireFormat.FieldType valueType) {
      this.defaultInstance = defaultInstance; 
      this.keyType = keyType;
      this.valueType = valueType;
      final Metadata<K, V> finalThis = this;
      this.parser = new AbstractParser<MapEntryLite<K, V>>() {
        @Override
        public MapEntryLite<K, V> parsePartialFrom(
            CodedInputStream input, ExtensionRegistryLite extensionRegistry)
            throws InvalidProtocolBufferException {
          return new MapEntryLite<K, V>(finalThis, input, extensionRegistry);
        }
      };
    }
  }
  
  private static final int KEY_FIELD_NUMBER = 1;
  private static final int VALUE_FIELD_NUMBER = 2;
  
  private final Metadata<K, V> metadata;
  private final K key;
  private final V value;
  
  /** Creates a default MapEntryLite message instance. */
  private MapEntryLite(
      WireFormat.FieldType keyType, K defaultKey,
      WireFormat.FieldType valueType, V defaultValue) {
    this.metadata = new Metadata<K, V>(this, keyType, valueType);
    this.key = defaultKey;
    this.value = defaultValue;
  }
  
  /** Creates a new MapEntryLite message. */
  private MapEntryLite(Metadata<K, V> metadata, K key, V value) {
    this.metadata = metadata;
    this.key = key;
    this.value = value;
  }
  
  public K getKey() {
    return key;
  }
  
  public V getValue() {
    return value;
  }

  /**
   * Creates a default MapEntryLite message instance.
   * 
   * This method is used by generated code to create the default instance for
   * a map entry message. The created default instance should be used to create
   * new map entry messages of the same type. For each map entry message, only
   * one default instance should be created. 
   */
  public static <K, V> MapEntryLite<K, V> newDefaultInstance(
      WireFormat.FieldType keyType, K defaultKey,
      WireFormat.FieldType valueType, V defaultValue) {
    return new MapEntryLite<K, V>(
        keyType, defaultKey, valueType, defaultValue);
  }
  
  @Override
  public void writeTo(CodedOutputStream output) throws IOException {
    writeField(KEY_FIELD_NUMBER, metadata.keyType, key, output);
    writeField(VALUE_FIELD_NUMBER, metadata.valueType, value, output);
  }

  private void writeField(
      int number, WireFormat.FieldType type, Object value,
      CodedOutputStream output) throws IOException {
    output.writeTag(number, type.getWireType());
    FieldSet.writeElementNoTag(output, type, value);
  }

  private volatile int cachedSerializedSize = -1;
  @Override
  public int getSerializedSize() {
    if (cachedSerializedSize != -1) {
      return cachedSerializedSize;
    }
    int size = 0;
    size += getFieldSize(KEY_FIELD_NUMBER, metadata.keyType, key);
    size += getFieldSize(VALUE_FIELD_NUMBER, metadata.valueType, value);
    cachedSerializedSize = size;
    return size;
  }

  private int getFieldSize(
      int number, WireFormat.FieldType type, Object value) {
    return CodedOutputStream.computeTagSize(number)
        + FieldSet.computeElementSizeNoTag(type, value);
  }
  
  /** Parsing constructor. */
  private MapEntryLite(
      Metadata<K, V> metadata,
      CodedInputStream input,
      ExtensionRegistryLite extensionRegistry)
      throws InvalidProtocolBufferException {
    try {
      K key = metadata.defaultInstance.key;
      V value = metadata.defaultInstance.value;
      while (true) {
        int tag = input.readTag();
        if (tag == 0) {
          break;
        }
        if (tag == WireFormat.makeTag(
                KEY_FIELD_NUMBER, metadata.keyType.getWireType())) {
          key = mergeField(
              input, extensionRegistry, metadata.keyType, key);
        } else if (tag == WireFormat.makeTag(
                       VALUE_FIELD_NUMBER, metadata.valueType.getWireType())) {
          value = mergeField(
              input, extensionRegistry, metadata.valueType, value);
        } else {
          if (!input.skipField(tag)) {
            break;
          }
        }
      }
      this.metadata = metadata;
      this.key = key;
      this.value = value;
    } catch (InvalidProtocolBufferException e) {
      throw e.setUnfinishedMessage(this);
    } catch (IOException e) {
      throw new InvalidProtocolBufferException(e.getMessage())
          .setUnfinishedMessage(this);
    }
  }
  
  @SuppressWarnings("unchecked")
  private <T> T mergeField(
      CodedInputStream input, ExtensionRegistryLite extensionRegistry,
      WireFormat.FieldType type, T value) throws IOException {
    switch (type) {
      case MESSAGE:
        MessageLite.Builder subBuilder = ((MessageLite) value).toBuilder();
        input.readMessage(subBuilder, extensionRegistry);
        return (T) subBuilder.buildPartial();
      case ENUM:
        return (T) (java.lang.Integer) input.readEnum();
      case GROUP:
        throw new RuntimeException("Groups are not allowed in maps.");
      default:
        return (T) FieldSet.readPrimitiveField(input, type, true);
    }
  }

  @Override
  public Parser<MapEntryLite<K, V>> getParserForType() {
    return metadata.parser;
  }

  @Override
  public Builder<K, V> newBuilderForType() {
    return new Builder<K, V>(metadata);
  }

  @Override
  public Builder<K, V> toBuilder() {
    return new Builder<K, V>(metadata, key, value);
  }

  @Override
  public MapEntryLite<K, V> getDefaultInstanceForType() {
    return metadata.defaultInstance;
  }

  @Override
  public boolean isInitialized() {
    if (metadata.valueType.getJavaType() == WireFormat.JavaType.MESSAGE) {
      return ((MessageLite) value).isInitialized();
    }
    return true;
  }

  /**
   * Builder used to create {@link MapEntryLite} messages.
   */
  public static class Builder<K, V>
      extends AbstractMessageLite.Builder<MapEntryLite<K, V>, Builder<K, V>> {
    private final Metadata<K, V> metadata;
    private K key;
    private V value;
    
    private Builder(Metadata<K, V> metadata) {
      this.metadata = metadata;
      this.key = metadata.defaultInstance.key;
      this.value = metadata.defaultInstance.value;
    }
    
    public K getKey() {
      return key;
    }
    
    public V getValue() {
      return value;
    }
    
    public Builder<K, V> setKey(K key) {
      this.key = key;
      return this;
    }
    
    public Builder<K, V> setValue(V value) {
      this.value = value;
      return this;
    }
    
    public Builder<K, V> clearKey() {
      this.key = metadata.defaultInstance.key;
      return this;
    }
    
    public Builder<K, V> clearValue() {
      this.value = metadata.defaultInstance.value;
      return this;
    }

    @Override
    public Builder<K, V> clear() {
      this.key = metadata.defaultInstance.key;
      this.value = metadata.defaultInstance.value;
      return this;
    }

    @Override
    public MapEntryLite<K, V> build() {
      MapEntryLite<K, V> result = buildPartial();
      if (!result.isInitialized()) {
        throw newUninitializedMessageException(result);
      }
      return result;
    }

    @Override
    public MapEntryLite<K, V> buildPartial() {
      return new MapEntryLite<K, V>(metadata, key, value);
    }

    @Override
    public MessageLite getDefaultInstanceForType() {
      return metadata.defaultInstance;
    }

    @Override
    public boolean isInitialized() {
      if (metadata.valueType.getJavaType() == WireFormat.JavaType.MESSAGE) {
        return ((MessageLite) value).isInitialized();
      }
      return true;
    }

    private Builder(Metadata<K, V> metadata, K key, V value) {
      this.metadata = metadata;
      this.key = key;
      this.value = value;
    }
    
    @Override
    public Builder<K, V> clone() {
      return new Builder<K, V>(metadata, key, value);
    }

    @Override
    public Builder<K, V> mergeFrom(
        CodedInputStream input, ExtensionRegistryLite extensionRegistry)
        throws IOException {
      MapEntryLite<K, V> entry =
          new MapEntryLite<K, V>(metadata, input, extensionRegistry);
      this.key = entry.key;
      this.value = entry.value;
      return this;
    }

    @Override
    protected Builder<K, V> internalMergeFrom(MapEntryLite<K, V> message) {
      throw new UnsupportedOperationException();
    }
  }
}