Skip to content

Commit ad5d226

Browse files
mp911dechristophstrobl
authored andcommitted
Accept target type in KeyValueAdapter.entries(…) and getAllOf(…).
We now accept the target type in both methods to retain type hints so that the underlying adapter implementation can use this type when materializing object instances. Original Pull Request: #356
1 parent bbaa635 commit ad5d226

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

src/main/java/org/springframework/data/keyvalue/core/KeyValueAdapter.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,19 @@ public interface KeyValueAdapter extends DisposableBean {
102102
*/
103103
Iterable<?> getAllOf(String keyspace);
104104

105+
/**
106+
* Get all elements for given keyspace.
107+
*
108+
* @param type must not be {@literal null}.
109+
* @param keyspace must not be {@literal null}.
110+
* @return empty {@link Collection} if nothing found.
111+
* @since 2.5
112+
*/
113+
@SuppressWarnings("unchecked")
114+
default <T> Iterable<T> getAllOf(String keyspace, Class<T> type) {
115+
return (Iterable<T>) getAllOf(keyspace);
116+
}
117+
105118
/**
106119
* Returns a {@link CloseableIterator} that iterates over all entries.
107120
*
@@ -110,6 +123,19 @@ public interface KeyValueAdapter extends DisposableBean {
110123
*/
111124
CloseableIterator<Map.Entry<Object, Object>> entries(String keyspace);
112125

126+
/**
127+
* Returns a {@link CloseableIterator} that iterates over all entries.
128+
*
129+
* @param type must not be {@literal null}.
130+
* @param keyspace must not be {@literal null}.
131+
* @return
132+
* @since 2.5
133+
*/
134+
@SuppressWarnings("unchecked")
135+
default <T> CloseableIterator<Map.Entry<Object, T>> entries(String keyspace, Class<T> type) {
136+
return (CloseableIterator) entries(keyspace);
137+
}
138+
113139
/**
114140
* Remove all objects of given type.
115141
*
@@ -129,7 +155,9 @@ public interface KeyValueAdapter extends DisposableBean {
129155
* @param keyspace must not be {@literal null}.
130156
* @return empty {@link Collection} if no match found.
131157
*/
132-
Iterable<?> find(KeyValueQuery<?> query, String keyspace);
158+
default Iterable<?> find(KeyValueQuery<?> query, String keyspace) {
159+
return find(query, keyspace, Object.class);
160+
}
133161

134162
/**
135163
* @param query must not be {@literal null}.

src/main/java/org/springframework/data/keyvalue/core/KeyValueTemplate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ public <T> Iterable<T> findAll(Class<T> type) {
237237

238238
return executeRequired(adapter -> {
239239

240-
Iterable<?> values = adapter.getAllOf(resolveKeySpace(type));
240+
Iterable<?> values = adapter.getAllOf(resolveKeySpace(type), type);
241241

242242
ArrayList<T> filtered = new ArrayList<>();
243243
for (Object candidate : values) {

src/test/java/org/springframework/data/keyvalue/core/KeyValueTemplateUnitTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ void findAllOfShouldReturnEntireCollection() {
190190

191191
template.findAll(Foo.class);
192192

193-
verify(adapterMock, times(1)).getAllOf(Foo.class.getName());
193+
verify(adapterMock, times(1)).getAllOf(Foo.class.getName(), Foo.class);
194194
}
195195

196196
@Test // DATACMNS-525
@@ -327,7 +327,7 @@ void insertShouldRespectTypeAliasOnSubClass() {
327327
void findAllOfShouldRespectTypeAliasAndFilterNonMatchingTypes() {
328328

329329
Collection foo = Arrays.asList(ALIASED_USING_ALIAS_FOR, SUBCLASS_OF_ALIASED_USING_ALIAS_FOR);
330-
when(adapterMock.getAllOf("aliased")).thenReturn(foo);
330+
when(adapterMock.getAllOf("aliased", SUBCLASS_OF_ALIASED_USING_ALIAS_FOR.getClass())).thenReturn(foo);
331331

332332
assertThat((Iterable) template.findAll(SUBCLASS_OF_ALIASED_USING_ALIAS_FOR.getClass()))
333333
.contains(SUBCLASS_OF_ALIASED_USING_ALIAS_FOR);

0 commit comments

Comments
 (0)