22
22
import java .util .Collection ;
23
23
import java .util .HashMap ;
24
24
import java .util .Map ;
25
- import java .util .Objects ;
26
25
27
26
/**
28
27
* Warning: this is a special map-like construct that suits only and should be used only in this package!
@@ -52,17 +51,14 @@ public static <K, V> MMap<K, V> copy(MMap<K, V> orig) {
52
51
}
53
52
54
53
public static <K , V > MMap <K , Collection <V >> copyWithListValue (MMap <K , Collection <V >> orig ) {
55
- HashMap <K , Collection <V >> newMap = new HashMap <>((int ) Math .ceil (orig .size () / 0.75D ));
56
- for (Map .Entry <K , Collection <V >> entry : orig .delegate .entrySet ()) {
54
+ HashMap <K , Collection <V >> delegateLocal = orig .delegate ;
55
+ HashMap <K , Collection <V >> newMap = new HashMap <>((int ) Math .ceil (delegateLocal .size () / 0.75D ));
56
+ for (Map .Entry <K , Collection <V >> entry : delegateLocal .entrySet ()) {
57
57
newMap .put (entry .getKey (), entry .getValue () == null ? null : new ArrayList <>(entry .getValue ()));
58
58
}
59
59
return new MMap <>(newMap );
60
60
}
61
61
62
- public static <K , V > MMap <K , V > append (MMap <K , V > orig ) {
63
- return new AppendMMap <>(orig );
64
- }
65
-
66
62
protected final HashMap <K , V > delegate ;
67
63
68
64
private MMap (HashMap <K , V > delegate ) {
@@ -85,10 +81,6 @@ public MMap<K, V> done() {
85
81
return new DoneMMap <>(delegate );
86
82
}
87
83
88
- public MMap <K , V > append () {
89
- return new AppendMMap <>(this );
90
- }
91
-
92
84
@ Override
93
85
public int hashCode () {
94
86
throw new IllegalStateException ("MMap is not done yet" );
@@ -99,20 +91,16 @@ public boolean equals(Object o) {
99
91
throw new IllegalStateException ("MMap is not done yet" );
100
92
}
101
93
102
- public int size () {
94
+ protected int size () {
103
95
return delegate .size ();
104
96
}
105
97
106
98
private static class DoneMMap <K , V > extends MMap <K , V > {
107
- volatile long hashCode = Long . MAX_VALUE ;
99
+ private final int hashCode ;
108
100
109
101
private DoneMMap (HashMap <K , V > delegate ) {
110
102
super (delegate );
111
- }
112
-
113
- private DoneMMap (HashMap <K , V > delegate , long hashCode ) {
114
- super (delegate );
115
- this .hashCode = hashCode ;
103
+ this .hashCode = delegate .hashCode ();
116
104
}
117
105
118
106
@ Override
@@ -127,12 +115,7 @@ public MMap<K, V> done() {
127
115
128
116
@ Override
129
117
public int hashCode () {
130
- if (this .hashCode != Long .MAX_VALUE ) {
131
- return (int ) hashCode ;
132
- }
133
- int result = delegate .hashCode ();
134
- this .hashCode = result ;
135
- return result ;
118
+ return hashCode ;
136
119
}
137
120
138
121
@ Override
@@ -144,35 +127,4 @@ public boolean equals(Object o) {
144
127
return delegate .equals (other .delegate );
145
128
}
146
129
}
147
-
148
- private static class AppendMMap <K , V > extends DoneMMap <K , V > {
149
-
150
- AppendMMap (MMap <K , V > orig ) {
151
- super (new HashMap <>(orig .delegate ));
152
- if (orig instanceof DoneMMap ) {
153
- this .hashCode = ((DoneMMap <K , V >) orig ).hashCode ;
154
- } else {
155
- this .hashCode = Long .MAX_VALUE ;
156
- }
157
- }
158
-
159
- @ Override
160
- public V put (K key , V value ) {
161
- boolean ifPresent = delegate .containsKey (key );
162
- V originalValue = delegate .put (key , value );
163
- if (hashCode != Long .MAX_VALUE ) {
164
- int keyHash = Objects .hashCode (key );
165
- if (ifPresent ) {
166
- hashCode -= keyHash ^ Objects .hashCode (originalValue );
167
- }
168
- hashCode += keyHash ^ Objects .hashCode (value );
169
- }
170
- return originalValue ;
171
- }
172
-
173
- @ Override
174
- public MMap <K , V > done () {
175
- return new DoneMMap <>(delegate , hashCode );
176
- }
177
- }
178
130
}
0 commit comments