13
13
* See the License for the specific language governing permissions and
14
14
* limitations under the License.
15
15
*/
16
+
16
17
package org .springframework .data .gemfire .repository .support ;
17
18
18
19
import java .io .Serializable ;
21
22
import java .util .HashMap ;
22
23
import java .util .List ;
23
24
import java .util .Map ;
25
+ import java .util .Objects ;
26
+ import java .util .stream .Collectors ;
24
27
25
28
import org .apache .geode .cache .Cache ;
26
29
import org .apache .geode .cache .CacheTransactionManager ;
33
36
import org .springframework .data .gemfire .repository .GemfireRepository ;
34
37
import org .springframework .data .gemfire .repository .Wrapper ;
35
38
import org .springframework .data .gemfire .repository .query .QueryString ;
39
+ import org .springframework .data .gemfire .util .CollectionUtils ;
36
40
import org .springframework .data .repository .core .EntityInformation ;
37
41
import org .springframework .util .Assert ;
38
42
50
54
*/
51
55
public class SimpleGemfireRepository <T , ID extends Serializable > implements GemfireRepository <T , ID > {
52
56
53
- private final GemfireTemplate template ;
54
57
private final EntityInformation <T , ID > entityInformation ;
55
58
59
+ private final GemfireTemplate template ;
60
+
56
61
/**
57
62
* Creates a new {@link SimpleGemfireRepository}.
58
63
*
59
64
* @param template must not be {@literal null}.
60
65
* @param entityInformation must not be {@literal null}.
61
66
*/
62
67
public SimpleGemfireRepository (GemfireTemplate template , EntityInformation <T , ID > entityInformation ) {
63
-
64
- Assert .notNull (template );
65
- Assert .notNull (entityInformation );
68
+ Assert .notNull (template , "Template must not be null" );
69
+ Assert .notNull (entityInformation , "EntityInformation must not be null" );
66
70
67
71
this .template = template ;
68
72
this .entityInformation = entityInformation ;
69
73
}
70
74
71
75
/*
72
76
* (non-Javadoc)
73
- *
74
77
* @see org.springframework.data.repository.CrudRepository#save(S)
75
78
*/
76
79
@ Override
@@ -81,12 +84,11 @@ public <U extends T> U save(U entity) {
81
84
82
85
/*
83
86
* (non-Javadoc)
84
- *
85
87
* @see org.springframework.data.repository.CrudRepository#save(java.lang.Iterable)
86
88
*/
87
89
@ Override
88
90
public <U extends T > Iterable <U > save (Iterable <U > entities ) {
89
- Map <ID , U > result = new HashMap <ID , U >();
91
+ Map <ID , U > result = new HashMap <>();
90
92
91
93
for (U entity : entities ) {
92
94
result .put (entityInformation .getId (entity ), entity );
@@ -99,19 +101,17 @@ public <U extends T> Iterable<U> save(Iterable<U> entities) {
99
101
100
102
/*
101
103
* (non-Javadoc)
102
- *
103
- * @see
104
- * org.springframework.data.gemfire.repository.GemfireRepository#save(
105
- * org.springframework.data.gemfire.repository.Wrapper)
104
+ * @see org.springframework.data.gemfire.repository.GemfireRepository#save(org.springframework.data.gemfire.repository.Wrapper)
106
105
*/
107
106
@ Override
108
107
public T save (Wrapper <T , ID > wrapper ) {
109
- return template .put (wrapper .getKey (), wrapper .getEntity ());
108
+ T entity = wrapper .getEntity ();
109
+ template .put (wrapper .getKey (), entity );
110
+ return entity ;
110
111
}
111
112
112
113
/*
113
114
* (non-Javadoc)
114
- *
115
115
* @see org.springframework.data.repository.CrudRepository#count()
116
116
*/
117
117
@ Override
@@ -122,7 +122,6 @@ public long count() {
122
122
123
123
/*
124
124
* (non-Javadoc)
125
- *
126
125
* @see org.springframework.data.repository.CrudRepository#exists(java.io.Serializable)
127
126
*/
128
127
@ Override
@@ -132,7 +131,6 @@ public boolean exists(ID id) {
132
131
133
132
/*
134
133
* (non-Javadoc)
135
- *
136
134
* @see org.springframework.data.repository.CrudRepository#findOne(java.io.Serializable)
137
135
*/
138
136
@ Override
@@ -143,7 +141,6 @@ public T findOne(ID id) {
143
141
144
142
/*
145
143
* (non-Javadoc)
146
- *
147
144
* @see org.springframework.data.repository.CrudRepository#findAll()
148
145
*/
149
146
@ Override
@@ -169,24 +166,23 @@ public Iterable<T> findAll(Sort sort) {
169
166
170
167
/*
171
168
* (non-Javadoc)
172
- *
173
169
* @see org.springframework.data.repository.CrudRepository#findAll(java.lang.Iterable)
174
170
*/
175
171
@ Override
176
172
@ SuppressWarnings ("unchecked" )
177
173
public Collection <T > findAll (Iterable <ID > ids ) {
178
- List <ID > parameters = new ArrayList <ID >();
174
+ List <ID > parameters = new ArrayList <>();
179
175
180
176
for (ID id : ids ) {
181
177
parameters .add (id );
182
178
}
183
179
184
- return (Collection <T >) template .getAll (parameters ).values ();
180
+ return CollectionUtils .<ID , T >nullSafeMap (template .getAll (parameters )).values ().stream ()
181
+ .filter (Objects ::nonNull ).collect (Collectors .toList ());
185
182
}
186
183
187
184
/*
188
185
* (non-Javadoc)
189
- *
190
186
* @see org.springframework.data.repository.CrudRepository#delete(java.io.Serializable)
191
187
*/
192
188
@ Override
@@ -196,9 +192,7 @@ public void delete(ID id) {
196
192
197
193
/*
198
194
* (non-Javadoc)
199
- *
200
- * @see
201
- * org.springframework.data.repository.CrudRepository#delete(java.lang.Object)
195
+ * @see org.springframework.data.repository.CrudRepository#delete(java.lang.Object)
202
196
*/
203
197
@ Override
204
198
public void delete (T entity ) {
@@ -207,9 +201,7 @@ public void delete(T entity) {
207
201
208
202
/*
209
203
* (non-Javadoc)
210
- *
211
- * @see
212
- * org.springframework.data.repository.CrudRepository#delete(java.lang.Iterable)
204
+ * @see org.springframework.data.repository.CrudRepository#delete(java.lang.Iterable)
213
205
*/
214
206
@ Override
215
207
public void delete (Iterable <? extends T > entities ) {
@@ -220,76 +212,66 @@ public void delete(Iterable<? extends T> entities) {
220
212
221
213
/*
222
214
* (non-Javadoc)
223
- *
224
215
* @see org.apache.geode.cache.Region#getAttributes()
225
216
* @see org.apache.geode.cache.RegionAttributes#getDataPolicy()
226
217
*/
227
- boolean isPartitioned (final Region region ) {
218
+ boolean isPartitioned (Region region ) {
228
219
return (region != null && region .getAttributes () != null
229
220
&& isPartitioned (region .getAttributes ().getDataPolicy ()));
230
221
}
231
222
232
223
/*
233
224
* (non-Javadoc)
234
- *
235
225
* @see org.apache.geode.cache.DataPolicy#withPartitioning()
236
226
*/
237
- boolean isPartitioned (final DataPolicy dataPolicy ) {
227
+ boolean isPartitioned (DataPolicy dataPolicy ) {
238
228
return (dataPolicy != null && dataPolicy .withPartitioning ());
239
229
}
240
230
241
231
/*
242
232
* (non-Javadoc)
243
- *
244
233
* @see org.apache.geode.cache.Region#getRegionService()
245
234
* @see org.apache.geode.cache.Cache#getCacheTransactionManager()
246
235
*/
247
- boolean isTransactionPresent (final Region region ) {
236
+ boolean isTransactionPresent (Region region ) {
248
237
return (region .getRegionService () instanceof Cache
249
238
&& isTransactionPresent (((Cache ) region .getRegionService ()).getCacheTransactionManager ()));
250
239
}
251
240
252
241
/*
253
242
* (non-Javadoc)
254
- *
255
243
* @see org.apache.geode.cache.CacheTransactionManager#exists()
256
244
*/
257
- boolean isTransactionPresent (final CacheTransactionManager cacheTransactionManager ) {
245
+ boolean isTransactionPresent (CacheTransactionManager cacheTransactionManager ) {
258
246
return (cacheTransactionManager != null && cacheTransactionManager .exists ());
259
247
}
260
248
261
249
/* (non-Javadoc) */
262
250
@ SuppressWarnings ("unchecked" )
263
- void doRegionClear (final Region region ) {
251
+ void doRegionClear (Region region ) {
264
252
region .removeAll (region .keySet ());
265
253
}
266
254
267
255
/*
268
256
* (non-Javadoc)
269
- *
270
257
* @see org.springframework.data.repository.CrudRepository#deleteAll()
271
258
*/
272
259
@ Override
273
260
public void deleteAll () {
274
- template .execute (new GemfireCallback <Void >() {
275
- @ Override
276
- @ SuppressWarnings ("rawtypes" )
277
- public Void doInGemfire (final Region region ) {
278
- if (isPartitioned (region ) || isTransactionPresent (region )) {
279
- doRegionClear (region );
261
+ template .execute ((GemfireCallback <Void >) region -> {
262
+ if (isPartitioned (region ) || isTransactionPresent (region )) {
263
+ doRegionClear (region );
264
+ }
265
+ else {
266
+ try {
267
+ region .clear ();
280
268
}
281
- else {
282
- try {
283
- region .clear ();
284
- }
285
- catch (UnsupportedOperationException ignore ) {
286
- doRegionClear (region );
287
- }
269
+ catch (UnsupportedOperationException ignore ) {
270
+ doRegionClear (region );
288
271
}
289
-
290
- return null ;
291
272
}
273
+
274
+ return null ;
292
275
});
293
276
}
294
-
295
277
}
0 commit comments