1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.trendmicro.grid.acl.ds.cache.translation;
18
19 import com.trendmicro.grid.acl.ds.cache.CacheDestination;
20 import com.trendmicro.grid.acl.ds.cache.CacheSource;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24
25
26
27 public abstract class Translator<K, V, DK, DV> {
28
29 private static final Logger log = LoggerFactory.getLogger(Translator.class);
30
31
32
33
34
35
36
37 public final CacheSource<K, V> newSource(CacheSource<? extends DK, ? extends DV> source) {
38 @SuppressWarnings("unchecked")
39 final CacheSource<DK, DV> cacheSource = (CacheSource<DK, DV>) source;
40 return new TranslatingSource<K, V, DK, DV>(cacheSource, this);
41 }
42
43
44
45
46
47
48
49 public final CacheDestination<K, V> newDestination(CacheDestination<? extends DK, ? extends DV> destination) {
50 @SuppressWarnings("unchecked")
51 final CacheDestination<DK, DV> cacheDestination = (CacheDestination<DK, DV>) destination;
52 return new TranslatingDestination<K, V, DK, DV>(cacheDestination, this);
53 }
54
55
56
57
58
59
60
61
62 @SuppressWarnings("unchecked")
63 protected DK toDelegateKey(K key, V value) {
64 try {
65 return (DK) key;
66 } catch (ClassCastException e) {
67 if (log.isDebugEnabled()) log.debug("Converting key '" + key + "' to 'null' as it's not of the expected target class.", e);
68 return null;
69 }
70 }
71
72
73
74
75
76
77
78
79
80 @SuppressWarnings("unchecked")
81 protected DV toDelegateValue(K key, DK delegateKey, V value) {
82 try {
83 return (DV) value;
84 } catch (ClassCastException e) {
85 if (log.isDebugEnabled()) log.debug("Converting value '" + value + "' to 'null' as it's not of the expected target class.", e);
86 return null;
87 }
88 }
89
90
91
92
93
94
95
96
97
98 @SuppressWarnings("unchecked")
99 protected V fromDelegateValue(K key, DK delegateKey, DV delegateValue) {
100 try {
101 return (V) delegateValue;
102 } catch (ClassCastException e) {
103 if (log.isDebugEnabled())
104 log.debug("Converting delegate-value '" + delegateValue + "' to 'null' as it's not of the expected target class.", e);
105 return null;
106 }
107 }
108 }