-
Notifications
You must be signed in to change notification settings - Fork 10.9k
/
MutableTypeToInstanceMap.java
170 lines (145 loc) · 4.88 KB
/
MutableTypeToInstanceMap.java
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
/*
* Copyright (C) 2012 The Guava 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 com.google.common.reflect;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.annotations.Beta;
import com.google.common.base.Function;
import com.google.common.collect.ForwardingMap;
import com.google.common.collect.ForwardingMapEntry;
import com.google.common.collect.ForwardingSet;
import com.google.common.collect.Iterators;
import com.google.common.collect.Maps;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.checkerframework.checker.nullness.compatqual.NullableDecl;
/**
* A mutable type-to-instance map. See also {@link ImmutableTypeToInstanceMap}.
*
* @author Ben Yu
* @since 13.0
*/
@Beta
public final class MutableTypeToInstanceMap<B> extends ForwardingMap<TypeToken<? extends B>, B>
implements TypeToInstanceMap<B> {
private final Map<TypeToken<? extends B>, B> backingMap = Maps.newHashMap();
@Override
@NullableDecl
public <T extends B> T getInstance(Class<T> type) {
return trustedGet(TypeToken.of(type));
}
@Override
@NullableDecl
public <T extends B> T getInstance(TypeToken<T> type) {
return trustedGet(type.rejectTypeVariables());
}
@Override
@CanIgnoreReturnValue
@NullableDecl
public <T extends B> T putInstance(Class<T> type, @NullableDecl T value) {
return trustedPut(TypeToken.of(type), value);
}
@Override
@CanIgnoreReturnValue
@NullableDecl
public <T extends B> T putInstance(TypeToken<T> type, @NullableDecl T value) {
return trustedPut(type.rejectTypeVariables(), value);
}
/**
* Not supported. Use {@link #putInstance} instead.
*
* @deprecated unsupported operation
* @throws UnsupportedOperationException always
*/
@CanIgnoreReturnValue
@Deprecated
@Override
public B put(TypeToken<? extends B> key, B value) {
throw new UnsupportedOperationException("Please use putInstance() instead.");
}
/**
* Not supported. Use {@link #putInstance} instead.
*
* @deprecated unsupported operation
* @throws UnsupportedOperationException always
*/
@Deprecated
@Override
public void putAll(Map<? extends TypeToken<? extends B>, ? extends B> map) {
throw new UnsupportedOperationException("Please use putInstance() instead.");
}
@Override
public Set<Entry<TypeToken<? extends B>, B>> entrySet() {
return UnmodifiableEntry.transformEntries(super.entrySet());
}
@Override
protected Map<TypeToken<? extends B>, B> delegate() {
return backingMap;
}
@SuppressWarnings("unchecked") // value could not get in if not a T
@NullableDecl
private <T extends B> T trustedPut(TypeToken<T> type, @NullableDecl T value) {
return (T) backingMap.put(type, value);
}
@SuppressWarnings("unchecked") // value could not get in if not a T
@NullableDecl
private <T extends B> T trustedGet(TypeToken<T> type) {
return (T) backingMap.get(type);
}
private static final class UnmodifiableEntry<K, V> extends ForwardingMapEntry<K, V> {
private final Entry<K, V> delegate;
static <K, V> Set<Entry<K, V>> transformEntries(final Set<Entry<K, V>> entries) {
return new ForwardingSet<Map.Entry<K, V>>() {
@Override
protected Set<Entry<K, V>> delegate() {
return entries;
}
@Override
public Iterator<Entry<K, V>> iterator() {
return UnmodifiableEntry.transformEntries(super.iterator());
}
@Override
public Object[] toArray() {
return standardToArray();
}
@Override
public <T> T[] toArray(T[] array) {
return standardToArray(array);
}
};
}
private static <K, V> Iterator<Entry<K, V>> transformEntries(Iterator<Entry<K, V>> entries) {
return Iterators.transform(
entries,
new Function<Entry<K, V>, Entry<K, V>>() {
@Override
public Entry<K, V> apply(Entry<K, V> entry) {
return new UnmodifiableEntry<>(entry);
}
});
}
private UnmodifiableEntry(java.util.Map.Entry<K, V> delegate) {
this.delegate = checkNotNull(delegate);
}
@Override
protected Entry<K, V> delegate() {
return delegate;
}
@Override
public V setValue(V value) {
throw new UnsupportedOperationException();
}
}
}