/
ShadowContentValues.java
316 lines (279 loc) · 9.33 KB
/
ShadowContentValues.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
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
/*
* Copyright (C) 2007 The Android Open Source Project
*
* 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.xtremelabs.robolectric.shadows;
import android.content.ContentValues;
import android.util.Log;
import com.xtremelabs.robolectric.internal.Implementation;
import com.xtremelabs.robolectric.internal.Implements;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import static com.xtremelabs.robolectric.Robolectric.shadowOf;
import static com.xtremelabs.robolectric.Robolectric.shadowOf_;
/**
* Shadows the {@code android.content.ContentValues} class.
* <p/>
* This is a fancy map from String to... something. Works just like the Android class it shadows.
*/
@SuppressWarnings({"UnusedDeclaration"})
@Implements(ContentValues.class)
public final class ShadowContentValues {
private static final String TAG = "ShadowContentValues";
private HashMap<String, Object> values = new HashMap<String, Object>();
public void __constructor__(ContentValues from) {
values = new HashMap<String, Object>(shadowOf(from).values);
}
@Implementation
public void put(String key, String value) {
values.put(key, value);
}
@Implementation
public void putAll(ContentValues other) {
values.putAll(shadowOf(other).values);
}
@Implementation
public void put(String key, Byte value) {
values.put(key, value);
}
@Implementation
public void put(String key, Short value) {
values.put(key, value);
}
@Implementation
public void put(String key, Integer value) {
values.put(key, value);
}
@Implementation
public void put(String key, Long value) {
values.put(key, value);
}
@Implementation
public void put(String key, Float value) {
values.put(key, value);
}
@Implementation
public void put(String key, Double value) {
values.put(key, value);
}
@Implementation
public void put(String key, Boolean value) {
values.put(key, value);
}
@Implementation
public void put(String key, byte[] value) {
values.put(key, value);
}
@Implementation
public void putNull(String key) {
values.put(key, null);
}
@Implementation
public int size() {
return values.size();
}
@Implementation
public void remove(String key) {
values.remove(key);
}
@Implementation
public void clear() {
values.clear();
}
@Implementation
public boolean containsKey(String key) {
return values.containsKey(key);
}
@Implementation
public Object get(String key) {
return values.get(key);
}
@Implementation
public String getAsString(String key) {
Object value = values.get(key);
return value != null ? value.toString() : null;
}
@Implementation
public Long getAsLong(String key) {
Object value = values.get(key);
try {
return value != null ? ((Number) value).longValue() : null;
} catch (ClassCastException e) {
if (value instanceof CharSequence) {
try {
return Long.valueOf(value.toString());
} catch (NumberFormatException e2) {
Log.e(TAG, "Cannot parse Long value for " + value + " at key " + key);
return null;
}
} else {
Log.e(TAG, "Cannot cast value for " + key + " to a Long: " + value, e);
return null;
}
}
}
@Implementation
public Integer getAsInteger(String key) {
Object value = values.get(key);
try {
return value != null ? ((Number) value).intValue() : null;
} catch (ClassCastException e) {
if (value instanceof CharSequence) {
try {
return Integer.valueOf(value.toString());
} catch (NumberFormatException e2) {
Log.e(TAG, "Cannot parse Integer value for " + value + " at key " + key);
return null;
}
} else {
Log.e(TAG, "Cannot cast value for " + key + " to a Integer: " + value, e);
return null;
}
}
}
@Implementation
public Short getAsShort(String key) {
Object value = values.get(key);
try {
return value != null ? ((Number) value).shortValue() : null;
} catch (ClassCastException e) {
if (value instanceof CharSequence) {
try {
return Short.valueOf(value.toString());
} catch (NumberFormatException e2) {
Log.e(TAG, "Cannot parse Short value for " + value + " at key " + key);
return null;
}
} else {
Log.e(TAG, "Cannot cast value for " + key + " to a Short: " + value, e);
return null;
}
}
}
@Implementation
public Byte getAsByte(String key) {
Object value = values.get(key);
try {
return value != null ? ((Number) value).byteValue() : null;
} catch (ClassCastException e) {
if (value instanceof CharSequence) {
try {
return Byte.valueOf(value.toString());
} catch (NumberFormatException e2) {
Log.e(TAG, "Cannot parse Byte value for " + value + " at key " + key);
return null;
}
} else {
Log.e(TAG, "Cannot cast value for " + key + " to a Byte: " + value, e);
return null;
}
}
}
@Implementation
public Double getAsDouble(String key) {
Object value = values.get(key);
try {
return value != null ? ((Number) value).doubleValue() : null;
} catch (ClassCastException e) {
if (value instanceof CharSequence) {
try {
return Double.valueOf(value.toString());
} catch (NumberFormatException e2) {
Log.e(TAG, "Cannot parse Double value for " + value + " at key " + key);
return null;
}
} else {
Log.e(TAG, "Cannot cast value for " + key + " to a Double: " + value, e);
return null;
}
}
}
@Implementation
public Float getAsFloat(String key) {
Object value = values.get(key);
try {
return value != null ? ((Number) value).floatValue() : null;
} catch (ClassCastException e) {
if (value instanceof CharSequence) {
try {
return Float.valueOf(value.toString());
} catch (NumberFormatException e2) {
Log.e(TAG, "Cannot parse Float value for " + value + " at key " + key);
return null;
}
} else {
Log.e(TAG, "Cannot cast value for " + key + " to a Float: " + value, e);
return null;
}
}
}
@Implementation
public Boolean getAsBoolean(String key) {
Object value = values.get(key);
try {
return (Boolean) value;
} catch (ClassCastException e) {
if (value instanceof CharSequence) {
return Boolean.valueOf(value.toString());
} else {
Log.e(TAG, "Cannot cast value for " + key + " to a Boolean: " + value, e);
return null;
}
}
}
@Implementation
public byte[] getAsByteArray(String key) {
Object value = values.get(key);
if (value instanceof byte[]) {
return (byte[]) value;
} else {
return null;
}
}
@Implementation
public Set<Map.Entry<String, Object>> valueSet() {
return values.entrySet();
}
@Implementation
public int describeContents() {
return 0;
}
@Override @Implementation
public boolean equals(Object object) {
if (object == null) return false;
Object o = shadowOf_(object);
if (o == null) return false;
if (this == o) return true;
if (getClass() != o.getClass()) return false;
if (!(o instanceof ContentValues)) {
return false;
}
return values.equals(shadowOf((ContentValues) o).values);
}
@Override @Implementation
public int hashCode() {
return values.hashCode();
}
@Override @Implementation
public String toString() {
StringBuilder sb = new StringBuilder();
for (String name : values.keySet()) {
String value = getAsString(name);
if (sb.length() > 0) sb.append(" ");
sb.append(name + "=" + value);
}
return sb.toString();
}
}