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
This commit is contained in:
Mark Paluch
2021-03-11 15:12:29 +01:00
committed by Christoph Strobl
parent bbaa63501d
commit ad5d2268ec
3 changed files with 32 additions and 4 deletions

View File

@@ -102,6 +102,19 @@ public interface KeyValueAdapter extends DisposableBean {
*/
Iterable<?> getAllOf(String keyspace);
/**
* Get all elements for given keyspace.
*
* @param type must not be {@literal null}.
* @param keyspace must not be {@literal null}.
* @return empty {@link Collection} if nothing found.
* @since 2.5
*/
@SuppressWarnings("unchecked")
default <T> Iterable<T> getAllOf(String keyspace, Class<T> type) {
return (Iterable<T>) getAllOf(keyspace);
}
/**
* Returns a {@link CloseableIterator} that iterates over all entries.
*
@@ -110,6 +123,19 @@ public interface KeyValueAdapter extends DisposableBean {
*/
CloseableIterator<Map.Entry<Object, Object>> entries(String keyspace);
/**
* Returns a {@link CloseableIterator} that iterates over all entries.
*
* @param type must not be {@literal null}.
* @param keyspace must not be {@literal null}.
* @return
* @since 2.5
*/
@SuppressWarnings("unchecked")
default <T> CloseableIterator<Map.Entry<Object, T>> entries(String keyspace, Class<T> type) {
return (CloseableIterator) entries(keyspace);
}
/**
* Remove all objects of given type.
*
@@ -129,7 +155,9 @@ public interface KeyValueAdapter extends DisposableBean {
* @param keyspace must not be {@literal null}.
* @return empty {@link Collection} if no match found.
*/
Iterable<?> find(KeyValueQuery<?> query, String keyspace);
default Iterable<?> find(KeyValueQuery<?> query, String keyspace) {
return find(query, keyspace, Object.class);
}
/**
* @param query must not be {@literal null}.

View File

@@ -237,7 +237,7 @@ public class KeyValueTemplate implements KeyValueOperations, ApplicationEventPub
return executeRequired(adapter -> {
Iterable<?> values = adapter.getAllOf(resolveKeySpace(type));
Iterable<?> values = adapter.getAllOf(resolveKeySpace(type), type);
ArrayList<T> filtered = new ArrayList<>();
for (Object candidate : values) {

View File

@@ -190,7 +190,7 @@ class KeyValueTemplateUnitTests {
template.findAll(Foo.class);
verify(adapterMock, times(1)).getAllOf(Foo.class.getName());
verify(adapterMock, times(1)).getAllOf(Foo.class.getName(), Foo.class);
}
@Test // DATACMNS-525
@@ -327,7 +327,7 @@ class KeyValueTemplateUnitTests {
void findAllOfShouldRespectTypeAliasAndFilterNonMatchingTypes() {
Collection foo = Arrays.asList(ALIASED_USING_ALIAS_FOR, SUBCLASS_OF_ALIASED_USING_ALIAS_FOR);
when(adapterMock.getAllOf("aliased")).thenReturn(foo);
when(adapterMock.getAllOf("aliased", SUBCLASS_OF_ALIASED_USING_ALIAS_FOR.getClass())).thenReturn(foo);
assertThat((Iterable) template.findAll(SUBCLASS_OF_ALIASED_USING_ALIAS_FOR.getClass()))
.contains(SUBCLASS_OF_ALIASED_USING_ALIAS_FOR);