From 94e63081233c29fe45ec1f0f05a99030e48611fc Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Wed, 25 Nov 2020 11:47:28 +0100 Subject: [PATCH] DATACOUCH-650 - Polishing. Reorder methods. Remove superfluous final keyword. Reformat pom. Fix dependency to Spring Data Commons. Original pull request: #279. --- pom.xml | 6 +- .../support/SimpleCouchbaseRepository.java | 45 +++++++------ .../SimpleReactiveCouchbaseRepository.java | 65 ++++++++----------- 3 files changed, 54 insertions(+), 62 deletions(-) diff --git a/pom.xml b/pom.xml index 76cb32a8..8dfadf76 100644 --- a/pom.xml +++ b/pom.xml @@ -20,7 +20,7 @@ 3.0.9 3.0.9 - 2.4.0-DATACMNS-800-SNAPSHOT + 2.5.0-SNAPSHOT spring.data.couchbase @@ -41,10 +41,12 @@ org.springframework spring-context-support + org.springframework spring-web + org.springframework spring-tx @@ -174,12 +176,14 @@ ${kotlin} true + org.jetbrains.kotlin kotlin-reflect ${kotlin} true + org.jetbrains.kotlin kotlin-test diff --git a/src/main/java/org/springframework/data/couchbase/repository/support/SimpleCouchbaseRepository.java b/src/main/java/org/springframework/data/couchbase/repository/support/SimpleCouchbaseRepository.java index 5e8d569f..bfead97d 100644 --- a/src/main/java/org/springframework/data/couchbase/repository/support/SimpleCouchbaseRepository.java +++ b/src/main/java/org/springframework/data/couchbase/repository/support/SimpleCouchbaseRepository.java @@ -16,6 +16,8 @@ package org.springframework.data.couchbase.repository.support; +import static org.springframework.data.couchbase.repository.support.Util.*; + import java.util.Collection; import java.util.List; import java.util.Objects; @@ -26,7 +28,6 @@ import org.springframework.data.couchbase.core.CouchbaseOperations; import org.springframework.data.couchbase.core.query.Query; import org.springframework.data.couchbase.repository.CouchbaseRepository; import org.springframework.data.couchbase.repository.query.CouchbaseEntityInformation; -import static org.springframework.data.couchbase.repository.support.Util.hasNonZeroVersionProperty; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.Pageable; @@ -64,8 +65,8 @@ public class SimpleCouchbaseRepository implements CouchbaseRepository entityInformation, - final CouchbaseOperations couchbaseOperations) { + public SimpleCouchbaseRepository(CouchbaseEntityInformation entityInformation, + CouchbaseOperations couchbaseOperations) { Assert.notNull(entityInformation, "CouchbaseEntityInformation must not be null!"); Assert.notNull(couchbaseOperations, "CouchbaseOperations must not be null!"); @@ -75,7 +76,7 @@ public class SimpleCouchbaseRepository implements CouchbaseRepository S save(final S entity) { + public S save(S entity) { Assert.notNull(entity, "Entity must not be null!"); // if entity has non-null, non-zero version property, then replace() if (hasNonZeroVersionProperty(entity, couchbaseOperations.getConverter())) { @@ -86,21 +87,19 @@ public class SimpleCouchbaseRepository implements CouchbaseRepository Iterable saveAll(final Iterable entities) { + public Iterable saveAll(Iterable entities) { Assert.notNull(entities, "The given Iterable of entities must not be null!"); return Streamable.of(entities).stream().map((e) -> save(e)).collect(StreamUtils.toUnmodifiableList()); } @Override - public Optional findById(final ID id) { + public Optional findById(ID id) { Assert.notNull(id, "The given id must not be null!"); return Optional.ofNullable(couchbaseOperations.findById(entityInformation.getJavaType()).one(id.toString())); } @Override - @SuppressWarnings("unchecked") - public List findAllById(final Iterable ids) { + public List findAllById(Iterable ids) { Assert.notNull(ids, "The given Iterable of ids must not be null!"); List convertedIds = Streamable.of(ids).stream().map(Objects::toString).collect(Collectors.toList()); Collection all = couchbaseOperations.findById(entityInformation.getJavaType()).all(convertedIds); @@ -108,35 +107,35 @@ public class SimpleCouchbaseRepository implements CouchbaseRepository entities) { - Assert.notNull(entities, "The given Iterable of entities must not be null!"); - couchbaseOperations.removeById().all(Streamable.of(entities).map(entityInformation::getId).toList()); - } - @Override public void deleteAllById(Iterable ids) { Assert.notNull(ids, "The given Iterable of ids must not be null!"); couchbaseOperations.removeById().all(Streamable.of(ids).map(Objects::toString).toList()); } + @Override + public void deleteAll(Iterable entities) { + Assert.notNull(entities, "The given Iterable of entities must not be null!"); + couchbaseOperations.removeById().all(Streamable.of(entities).map(entityInformation::getId).toList()); + } + @Override public long count() { return couchbaseOperations.findByQuery(entityInformation.getJavaType()).consistentWith(buildQueryScanConsistency()) @@ -155,17 +154,17 @@ public class SimpleCouchbaseRepository implements CouchbaseRepository findAll(final Sort sort) { + public List findAll(Sort sort) { return findAll(new Query().with(sort)); } @Override - public List findAll(final QueryScanConsistency queryScanConsistency) { + public List findAll(QueryScanConsistency queryScanConsistency) { return findAll(new Query().scanConsistency(queryScanConsistency)); } @Override - public Page findAll(final Pageable pageable) { + public Page findAll(Pageable pageable) { List results = findAll(new Query().with(pageable)); return new PageImpl<>(results, pageable, count()); } @@ -185,7 +184,7 @@ public class SimpleCouchbaseRepository implements CouchbaseRepository findAll(final Query query) { + private List findAll(Query query) { return couchbaseOperations.findByQuery(entityInformation.getJavaType()).consistentWith(buildQueryScanConsistency()) .matching(query).all(); } @@ -203,7 +202,7 @@ public class SimpleCouchbaseRepository implements CouchbaseRepository implements ReactiveCouchba * @param entityInformation the Metadata for the entity. * @param operations the reference to the reactive template used. */ - public SimpleReactiveCouchbaseRepository(final CouchbaseEntityInformation entityInformation, - final ReactiveCouchbaseOperations operations) { + public SimpleReactiveCouchbaseRepository(CouchbaseEntityInformation entityInformation, + ReactiveCouchbaseOperations operations) { Assert.notNull(operations, "ReactiveCouchbaseOperations must not be null!"); Assert.notNull(entityInformation, "CouchbaseEntityInformation must not be null!"); @@ -78,7 +80,7 @@ public class SimpleReactiveCouchbaseRepository implements ReactiveCouchba @SuppressWarnings("unchecked") @Override - public Mono save(final S entity) { + public Mono save(S entity) { Assert.notNull(entity, "Entity must not be null!"); // if entity has non-null version property, then replace() if (hasNonZeroVersionProperty(entity, operations.getConverter())) { @@ -89,52 +91,45 @@ public class SimpleReactiveCouchbaseRepository implements ReactiveCouchba } @Override - public Flux findAll(final Sort sort) { + public Flux findAll(Sort sort) { return findAll(new Query().with(sort)); } - @SuppressWarnings("unchecked") @Override - public Flux saveAll(final Iterable entities) { + public Flux saveAll(Iterable entities) { Assert.notNull(entities, "The given Iterable of entities must not be null!"); return Flux.fromIterable(entities).flatMap(this::save); } - @SuppressWarnings("unchecked") @Override - public Flux saveAll(final Publisher entityStream) { + public Flux saveAll(Publisher entityStream) { Assert.notNull(entityStream, "The given Iterable of entities must not be null!"); return Flux.from(entityStream).flatMap(this::save); } - @SuppressWarnings("unchecked") @Override - public Mono findById(final ID id) { + public Mono findById(ID id) { return operations.findById(entityInformation.getJavaType()).one(id.toString()); } - @SuppressWarnings("unchecked") @Override - public Mono findById(final Publisher publisher) { + public Mono findById(Publisher publisher) { Assert.notNull(publisher, "The given Publisher must not be null!"); return Mono.from(publisher).flatMap(this::findById); } - @SuppressWarnings("unchecked") @Override - public Mono existsById(final ID id) { + public Mono existsById(ID id) { Assert.notNull(id, "The given id must not be null!"); return operations.existsById().one(id.toString()); } - @SuppressWarnings("unchecked") @Override - public Mono existsById(final Publisher publisher) { + public Mono existsById(Publisher publisher) { Assert.notNull(publisher, "The given Publisher must not be null!"); return Mono.from(publisher).flatMap(this::existsById); } - @SuppressWarnings("unchecked") @Override public Flux findAll() { return findAll(new Query()); @@ -142,62 +137,56 @@ public class SimpleReactiveCouchbaseRepository implements ReactiveCouchba @SuppressWarnings("unchecked") @Override - public Flux findAllById(final Iterable ids) { + public Flux findAllById(Iterable ids) { Assert.notNull(ids, "The given Iterable of ids must not be null!"); List convertedIds = Streamable.of(ids).stream().map(Objects::toString).collect(Collectors.toList()); return (Flux) operations.findById(entityInformation.getJavaType()).all(convertedIds); } - @SuppressWarnings("unchecked") @Override - public Flux findAllById(final Publisher entityStream) { + public Flux findAllById(Publisher entityStream) { Assert.notNull(entityStream, "The given entityStream must not be null!"); return Flux.from(entityStream).flatMap(this::findById); } - @SuppressWarnings("unchecked") @Override - public Mono deleteById(final ID id) { + public Mono deleteById(ID id) { return operations.removeById().one(id.toString()).then(); } @Override - public Mono deleteById(final Publisher publisher) { + public Mono deleteById(Publisher publisher) { Assert.notNull(publisher, "The given id must not be null!"); return Mono.from(publisher).flatMap(this::deleteById); } - @SuppressWarnings("unchecked") @Override - public Mono delete(final T entity) { + public Mono delete(T entity) { Assert.notNull(entity, "Entity must not be null!"); return operations.removeById().one(entityInformation.getId(entity)).then(); } - @SuppressWarnings("unchecked") @Override - public Mono deleteAll(final Iterable entities) { + public Mono deleteAllById(Iterable ids) { + return operations.removeById().all(Streamable.of(ids).map(Object::toString).toList()).then(); + } + + @Override + public Mono deleteAll(Iterable entities) { return operations.removeById().all(Streamable.of(entities).map(entityInformation::getId).toList()).then(); } @Override - public Mono deleteAll(final Publisher entityStream) { + public Mono deleteAll(Publisher entityStream) { Assert.notNull(entityStream, "The given publisher of entities must not be null!"); return Flux.from(entityStream).flatMap(this::delete).single(); } - @Override - public Mono deleteAllById(final Iterable ids) { - return operations.removeById().all(Streamable.of(ids).map(Object::toString).toList()).then(); - } - - @SuppressWarnings("unchecked") @Override public Mono count() { return operations.findByQuery(entityInformation.getJavaType()).consistentWith(buildQueryScanConsistency()).count(); } - @SuppressWarnings("unchecked") @Override public Mono deleteAll() { return operations.removeByQuery(entityInformation.getJavaType()).all().then(); @@ -212,7 +201,7 @@ public class SimpleReactiveCouchbaseRepository implements ReactiveCouchba return entityInformation; } - private Flux findAll(final Query query) { + private Flux findAll(Query query) { return operations.findByQuery(entityInformation.getJavaType()).consistentWith(buildQueryScanConsistency()) .matching(query).all(); } @@ -230,7 +219,7 @@ public class SimpleReactiveCouchbaseRepository implements ReactiveCouchba * * @param crudMethodMetadata the injected repository metadata. */ - void setRepositoryMethodMetadata(final CrudMethodMetadata crudMethodMetadata) { + void setRepositoryMethodMetadata(CrudMethodMetadata crudMethodMetadata) { this.crudMethodMetadata = crudMethodMetadata; }