11
11
* Created by adarsh.sharma on 07/01/18.
12
12
*/
13
13
public abstract class ReferenceDiffCache <C , D > {
14
- private Map <String , Map < String , D > > refDiffMap ;
14
+ private Map <CacheKey , D > refDiffMap ;
15
15
16
16
public ReferenceDiffCache () {
17
17
this .refDiffMap = new HashMap <>();
18
18
}
19
19
20
- private Optional <D > getFromCache (String leftRef , String rightRef ) {
21
- Optional <Map <String , D >> changedSchemaMap = Optional .ofNullable (refDiffMap .get (leftRef ));
22
- if (changedSchemaMap .isPresent ()) {
23
- return Optional .ofNullable (changedSchemaMap .get ().get (rightRef ));
24
- }
25
- return Optional .empty ();
20
+ private Optional <D > getFromCache (CacheKey cacheKey ) {
21
+ return Optional .ofNullable (refDiffMap .get (cacheKey ));
26
22
}
27
23
28
- private void addToCache (String leftRef , String rightRef , D changed ) {
29
- Map <String , D > changedSchemaMap = refDiffMap .computeIfAbsent (leftRef , k -> new HashMap <>());
30
- changedSchemaMap .put (rightRef , changed );
24
+ private void addToCache (CacheKey cacheKey , D changed ) {
25
+ refDiffMap .put (cacheKey , changed );
31
26
}
32
27
33
- // public Optional<D> cachedDiff(HashSet<String> refSet, C left, C right, String leftRef, String rightRef) {
34
- // return cachedDiff(refSet, left, right, leftRef, rightRef, null);
35
- // }
36
- //
37
28
public Optional <D > cachedDiff (HashSet <String > refSet , C left , C right , String leftRef , String rightRef , DiffContext context ) {
38
29
boolean areBothRefParameters = leftRef != null && rightRef != null ;
39
30
if (areBothRefParameters ) {
40
- Optional <D > changedFromRef = getFromCache (leftRef , rightRef );
31
+ CacheKey key = new CacheKey (leftRef , rightRef , context );
32
+ Optional <D > changedFromRef = getFromCache (key );
41
33
if (changedFromRef .isPresent ()) {
42
34
return changedFromRef ;
43
35
} else {
@@ -47,9 +39,8 @@ public Optional<D> cachedDiff(HashSet<String> refSet, C left, C right, String le
47
39
} else {
48
40
refSet .add (refKey );
49
41
Optional <D > changed = computeDiff (refSet , left , right , context );
50
- addToCache (leftRef , rightRef , changed .orElse (null ));
42
+ addToCache (key , changed .orElse (null ));
51
43
refSet .remove (refKey );
52
-
53
44
return changed ;
54
45
}
55
46
}
0 commit comments