17
17
package org .springframework .cache .jcache ;
18
18
19
19
import java .util .concurrent .Callable ;
20
+ import java .util .function .Function ;
20
21
21
22
import javax .cache .Cache ;
22
23
import javax .cache .processor .EntryProcessor ;
@@ -42,6 +43,8 @@ public class JCacheCache extends AbstractValueAdaptingCache {
42
43
43
44
private final Cache <Object , Object > cache ;
44
45
46
+ private final ValueLoaderEntryProcessor valueLoaderEntryProcessor ;
47
+
45
48
46
49
/**
47
50
* Create a {@code JCacheCache} instance.
@@ -60,6 +63,8 @@ public JCacheCache(Cache<Object, Object> jcache, boolean allowNullValues) {
60
63
super (allowNullValues );
61
64
Assert .notNull (jcache , "Cache must not be null" );
62
65
this .cache = jcache ;
66
+ this .valueLoaderEntryProcessor = new ValueLoaderEntryProcessor (
67
+ this ::fromStoreValue , this ::toStoreValue );
63
68
}
64
69
65
70
@@ -81,9 +86,10 @@ protected Object lookup(Object key) {
81
86
82
87
@ Override
83
88
@ Nullable
89
+ @ SuppressWarnings ("unchecked" )
84
90
public <T > T get (Object key , Callable <T > valueLoader ) {
85
91
try {
86
- return this .cache .invoke (key , new ValueLoaderEntryProcessor < T >() , valueLoader );
92
+ return ( T ) this .cache .invoke (key , this . valueLoaderEntryProcessor , valueLoader );
87
93
}
88
94
catch (EntryProcessorException ex ) {
89
95
throw new ValueRetrievalException (key , valueLoader , ex .getCause ());
@@ -141,26 +147,37 @@ public Object process(MutableEntry<Object, Object> entry, Object... arguments) t
141
147
}
142
148
143
149
144
- private class ValueLoaderEntryProcessor <T > implements EntryProcessor <Object , Object , T > {
150
+ private static final class ValueLoaderEntryProcessor implements EntryProcessor <Object , Object , Object > {
151
+
152
+ private final Function <Object , Object > fromStoreValue ;
153
+
154
+ private final Function <Object , Object > toStoreValue ;
155
+
156
+ private ValueLoaderEntryProcessor (Function <Object , Object > fromStoreValue ,
157
+ Function <Object , Object > toStoreValue ) {
158
+
159
+ this .fromStoreValue = fromStoreValue ;
160
+ this .toStoreValue = toStoreValue ;
161
+ }
145
162
146
- @ SuppressWarnings ("unchecked" )
147
163
@ Override
148
164
@ Nullable
149
- public T process (MutableEntry <Object , Object > entry , Object ... arguments ) throws EntryProcessorException {
150
- Callable <T > valueLoader = (Callable <T >) arguments [0 ];
165
+ @ SuppressWarnings ("unchecked" )
166
+ public Object process (MutableEntry <Object , Object > entry , Object ... arguments ) throws EntryProcessorException {
167
+ Callable <Object > valueLoader = (Callable <Object >) arguments [0 ];
151
168
if (entry .exists ()) {
152
- return ( T ) fromStoreValue (entry .getValue ());
169
+ return this . fromStoreValue . apply (entry .getValue ());
153
170
}
154
171
else {
155
- T value ;
172
+ Object value ;
156
173
try {
157
174
value = valueLoader .call ();
158
175
}
159
176
catch (Exception ex ) {
160
177
throw new EntryProcessorException ("Value loader '" + valueLoader + "' failed " +
161
178
"to compute value for key '" + entry .getKey () + "'" , ex );
162
179
}
163
- entry .setValue (toStoreValue (value ));
180
+ entry .setValue (this . toStoreValue . apply (value ));
164
181
return value ;
165
182
}
166
183
}
0 commit comments