DATAKV-330 - Polishing.

Reorder methods.

Original pull request: #52.
This commit is contained in:
Mark Paluch
2020-11-25 14:10:29 +01:00
parent 54c7603496
commit c0ead79916
2 changed files with 58 additions and 45 deletions

View File

@@ -30,6 +30,8 @@ import org.springframework.data.repository.core.EntityInformation;
import org.springframework.util.Assert;
/**
* Simple {@link KeyValueRepository} implementation.
*
* @author Christoph Strobl
* @author Oliver Gierke
* @author Mark Paluch
@@ -58,38 +60,9 @@ public class SimpleKeyValueRepository<T, ID> implements KeyValueRepository<T, ID
this.operations = operations;
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.PagingAndSortingRepository#findAll(org.springframework.data.domain.Sort)
*/
@Override
public Iterable<T> findAll(Sort sort) {
Assert.notNull(sort, "Sort must not be null!");
return operations.findAll(sort, entityInformation.getJavaType());
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.PagingAndSortingRepository#findAll(org.springframework.data.domain.Pageable)
*/
@Override
public Page<T> findAll(Pageable pageable) {
Assert.notNull(pageable, "Pageable must not be null!");
if (pageable.isUnpaged()) {
List<T> result = findAll();
return new PageImpl<>(result, Pageable.unpaged(), result.size());
}
Iterable<T> content = operations.findInRange(pageable.getOffset(), pageable.getPageSize(), pageable.getSort(),
entityInformation.getJavaType());
return new PageImpl<>(IterableConverter.toList(content), pageable,
this.operations.count(entityInformation.getJavaType()));
}
// -------------------------------------------------------------------------
// Methods from CrudRepository
// -------------------------------------------------------------------------
/*
* (non-Javadoc)
@@ -206,16 +179,8 @@ public class SimpleKeyValueRepository<T, ID> implements KeyValueRepository<T, ID
/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#delete(java.lang.Iterable)
* @see org.springframework.data.repository.CrudRepository#deleteAllById(java.lang.Iterable)
*/
@Override
public void deleteAll(Iterable<? extends T> entities) {
Assert.notNull(entities, "The given Iterable of entities must not be null!");
entities.forEach(this::delete);
}
@Override
public void deleteAllById(Iterable<? extends ID> ids) {
@@ -224,6 +189,18 @@ public class SimpleKeyValueRepository<T, ID> implements KeyValueRepository<T, ID
ids.forEach(this::deleteById);
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#delete(java.lang.Iterable)
*/
@Override
public void deleteAll(Iterable<? extends T> entities) {
Assert.notNull(entities, "The given Iterable of entities must not be null!");
entities.forEach(this::delete);
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#deleteAll()
@@ -232,4 +209,41 @@ public class SimpleKeyValueRepository<T, ID> implements KeyValueRepository<T, ID
public void deleteAll() {
operations.delete(entityInformation.getJavaType());
}
// -------------------------------------------------------------------------
// Methods from PagingAndSortingRepository
// -------------------------------------------------------------------------
/*
* (non-Javadoc)
* @see org.springframework.data.repository.PagingAndSortingRepository#findAll(org.springframework.data.domain.Sort)
*/
@Override
public Iterable<T> findAll(Sort sort) {
Assert.notNull(sort, "Sort must not be null!");
return operations.findAll(sort, entityInformation.getJavaType());
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.PagingAndSortingRepository#findAll(org.springframework.data.domain.Pageable)
*/
@Override
public Page<T> findAll(Pageable pageable) {
Assert.notNull(pageable, "Pageable must not be null!");
if (pageable.isUnpaged()) {
List<T> result = findAll();
return new PageImpl<>(result, Pageable.unpaged(), result.size());
}
Iterable<T> content = operations.findInRange(pageable.getOffset(), pageable.getPageSize(), pageable.getSort(),
entityInformation.getJavaType());
return new PageImpl<>(IterableConverter.toList(content), pageable,
this.operations.count(entityInformation.getJavaType()));
}
}

View File

@@ -15,7 +15,6 @@
*/
package org.springframework.data.keyvalue.repository;
import static java.util.Arrays.*;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
@@ -96,7 +95,7 @@ public class SimpleKeyValueRepositoryUnitTests {
Foo one = new Foo("one");
Foo two = new Foo("two");
repo.saveAll(asList(one, two));
repo.saveAll(Arrays.asList(one, two));
verify(opsMock, times(1)).insert(eq(one));
verify(opsMock, times(1)).insert(eq(two));
}
@@ -123,7 +122,7 @@ public class SimpleKeyValueRepositoryUnitTests {
@Test // DATAKV-330
public void deleteAllById() {
repo.deleteAllById(asList("one", "two"));
repo.deleteAllById(Arrays.asList("one", "two"));
verify(opsMock, times(1)).delete(eq("one"), eq(Foo.class));
verify(opsMock, times(1)).delete(eq("two"), eq(Foo.class));
@@ -142,7 +141,7 @@ public class SimpleKeyValueRepositoryUnitTests {
public void findAllIds() {
when(opsMock.findById(any(), any(Class.class))).thenReturn(Optional.empty());
repo.findAllById(asList("one", "two", "three"));
repo.findAllById(Arrays.asList("one", "two", "three"));
verify(opsMock, times(3)).findById(anyString(), eq(Foo.class));
}