1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.settings4j.connector;
19
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.Map;
23
24 import org.settings4j.Connector;
25 import org.settings4j.ContentResolver;
26 import org.settings4j.ObjectResolver;
27
28
29
30
31
32
33
34
35
36 public class CachedConnectorWrapper implements Connector {
37
38 private final Connector targetConnector;
39
40 private final Map<String, String> cachedStrings = Collections.synchronizedMap(new HashMap<String, String>());
41 private final Map<String, byte[]> cachedContents = Collections.synchronizedMap(new HashMap<String, byte[]>());
42 private final Map<String, Object> cachedObjects = Collections.synchronizedMap(new HashMap<String, Object>());
43
44
45
46
47
48 public CachedConnectorWrapper(final Connector targetConnector) {
49 super();
50 this.targetConnector = targetConnector;
51 }
52
53
54 public byte[] getContent(final String key) {
55 byte[] result = this.cachedContents.get(key);
56 if (result != null) {
57 return result;
58 }
59
60 result = this.targetConnector.getContent(key);
61
62 if (result != null) {
63 this.cachedContents.put(key, result);
64 }
65 return result;
66 }
67
68
69 public Object getObject(final String key) {
70 Object result = this.cachedObjects.get(key);
71 if (result != null) {
72 return result;
73 }
74
75 result = this.targetConnector.getObject(key);
76
77 if (result != null) {
78 this.cachedObjects.put(key, result);
79 }
80 return result;
81 }
82
83
84 public String getString(final String key) {
85 String result = this.cachedStrings.get(key);
86 if (result != null) {
87 return result;
88 }
89
90 result = this.targetConnector.getString(key);
91
92 if (result != null) {
93 this.cachedStrings.put(key, result);
94 }
95 return result;
96 }
97
98
99
100
101 public void clearCachedValue(final String key) {
102 this.cachedStrings.remove(key);
103 this.cachedContents.remove(key);
104 this.cachedObjects.remove(key);
105 }
106
107
108
109
110
111
112 public void addConnector(final Connector connector) {
113 this.targetConnector.addConnector(connector);
114 }
115
116
117 public void setContentResolver(final ContentResolver contentResolver) {
118 this.targetConnector.setContentResolver(contentResolver);
119 }
120
121
122 public void setObjectResolver(final ObjectResolver objectResolver) {
123 this.targetConnector.setObjectResolver(objectResolver);
124 }
125
126
127 public void init() {
128 this.targetConnector.init();
129 }
130
131
132 public String getName() {
133 return this.targetConnector.getName();
134 }
135
136
137 public void setName(final String name) {
138 this.targetConnector.setName(name);
139 }
140
141 }