1 /*
2 * (C) Copyright 1989-2011 Trend Micro, Inc.
3 * All Rights Reserved.
4 *
5 * This program is an unpublished copyrighted work which is proprietary
6 * to Trend Micro, Inc. and contains confidential information that is not
7 * to be reproduced or disclosed to any other person or entity without
8 * prior written consent from Trend Micro, Inc. in each and every instance.
9 *
10 * WARNING: Unauthorized reproduction of this program as well as
11 * unauthorized preparation of derivative works based upon the
12 * program or distribution of copies by sale, rental, lease or
13 * lending are violations of federal copyright laws and state trade
14 * secret laws, punishable by civil and criminal penalties.
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 * Defines a key and value translator used to build translated cache source and destinations.
26 */
27 public abstract class Translator<K, V, DK, DV> {
28
29 private static final Logger log = LoggerFactory.getLogger(Translator.class);
30
31 /**
32 * Constructs a new source using this translator.
33 *
34 * @param source the source to translate.
35 * @return a translated source.
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 * Constructs a new destination using this translator.
45 *
46 * @param destination the destination to translate.
47 * @return a translated destination.
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 * Converts a key (and optional value) to the key to use when asking the delegate.
57 *
58 * @param key the cache key.
59 * @param value the cache value (may be 'null').
60 * @return a key to use on the delegate or 'null' if not compatible.
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 * Extracts the value to cache from the given entry.
74 *
75 * @param key the cache key.
76 * @param delegateKey the key to use when asking the delegate.
77 * @param value the cache value (may be 'null').
78 * @return the cache value to use for storing in the delegate.
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 * Extracts the value to return from the given values.
92 *
93 * @param key the cache key.
94 * @param delegateKey the key used on the cache delegate.
95 * @param delegateValue the value returned from the cache delegate.
96 * @return the cache value to return.
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 }