forked from grpc/grpc-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAttributes.java
More file actions
278 lines (250 loc) · 7.72 KB
/
Attributes.java
File metadata and controls
278 lines (250 loc) · 7.72 KB
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
/*
* Copyright 2015 The gRPC Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.grpc;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.base.Objects;
import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.Set;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
/**
* An immutable type-safe container of attributes.
*
* <h3>Annotation semantics</h3>
*
* <p>As a convention, annotations such as {@link Grpc.TransportAttr} is defined to associate
* attribute {@link Key}s and their propagation paths. The annotation may be applied to a {@code
* Key} definition field, a method that returns {@link Attributes}, or a variable of type {@link
* Attributes}, to indicate that the annotated {@link Attributes} objects may contain the annotated
* {@code Key}.
*
* <p>Javadoc users may click "USE" on the navigation bars of the annotation's javadoc page to view
* references of such annotation.
*
* @since 1.13.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1764")
@Immutable
public final class Attributes {
private final IdentityHashMap<Key<?>, Object> data;
private static final IdentityHashMap<Key<?>, Object> EMPTY_MAP =
new IdentityHashMap<Key<?>, Object>();
public static final Attributes EMPTY = new Attributes(EMPTY_MAP);
private Attributes(IdentityHashMap<Key<?>, Object> data) {
assert data != null;
this.data = data;
}
/**
* Gets the value for the key, or {@code null} if it's not present.
*/
@SuppressWarnings("unchecked")
@Nullable
public <T> T get(Key<T> key) {
return (T) data.get(key);
}
/**
* Returns set of keys stored in container.
*
* @return Set of Key objects.
* @deprecated This method is being considered for removal, if you feel this method is needed
* please reach out on this Github issue:
* <a href="https://github.com/grpc/grpc-java/issues/1764">grpc-java/issues/1764</a>.
*/
@Deprecated
public Set<Key<?>> keys() {
return Collections.unmodifiableSet(data.keySet());
}
Set<Key<?>> keysForTest() {
return Collections.unmodifiableSet(data.keySet());
}
/**
* Create a new builder that is pre-populated with the content from a given container.
* @deprecated Use {@link Attributes#toBuilder()} on the {@link Attributes} instance instead.
* This method will be removed in the future.
*/
@Deprecated
public static Builder newBuilder(Attributes base) {
checkNotNull(base, "base");
return new Builder(base);
}
/**
* Create a new builder.
*/
public static Builder newBuilder() {
return new Builder(EMPTY);
}
/**
* Creates a new builder that is pre-populated with the content of this container.
* @return a new builder.
*/
public Builder toBuilder() {
return new Builder(this);
}
/**
* Key for an key-value pair. Uses reference equality.
*
* @param <T> type of the value in the key-value pair
*/
@Immutable
@SuppressWarnings("UnusedTypeParameter")
public static final class Key<T> {
private final String debugString;
private Key(String debugString) {
this.debugString = debugString;
}
@Override
public String toString() {
return debugString;
}
/**
* Factory method for creating instances of {@link Key}.
*
* @param debugString a string used to describe the key, used for debugging.
* @param <T> Key type
* @return Key object
* @deprecated use {@link #create} instead. This method will be removed in the future.
*/
@Deprecated
public static <T> Key<T> of(String debugString) {
return new Key<>(debugString);
}
/**
* Factory method for creating instances of {@link Key}.
*
* @param debugString a string used to describe the key, used for debugging.
* @param <T> Key type
* @return Key object
*/
public static <T> Key<T> create(String debugString) {
return new Key<>(debugString);
}
}
@Override
public String toString() {
return data.toString();
}
/**
* Returns true if the given object is also a {@link Attributes} with an equal attribute values.
*
* <p>Note that if a stored values are mutable, it is possible for two objects to be considered
* equal at one point in time and not equal at another (due to concurrent mutation of attribute
* values).
*
* <p>This method is not implemented efficiently and is meant for testing.
*
* @param o an object.
* @return true if the given object is a {@link Attributes} equal attributes.
*/
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Attributes that = (Attributes) o;
if (data.size() != that.data.size()) {
return false;
}
for (Map.Entry<Key<?>, Object> e : data.entrySet()) {
if (!that.data.containsKey(e.getKey())) {
return false;
}
if (!Objects.equal(e.getValue(), that.data.get(e.getKey()))) {
return false;
}
}
return true;
}
/**
* Returns a hash code for the attributes.
*
* <p>Note that if a stored values are mutable, it is possible for two objects to be considered
* equal at one point in time and not equal at another (due to concurrent mutation of attribute
* values).
*
* @return a hash code for the attributes map.
*/
@Override
public int hashCode() {
int hashCode = 0;
for (Map.Entry<Key<?>, Object> e : data.entrySet()) {
hashCode += Objects.hashCode(e.getKey(), e.getValue());
}
return hashCode;
}
/**
* The helper class to build an Attributes instance.
*/
public static final class Builder {
private Attributes base;
private IdentityHashMap<Key<?>, Object> newdata;
private Builder(Attributes base) {
assert base != null;
this.base = base;
}
private IdentityHashMap<Key<?>, Object> data(int size) {
if (newdata == null) {
newdata = new IdentityHashMap<>(size);
}
return newdata;
}
public <T> Builder set(Key<T> key, T value) {
data(1).put(key, value);
return this;
}
/**
* Removes the key and associated value from the attributes.
*
* @since 1.22.0
* @param key The key to remove
* @return this
*/
public <T> Builder discard(Key<T> key) {
if (base.data.containsKey(key)) {
IdentityHashMap<Key<?>, Object> newBaseData = new IdentityHashMap<>(base.data);
newBaseData.remove(key);
base = new Attributes(newBaseData);
}
if (newdata != null) {
newdata.remove(key);
}
return this;
}
public Builder setAll(Attributes other) {
data(other.data.size()).putAll(other.data);
return this;
}
/**
* Build the attributes.
*/
public Attributes build() {
if (newdata != null) {
for (Map.Entry<Key<?>, Object> entry : base.data.entrySet()) {
if (!newdata.containsKey(entry.getKey())) {
newdata.put(entry.getKey(), entry.getValue());
}
}
base = new Attributes(newdata);
newdata = null;
}
return base;
}
}
}