MapFieldLite.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 static com.google.protobuf.Internal.checkNotNull;
34 
36 import java.util.Arrays;
37 import java.util.Collections;
38 import java.util.LinkedHashMap;
39 import java.util.Map;
40 import java.util.Set;
41 
47 public final class MapFieldLite<K, V> extends LinkedHashMap<K, V> {
48 
49  private boolean isMutable;
50 
51  private MapFieldLite() {
52  this.isMutable = true;
53  }
54 
55  private MapFieldLite(Map<K, V> mapData) {
56  super(mapData);
57  this.isMutable = true;
58  }
59 
60  @SuppressWarnings({"rawtypes", "unchecked"})
61  private static final MapFieldLite EMPTY_MAP_FIELD = new MapFieldLite();
62 
63  static {
65  }
66 
68  @SuppressWarnings({"unchecked", "cast"})
69  public static <K, V> MapFieldLite<K, V> emptyMapField() {
71  }
72 
73  public void mergeFrom(MapFieldLite<K, V> other) {
74  ensureMutable();
75  if (!other.isEmpty()) {
76  putAll(other);
77  }
78  }
79 
80  @SuppressWarnings({"unchecked", "cast"})
81  @Override
82  public Set<Map.Entry<K, V>> entrySet() {
83  return isEmpty() ? Collections.<Map.Entry<K, V>>emptySet() : super.entrySet();
84  }
85 
86  @Override
87  public void clear() {
88  ensureMutable();
89  super.clear();
90  }
91 
92  @Override
93  public V put(K key, V value) {
94  ensureMutable();
95  checkNotNull(key);
96 
97  checkNotNull(value);
98  return super.put(key, value);
99  }
100 
101  public V put(Map.Entry<K, V> entry) {
102  return put(entry.getKey(), entry.getValue());
103  }
104 
105  @Override
106  public void putAll(Map<? extends K, ? extends V> m) {
107  ensureMutable();
109  super.putAll(m);
110  }
111 
112  @Override
113  public V remove(Object key) {
114  ensureMutable();
115  return super.remove(key);
116  }
117 
118  private static void checkForNullKeysAndValues(Map<?, ?> m) {
119  for (Object key : m.keySet()) {
120  checkNotNull(key);
121  checkNotNull(m.get(key));
122  }
123  }
124 
125  private static boolean equals(Object a, Object b) {
126  if (a instanceof byte[] && b instanceof byte[]) {
127  return Arrays.equals((byte[]) a, (byte[]) b);
128  }
129  return a.equals(b);
130  }
131 
136  static <K, V> boolean equals(Map<K, V> a, Map<K, V> b) {
137  if (a == b) {
138  return true;
139  }
140  if (a.size() != b.size()) {
141  return false;
142  }
143  for (Map.Entry<K, V> entry : a.entrySet()) {
144  if (!b.containsKey(entry.getKey())) {
145  return false;
146  }
147  if (!equals(entry.getValue(), b.get(entry.getKey()))) {
148  return false;
149  }
150  }
151  return true;
152  }
153 
155  @SuppressWarnings("unchecked")
156  @Override
157  public boolean equals(Object object) {
158  return (object instanceof Map) && equals(this, (Map<K, V>) object);
159  }
160 
161  private static int calculateHashCodeForObject(Object a) {
162  if (a instanceof byte[]) {
163  return Internal.hashCode((byte[]) a);
164  }
165  // Enums should be stored as integers internally.
166  if (a instanceof EnumLite) {
167  throw new UnsupportedOperationException();
168  }
169  return a.hashCode();
170  }
171 
176  static <K, V> int calculateHashCodeForMap(Map<K, V> a) {
177  int result = 0;
178  for (Map.Entry<K, V> entry : a.entrySet()) {
179  result +=
180  calculateHashCodeForObject(entry.getKey()) ^ calculateHashCodeForObject(entry.getValue());
181  }
182  return result;
183  }
184 
185  @Override
186  public int hashCode() {
187  return calculateHashCodeForMap(this);
188  }
189 
190  private static Object copy(Object object) {
191  if (object instanceof byte[]) {
192  byte[] data = (byte[]) object;
193  return Arrays.copyOf(data, data.length);
194  }
195  return object;
196  }
197 
203  @SuppressWarnings("unchecked")
204  static <K, V> Map<K, V> copy(Map<K, V> map) {
205  Map<K, V> result = new LinkedHashMap<K, V>();
206  for (Map.Entry<K, V> entry : map.entrySet()) {
207  result.put(entry.getKey(), (V) copy(entry.getValue()));
208  }
209  return result;
210  }
211 
214  return isEmpty() ? new MapFieldLite<K, V>() : new MapFieldLite<K, V>(this);
215  }
216 
221  public void makeImmutable() {
222  isMutable = false;
223  }
224 
226  public boolean isMutable() {
227  return isMutable;
228  }
229 
230  private void ensureMutable() {
231  if (!isMutable()) {
232  throw new UnsupportedOperationException();
233  }
234  }
235 }
Map
struct Map Map
Definition: php/ext/google/protobuf/protobuf.h:648
com.google.protobuf.MapFieldLite.MapFieldLite
MapFieldLite(Map< K, V > mapData)
Definition: MapFieldLite.java:55
K
#define K(t)
Definition: sha1.c:43
Map
Definition: ruby/ext/google/protobuf_c/protobuf.h:442
com.google.protobuf.MapFieldLite.mutableCopy
MapFieldLite< K, V > mutableCopy()
Definition: MapFieldLite.java:213
com.google.protobuf.MapFieldLite.equals
static boolean equals(Object a, Object b)
Definition: MapFieldLite.java:125
com.google.protobuf.MapFieldLite.putAll
void putAll(Map<? extends K, ? extends V > m)
Definition: MapFieldLite.java:106
com.google.protobuf.MapFieldLite.entrySet
Set< Map.Entry< K, V > > entrySet()
Definition: MapFieldLite.java:82
com.google.protobuf.MapFieldLite.isMutable
boolean isMutable()
Definition: MapFieldLite.java:226
com.google.protobuf.MapFieldLite.makeImmutable
void makeImmutable()
Definition: MapFieldLite.java:221
com.google.protobuf.MapFieldLite.ensureMutable
void ensureMutable()
Definition: MapFieldLite.java:230
com.google.protobuf
Definition: ProtoCaliperBenchmark.java:2
com.google.protobuf.MapFieldLite.clear
void clear()
Definition: MapFieldLite.java:87
com.google.protobuf.MapFieldLite.mergeFrom
void mergeFrom(MapFieldLite< K, V > other)
Definition: MapFieldLite.java:73
map
zval * map
Definition: php/ext/google/protobuf/encode_decode.c:473
com.google.protobuf.Internal.hashCode
static int hashCode(List< byte[]> list)
Definition: Internal.java:269
b
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:3228
com.google.protobuf.MapFieldLite.EMPTY_MAP_FIELD
static final MapFieldLite EMPTY_MAP_FIELD
Definition: MapFieldLite.java:61
com.google.protobuf.Internal.EnumLite
Definition: Internal.java:199
com.google.protobuf.MapFieldLite.calculateHashCodeForObject
static int calculateHashCodeForObject(Object a)
Definition: MapFieldLite.java:161
com.google.protobuf.MapFieldLite.hashCode
int hashCode()
Definition: MapFieldLite.java:186
com.google.protobuf.MapFieldLite.emptyMapField
static< K, V > MapFieldLite< K, V > emptyMapField()
Definition: MapFieldLite.java:69
com.google.protobuf.MapFieldLite.checkForNullKeysAndValues
static void checkForNullKeysAndValues(Map<?, ?> m)
Definition: MapFieldLite.java:118
key
const SETUP_TEARDOWN_TESTCONTEXT char * key
Definition: test_wss_transport.cpp:10
java
com.google.protobuf.MapFieldLite.equals
boolean equals(Object object)
Definition: MapFieldLite.java:157
com.google.protobuf.MapFieldLite.copy
static Object copy(Object object)
Definition: MapFieldLite.java:190
com.google.protobuf.MapFieldLite
Definition: MapFieldLite.java:47
com.google.protobuf.MapFieldLite.MapFieldLite
MapFieldLite()
Definition: MapFieldLite.java:51
m
const upb_json_parsermethod * m
Definition: ruby/ext/google/protobuf_c/upb.h:10501
com.google
com
com.google.protobuf.MapFieldLite.put
V put(Map.Entry< K, V > entry)
Definition: MapFieldLite.java:101
com.google.protobuf.MapFieldLite.isMutable
boolean isMutable
Definition: MapFieldLite.java:49
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: glcorearb.h:2879
com.google.protobuf.Internal
Definition: Internal.java:54
value
GLsizei const GLfloat * value
Definition: glcorearb.h:3093
com.google.protobuf.MapFieldLite.put
V put(K key, V value)
Definition: MapFieldLite.java:93
a
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:3228


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