Skip to content

Commit eb2a58c

Browse files
committed
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: spring-projects#436.
1 parent 8c9bbf7 commit eb2a58c

File tree

3 files changed

+22
-29
lines changed

3 files changed

+22
-29
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/MongoEntityInformationSupport.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,16 @@ private MongoEntityInformationSupport() {}
4141
* @param idType can be {@literal null}.
4242
* @return never {@literal null}.
4343
*/
44+
@SuppressWarnings("unchecked")
4445
static <T, ID extends Serializable> MongoEntityInformation<T, ID> entityInformationFor(
4546
MongoPersistentEntity<?> entity, Class<?> idType) {
4647

4748
Assert.notNull(entity, "Entity must not be null!");
4849

49-
MappingMongoEntityInformation entityInformation = new MappingMongoEntityInformation<T, ID>(
50+
MappingMongoEntityInformation<T, ID> entityInformation = new MappingMongoEntityInformation<T, ID>(
5051
(MongoPersistentEntity<T>) entity, (Class<ID>) idType);
52+
5153
return ClassUtils.isAssignable(Persistable.class, entity.getType())
5254
? new PersistableMongoEntityInformation<T, ID>(entityInformation) : entityInformation;
5355
}
54-
5556
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/PersistableMongoEntityInformation.java

+11-8
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616
package org.springframework.data.mongodb.repository.support;
1717

18+
import lombok.NonNull;
19+
import lombok.RequiredArgsConstructor;
20+
1821
import java.io.Serializable;
1922

2023
import org.springframework.data.domain.Persistable;
@@ -26,15 +29,13 @@
2629
* {@link Persistable#isNew()} and {@link Persistable#getId()} implementations.
2730
*
2831
* @author Christoph Strobl
32+
* @author Oliver Gierke
2933
* @since 1.10
3034
*/
31-
public class PersistableMongoEntityInformation<T, ID extends Serializable> implements MongoEntityInformation<T, ID> {
32-
33-
private final MongoEntityInformation<T, ID> delegate;
35+
@RequiredArgsConstructor
36+
class PersistableMongoEntityInformation<T, ID extends Serializable> implements MongoEntityInformation<T, ID> {
3437

35-
public PersistableMongoEntityInformation(MongoEntityInformation<T, ID> delegate) {
36-
this.delegate = delegate;
37-
}
38+
private final @NonNull MongoEntityInformation<T, ID> delegate;
3839

3940
/*
4041
* (non-Javadoc)
@@ -59,10 +60,11 @@ public String getIdAttribute() {
5960
* @see org.springframework.data.repository.core.EntityInformation#isNew(java.lang.Object)
6061
*/
6162
@Override
63+
@SuppressWarnings("unchecked")
6264
public boolean isNew(T t) {
6365

6466
if (t instanceof Persistable) {
65-
return ((Persistable) t).isNew();
67+
return ((Persistable<ID>) t).isNew();
6668
}
6769

6870
return delegate.isNew(t);
@@ -73,10 +75,11 @@ public boolean isNew(T t) {
7375
* @see org.springframework.data.repository.core.EntityInformation#getId(java.lang.Object)
7476
*/
7577
@Override
78+
@SuppressWarnings("unchecked")
7679
public ID getId(T t) {
7780

7881
if (t instanceof Persistable) {
79-
return (ID) ((Persistable) t).getId();
82+
return (ID) ((Persistable<ID>) t).getId();
8083
}
8184

8285
return delegate.getId(t);

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/PersistableMappingMongoEntityInformationUnitTests.java

+8-19
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,27 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.data.mongodb.repository.query;
16+
package org.springframework.data.mongodb.repository.support;
1717

1818
import static org.hamcrest.CoreMatchers.*;
1919
import static org.junit.Assert.*;
2020
import static org.mockito.Mockito.*;
2121

22+
import lombok.Value;
23+
2224
import org.junit.Before;
2325
import org.junit.Test;
2426
import org.junit.runner.RunWith;
2527
import org.mockito.Mock;
2628
import org.mockito.runners.MockitoJUnitRunner;
2729
import org.springframework.data.domain.Persistable;
2830
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
29-
import org.springframework.data.mongodb.repository.support.MappingMongoEntityInformation;
30-
import org.springframework.data.mongodb.repository.support.PersistableMongoEntityInformation;
3131

3232
/**
3333
* Tests for {@link PersistableMongoEntityInformation}.
3434
*
3535
* @author Christoph Strobl
36+
* @author Oliver Gierke
3637
*/
3738
@RunWith(MockitoJUnitRunner.class)
3839
public class PersistableMappingMongoEntityInformationUnitTests {
@@ -53,24 +54,12 @@ public void considersPersistableIsNew() {
5354
assertThat(information.isNew(new TypeImplementingPersistable(100L, false)), is(false));
5455
}
5556

57+
@Value
5658
static class TypeImplementingPersistable implements Persistable<Long> {
5759

58-
final Long id;
59-
final boolean isNew;
60-
61-
public TypeImplementingPersistable(Long id, boolean isNew) {
62-
this.id = id;
63-
this.isNew = isNew;
64-
}
65-
66-
@Override
67-
public Long getId() {
68-
return id;
69-
}
60+
private static final long serialVersionUID = -1619090149320971099L;
7061

71-
@Override
72-
public boolean isNew() {
73-
return isNew;
74-
}
62+
Long id;
63+
boolean isNew;
7564
}
7665
}

0 commit comments

Comments
 (0)