DATAMONGO-1590 - Polishing.

Removed some compiler warnings. Hide newly introduced class in package scope and made use of Lombok annotations to avoid boilerplate code.

Original pull request: #436.
This commit is contained in:
Oliver Gierke
2017-01-18 19:41:11 +01:00
parent 822e29524d
commit 45818f26b3
3 changed files with 22 additions and 29 deletions

View File

@@ -41,15 +41,16 @@ final class MongoEntityInformationSupport {
* @param idType can be {@literal null}.
* @return never {@literal null}.
*/
@SuppressWarnings("unchecked")
static <T, ID extends Serializable> MongoEntityInformation<T, ID> entityInformationFor(
MongoPersistentEntity<?> entity, Class<?> idType) {
Assert.notNull(entity, "Entity must not be null!");
MappingMongoEntityInformation entityInformation = new MappingMongoEntityInformation<T, ID>(
MappingMongoEntityInformation<T, ID> entityInformation = new MappingMongoEntityInformation<T, ID>(
(MongoPersistentEntity<T>) entity, (Class<ID>) idType);
return ClassUtils.isAssignable(Persistable.class, entity.getType())
? new PersistableMongoEntityInformation<T, ID>(entityInformation) : entityInformation;
}
}

View File

@@ -15,6 +15,9 @@
*/
package org.springframework.data.mongodb.repository.support;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import java.io.Serializable;
import org.springframework.data.domain.Persistable;
@@ -26,15 +29,13 @@ import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
* {@link Persistable#isNew()} and {@link Persistable#getId()} implementations.
*
* @author Christoph Strobl
* @author Oliver Gierke
* @since 1.10
*/
public class PersistableMongoEntityInformation<T, ID extends Serializable> implements MongoEntityInformation<T, ID> {
@RequiredArgsConstructor
class PersistableMongoEntityInformation<T, ID extends Serializable> implements MongoEntityInformation<T, ID> {
private final MongoEntityInformation<T, ID> delegate;
public PersistableMongoEntityInformation(MongoEntityInformation<T, ID> delegate) {
this.delegate = delegate;
}
private final @NonNull MongoEntityInformation<T, ID> delegate;
/*
* (non-Javadoc)
@@ -59,10 +60,11 @@ public class PersistableMongoEntityInformation<T, ID extends Serializable> imple
* @see org.springframework.data.repository.core.EntityInformation#isNew(java.lang.Object)
*/
@Override
@SuppressWarnings("unchecked")
public boolean isNew(T t) {
if (t instanceof Persistable) {
return ((Persistable) t).isNew();
return ((Persistable<ID>) t).isNew();
}
return delegate.isNew(t);
@@ -73,10 +75,11 @@ public class PersistableMongoEntityInformation<T, ID extends Serializable> imple
* @see org.springframework.data.repository.core.EntityInformation#getId(java.lang.Object)
*/
@Override
@SuppressWarnings("unchecked")
public ID getId(T t) {
if (t instanceof Persistable) {
return (ID) ((Persistable) t).getId();
return (ID) ((Persistable<ID>) t).getId();
}
return delegate.getId(t);

View File

@@ -13,12 +13,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mongodb.repository.query;
package org.springframework.data.mongodb.repository.support;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import lombok.Value;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -26,13 +28,12 @@ import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.data.domain.Persistable;
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
import org.springframework.data.mongodb.repository.support.MappingMongoEntityInformation;
import org.springframework.data.mongodb.repository.support.PersistableMongoEntityInformation;
/**
* Tests for {@link PersistableMongoEntityInformation}.
*
* @author Christoph Strobl
* @author Oliver Gierke
*/
@RunWith(MockitoJUnitRunner.class)
public class PersistableMappingMongoEntityInformationUnitTests {
@@ -53,24 +54,12 @@ public class PersistableMappingMongoEntityInformationUnitTests {
assertThat(information.isNew(new TypeImplementingPersistable(100L, false)), is(false));
}
@Value
static class TypeImplementingPersistable implements Persistable<Long> {
final Long id;
final boolean isNew;
private static final long serialVersionUID = -1619090149320971099L;
public TypeImplementingPersistable(Long id, boolean isNew) {
this.id = id;
this.isNew = isNew;
}
@Override
public Long getId() {
return id;
}
@Override
public boolean isNew() {
return isNew;
}
Long id;
boolean isNew;
}
}