forked from HubSpot/jinjava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyMap.java
More file actions
86 lines (71 loc) · 1.84 KB
/
PyMap.java
File metadata and controls
86 lines (71 loc) · 1.84 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
package com.hubspot.jinjava.objects.collections;
import com.google.common.collect.ForwardingMap;
import com.hubspot.jinjava.objects.PyWrapper;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
public class PyMap extends ForwardingMap<String, Object> implements PyWrapper {
private boolean computingHashCode = false;
private final Map<String, Object> map;
public PyMap(Map<String, Object> map) {
this.map = map;
}
@Override
protected Map<String, Object> delegate() {
return map;
}
public Object get(String key, Object defaultValue) {
return getOrDefault(key, defaultValue);
}
@Override
public Object put(String s, Object o) {
if (o == this) {
throw new IllegalArgumentException("Can't add map object to itself");
}
return delegate().put(s, o);
}
@Override
public String toString() {
return delegate().toString();
}
public Map<String, Object> toMap() {
return map;
}
public Set<java.util.Map.Entry<String, Object>> items() {
return entrySet();
}
public Set<String> keys() {
return keySet();
}
public void update(Map<? extends String, ? extends Object> m) {
if (m == this) {
throw new IllegalArgumentException("Can't update map object with itself");
}
putAll(m);
}
@Override
public void putAll(Map<? extends String, ? extends Object> m) {
if (m == this) {
throw new IllegalArgumentException(
"Map putAll() operation can't be used to add map to itself"
);
}
super.putAll(m);
}
/**
* This is not thread-safe
* @return hashCode, preventing recursion
*/
@Override
public int hashCode() {
if (computingHashCode) {
return Objects.hashCode(null);
}
try {
computingHashCode = true;
return super.hashCode();
} finally {
computingHashCode = false;
}
}
}