diff --git a/src/main/java/org/springframework/data/keyvalue/core/GeneratingIdAccessor.java b/src/main/java/org/springframework/data/keyvalue/core/GeneratingIdAccessor.java index ab00989..7ec09d3 100644 --- a/src/main/java/org/springframework/data/keyvalue/core/GeneratingIdAccessor.java +++ b/src/main/java/org/springframework/data/keyvalue/core/GeneratingIdAccessor.java @@ -16,6 +16,7 @@ package org.springframework.data.keyvalue.core; import java.io.Serializable; +import java.util.Optional; import org.springframework.data.mapping.IdentifierAccessor; import org.springframework.data.mapping.PersistentProperty; @@ -60,7 +61,7 @@ class GeneratingIdAccessor implements IdentifierAccessor { * @see org.springframework.data.keyvalue.core.IdentifierAccessor#getIdentifier() */ @Override - public Object getIdentifier() { + public Optional getIdentifier() { return accessor.getProperty(identifierProperty); } @@ -72,14 +73,14 @@ class GeneratingIdAccessor implements IdentifierAccessor { */ public Object getOrGenerateIdentifier() { - Serializable existingIdentifier = (Serializable) getIdentifier(); + Optional existingIdentifier = getIdentifier(); - if (existingIdentifier != null) { - return existingIdentifier; + if (existingIdentifier.isPresent()) { + return existingIdentifier.get(); } Object generatedIdentifier = generator.generateIdentifierOfType(identifierProperty.getTypeInformation()); - accessor.setProperty(identifierProperty, generatedIdentifier); + accessor.setProperty(identifierProperty, Optional.ofNullable(generatedIdentifier)); return generatedIdentifier; } diff --git a/src/main/java/org/springframework/data/keyvalue/core/KeyValueOperations.java b/src/main/java/org/springframework/data/keyvalue/core/KeyValueOperations.java index 3e17f71..a16493b 100644 --- a/src/main/java/org/springframework/data/keyvalue/core/KeyValueOperations.java +++ b/src/main/java/org/springframework/data/keyvalue/core/KeyValueOperations.java @@ -16,6 +16,7 @@ package org.springframework.data.keyvalue.core; import java.io.Serializable; +import java.util.Optional; import org.springframework.beans.factory.DisposableBean; import org.springframework.data.domain.Sort; @@ -73,7 +74,7 @@ public interface KeyValueOperations extends DisposableBean { * @param type must not be {@literal null}. * @return null if not found. */ - T findById(Serializable id, Class type); + Optional findById(Serializable id, Class type); /** * Execute operation against underlying store. @@ -102,7 +103,7 @@ public interface KeyValueOperations extends DisposableBean { * @param type must not be {@literal null}. * @return */ - Iterable findInRange(int offset, int rows, Class type); + Iterable findInRange(long offset, int rows, Class type); /** * Get all elements in given range ordered by sort. Respects {@link KeySpace} if present and therefore returns all @@ -114,7 +115,7 @@ public interface KeyValueOperations extends DisposableBean { * @param type * @return */ - Iterable findInRange(int offset, int rows, Sort sort, Class type); + Iterable findInRange(long offset, int rows, Sort sort, Class type); /** * @param objectToUpdate must not be {@literal null}. diff --git a/src/main/java/org/springframework/data/keyvalue/core/KeyValueTemplate.java b/src/main/java/org/springframework/data/keyvalue/core/KeyValueTemplate.java index 452fbc6..d06eb84 100644 --- a/src/main/java/org/springframework/data/keyvalue/core/KeyValueTemplate.java +++ b/src/main/java/org/springframework/data/keyvalue/core/KeyValueTemplate.java @@ -19,6 +19,7 @@ import java.io.Serializable; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Optional; import java.util.Set; import org.springframework.context.ApplicationEventPublisher; @@ -33,12 +34,11 @@ import org.springframework.data.keyvalue.core.mapping.KeyValuePersistentEntity; import org.springframework.data.keyvalue.core.mapping.KeyValuePersistentProperty; import org.springframework.data.keyvalue.core.mapping.context.KeyValueMappingContext; import org.springframework.data.keyvalue.core.query.KeyValueQuery; -import org.springframework.data.mapping.PersistentEntity; -import org.springframework.data.mapping.PersistentProperty; import org.springframework.data.mapping.context.MappingContext; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; import org.springframework.util.CollectionUtils; +import org.springframework.util.ObjectUtils; /** * Basic implementation of {@link KeyValueOperations}. @@ -131,16 +131,25 @@ public class KeyValueTemplate implements KeyValueOperations, ApplicationEventPub @Override public T insert(T objectToInsert) { - PersistentEntity entity = this.mappingContext.getPersistentEntity(ClassUtils.getUserClass(objectToInsert)); + KeyValuePersistentEntity entity = getKeyValuePersistentEntity(objectToInsert); GeneratingIdAccessor generatingIdAccessor = new GeneratingIdAccessor(entity.getPropertyAccessor(objectToInsert), - entity.getIdProperty(), identifierGenerator); + entity.getIdProperty() + .orElseThrow(() -> new IllegalArgumentException("Unable to extract 'id' for object to be deleted")), + identifierGenerator); Object id = generatingIdAccessor.getOrGenerateIdentifier(); insert((Serializable) id, objectToInsert); return objectToInsert; } + private KeyValuePersistentEntity getKeyValuePersistentEntity(Object objectToInsert) { + + return this.mappingContext.getPersistentEntity(ClassUtils.getUserClass(objectToInsert)) + .orElseThrow(() -> new IllegalArgumentException( + String.format("Unable to find PersistentEntity for %s", ObjectUtils.nullSafeClassName(objectToInsert)))); + } + /* * (non-Javadoc) * @see org.springframework.data.keyvalue.core.KeyValueOperations#insert(java.io.Serializable, java.lang.Object) @@ -161,8 +170,8 @@ public class KeyValueTemplate implements KeyValueOperations, ApplicationEventPub public Void doInKeyValue(KeyValueAdapter adapter) { if (adapter.contains(id, keyspace)) { - throw new DuplicateKeyException(String.format( - "Cannot insert existing object with id %s!. Please use update.", id)); + throw new DuplicateKeyException( + String.format("Cannot insert existing object with id %s!. Please use update.", id)); } adapter.put(id, objectToInsert, keyspace); @@ -181,15 +190,14 @@ public class KeyValueTemplate implements KeyValueOperations, ApplicationEventPub @Override public void update(Object objectToUpdate) { - PersistentEntity entity = this.mappingContext.getPersistentEntity(ClassUtils - .getUserClass(objectToUpdate)); + KeyValuePersistentEntity entity = getKeyValuePersistentEntity(objectToUpdate); if (!entity.hasIdProperty()) { - throw new InvalidDataAccessApiUsageException(String.format("Cannot determine id for type %s", - ClassUtils.getUserClass(objectToUpdate))); + throw new InvalidDataAccessApiUsageException( + String.format("Cannot determine id for type %s", ClassUtils.getUserClass(objectToUpdate))); } - update((Serializable) entity.getIdentifierAccessor(objectToUpdate).getIdentifier(), objectToUpdate); + update((Serializable) entity.getIdentifierAccessor(objectToUpdate).getIdentifier().get(), objectToUpdate); } /* @@ -214,8 +222,8 @@ public class KeyValueTemplate implements KeyValueOperations, ApplicationEventPub } }); - potentiallyPublishEvent(KeyValueEvent - .afterUpdate(id, keyspace, objectToUpdate.getClass(), objectToUpdate, existing)); + potentiallyPublishEvent( + KeyValueEvent.afterUpdate(id, keyspace, objectToUpdate.getClass(), objectToUpdate, existing)); } /* @@ -256,7 +264,7 @@ public class KeyValueTemplate implements KeyValueOperations, ApplicationEventPub * @see org.springframework.data.keyvalue.core.KeyValueOperations#findById(java.io.Serializable, java.lang.Class) */ @Override - public T findById(final Serializable id, final Class type) { + public Optional findById(final Serializable id, final Class type) { Assert.notNull(id, "Id for object to be inserted must not be null!"); Assert.notNull(type, "Type to fetch must not be null!"); @@ -283,7 +291,7 @@ public class KeyValueTemplate implements KeyValueOperations, ApplicationEventPub potentiallyPublishEvent(KeyValueEvent.afterGet(id, keyspace, type, result)); - return result; + return Optional.ofNullable(result); } /* @@ -321,9 +329,10 @@ public class KeyValueTemplate implements KeyValueOperations, ApplicationEventPub public T delete(T objectToDelete) { Class type = (Class) ClassUtils.getUserClass(objectToDelete); - PersistentEntity entity = this.mappingContext.getPersistentEntity(type); + KeyValuePersistentEntity entity = getKeyValuePersistentEntity(objectToDelete); - return delete((Serializable) entity.getIdentifierAccessor(objectToDelete).getIdentifier(), type); + return delete((Serializable) entity.getIdentifierAccessor(objectToDelete).getIdentifier() + .orElseThrow(() -> new IllegalArgumentException("Unable to extract 'id' for object to be deleted")), type); } /* @@ -423,21 +432,21 @@ public class KeyValueTemplate implements KeyValueOperations, ApplicationEventPub /* * (non-Javadoc) - * @see org.springframework.data.keyvalue.core.KeyValueOperations#findInRange(int, int, java.lang.Class) + * @see org.springframework.data.keyvalue.core.KeyValueOperations#findInRange(long, int, java.lang.Class) */ @SuppressWarnings("rawtypes") @Override - public Iterable findInRange(int offset, int rows, Class type) { + public Iterable findInRange(long offset, int rows, Class type) { return find(new KeyValueQuery().skip(offset).limit(rows), type); } /* * (non-Javadoc) - * @see org.springframework.data.keyvalue.core.KeyValueOperations#findInRange(int, int, org.springframework.data.domain.Sort, java.lang.Class) + * @see org.springframework.data.keyvalue.core.KeyValueOperations#findInRange(long, int, org.springframework.data.domain.Sort, java.lang.Class) */ @SuppressWarnings("rawtypes") @Override - public Iterable findInRange(int offset, int rows, Sort sort, Class type) { + public Iterable findInRange(long offset, int rows, Sort sort, Class type) { return find(new KeyValueQuery(sort).skip(offset).limit(rows), type); } @@ -476,7 +485,7 @@ public class KeyValueTemplate implements KeyValueOperations, ApplicationEventPub } private String resolveKeySpace(Class type) { - return this.mappingContext.getPersistentEntity(type).getKeySpace(); + return this.mappingContext.getPersistentEntity(type).get().getKeySpace(); } private RuntimeException resolveExceptionIfPossible(RuntimeException e) { diff --git a/src/main/java/org/springframework/data/keyvalue/core/QueryEngine.java b/src/main/java/org/springframework/data/keyvalue/core/QueryEngine.java index ab1dde6..fce9358 100644 --- a/src/main/java/org/springframework/data/keyvalue/core/QueryEngine.java +++ b/src/main/java/org/springframework/data/keyvalue/core/QueryEngine.java @@ -92,7 +92,7 @@ public abstract class QueryEngine execute(CRITERIA criteria, SORT sort, int offset, int rows, Serializable keyspace); + public abstract Collection execute(CRITERIA criteria, SORT sort, long offset, int rows, Serializable keyspace); /** * @param criteria @@ -104,7 +104,7 @@ public abstract class QueryEngine Collection execute(CRITERIA criteria, SORT sort, int offset, int rows, Serializable keyspace, + public Collection execute(CRITERIA criteria, SORT sort, long offset, int rows, Serializable keyspace, Class type) { return (Collection) execute(criteria, sort, offset, rows, keyspace); } diff --git a/src/main/java/org/springframework/data/keyvalue/core/SpelQueryEngine.java b/src/main/java/org/springframework/data/keyvalue/core/SpelQueryEngine.java index e9875dc..6117ef5 100644 --- a/src/main/java/org/springframework/data/keyvalue/core/SpelQueryEngine.java +++ b/src/main/java/org/springframework/data/keyvalue/core/SpelQueryEngine.java @@ -51,7 +51,7 @@ class SpelQueryEngine extends QueryEngine execute(SpelCriteria criteria, Comparator sort, int offset, int rows, Serializable keyspace) { + public Collection execute(SpelCriteria criteria, Comparator sort, long offset, int rows, Serializable keyspace) { return sortAndFilterMatchingRange(getAdapter().getAllOf(keyspace), criteria, sort, offset, rows); } @@ -65,7 +65,7 @@ class SpelQueryEngine extends QueryEngine sortAndFilterMatchingRange(Iterable source, SpelCriteria criteria, Comparator sort, int offset, + private List sortAndFilterMatchingRange(Iterable source, SpelCriteria criteria, Comparator sort, long offset, int rows) { List tmp = IterableConverter.toList(source); @@ -76,7 +76,7 @@ class SpelQueryEngine extends QueryEngine List filterMatchingRange(Iterable source, SpelCriteria criteria, int offset, int rows) { + private static List filterMatchingRange(Iterable source, SpelCriteria criteria, long offset, int rows) { List result = new ArrayList(); diff --git a/src/main/java/org/springframework/data/keyvalue/core/SpelSortAccessor.java b/src/main/java/org/springframework/data/keyvalue/core/SpelSortAccessor.java index 6d5c9d0..d8f5f9b 100644 --- a/src/main/java/org/springframework/data/keyvalue/core/SpelSortAccessor.java +++ b/src/main/java/org/springframework/data/keyvalue/core/SpelSortAccessor.java @@ -17,6 +17,7 @@ package org.springframework.data.keyvalue.core; import java.util.Comparator; +import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort.Direction; import org.springframework.data.domain.Sort.NullHandling; import org.springframework.data.domain.Sort.Order; @@ -52,7 +53,7 @@ class SpelSortAccessor implements SortAccessor> { @Override public Comparator resolve(KeyValueQuery query) { - if (query == null || query.getSort() == null) { + if (query == null || query.getSort() == null || Sort.unsorted().equals(query.getSort())) { return null; } diff --git a/src/main/java/org/springframework/data/keyvalue/core/mapping/KeyValuePersistentProperty.java b/src/main/java/org/springframework/data/keyvalue/core/mapping/KeyValuePersistentProperty.java index 3fd4947..980dadb 100644 --- a/src/main/java/org/springframework/data/keyvalue/core/mapping/KeyValuePersistentProperty.java +++ b/src/main/java/org/springframework/data/keyvalue/core/mapping/KeyValuePersistentProperty.java @@ -22,6 +22,7 @@ import org.springframework.data.mapping.Association; import org.springframework.data.mapping.PersistentEntity; import org.springframework.data.mapping.PersistentProperty; import org.springframework.data.mapping.model.AnnotationBasedPersistentProperty; +import org.springframework.data.mapping.model.Property; import org.springframework.data.mapping.model.SimpleTypeHolder; /** @@ -31,9 +32,9 @@ import org.springframework.data.mapping.model.SimpleTypeHolder; */ public class KeyValuePersistentProperty extends AnnotationBasedPersistentProperty { - public KeyValuePersistentProperty(Field field, PropertyDescriptor propertyDescriptor, - PersistentEntity owner, SimpleTypeHolder simpleTypeHolder) { - super(field, propertyDescriptor, owner, simpleTypeHolder); + public KeyValuePersistentProperty(Property property, PersistentEntity owner, + SimpleTypeHolder simpleTypeHolder) { + super(property, owner, simpleTypeHolder); } /* diff --git a/src/main/java/org/springframework/data/keyvalue/core/mapping/context/KeyValueMappingContext.java b/src/main/java/org/springframework/data/keyvalue/core/mapping/context/KeyValueMappingContext.java index f544171..fdd8df3 100644 --- a/src/main/java/org/springframework/data/keyvalue/core/mapping/context/KeyValueMappingContext.java +++ b/src/main/java/org/springframework/data/keyvalue/core/mapping/context/KeyValueMappingContext.java @@ -24,6 +24,7 @@ import org.springframework.data.keyvalue.core.mapping.KeyValuePersistentEntity; import org.springframework.data.keyvalue.core.mapping.KeyValuePersistentProperty; import org.springframework.data.mapping.context.AbstractMappingContext; import org.springframework.data.mapping.context.MappingContext; +import org.springframework.data.mapping.model.Property; import org.springframework.data.mapping.model.SimpleTypeHolder; import org.springframework.data.util.TypeInformation; @@ -57,13 +58,8 @@ public class KeyValueMappingContext extends return new BasicKeyValuePersistentEntity(typeInformation, fallbackKeySpaceResolver); } - /* - * (non-Javadoc) - * @see org.springframework.data.mapping.context.AbstractMappingContext#createPersistentProperty(java.lang.reflect.Field, java.beans.PropertyDescriptor, org.springframework.data.mapping.model.MutablePersistentEntity, org.springframework.data.mapping.model.SimpleTypeHolder) - */ @Override - protected KeyValuePersistentProperty createPersistentProperty(Field field, PropertyDescriptor descriptor, - KeyValuePersistentEntity owner, SimpleTypeHolder simpleTypeHolder) { - return new KeyValuePersistentProperty(field, descriptor, owner, simpleTypeHolder); + protected KeyValuePersistentProperty createPersistentProperty(Property property, KeyValuePersistentEntity owner, SimpleTypeHolder simpleTypeHolder) { + return new KeyValuePersistentProperty(property, owner, simpleTypeHolder); } } diff --git a/src/main/java/org/springframework/data/keyvalue/core/query/KeyValueQuery.java b/src/main/java/org/springframework/data/keyvalue/core/query/KeyValueQuery.java index b032a62..994d9b5 100644 --- a/src/main/java/org/springframework/data/keyvalue/core/query/KeyValueQuery.java +++ b/src/main/java/org/springframework/data/keyvalue/core/query/KeyValueQuery.java @@ -24,7 +24,7 @@ import org.springframework.data.domain.Sort; public class KeyValueQuery { private Sort sort; - private int offset = -1; + private long offset = -1; private int rows = -1; private T criteria; @@ -74,7 +74,7 @@ public class KeyValueQuery { * * @return negative value if not set. */ - public int getOffset() { + public long getOffset() { return this.offset; } @@ -92,7 +92,7 @@ public class KeyValueQuery { * * @param offset use negative value for none. */ - public void setOffset(int offset) { + public void setOffset(long offset) { this.offset = offset; } @@ -139,7 +139,7 @@ public class KeyValueQuery { * @param offset * @return */ - public KeyValueQuery skip(int offset) { + public KeyValueQuery skip(long offset) { setOffset(offset); return this; } diff --git a/src/main/java/org/springframework/data/keyvalue/repository/config/KeyValueRepositoryConfigurationExtension.java b/src/main/java/org/springframework/data/keyvalue/repository/config/KeyValueRepositoryConfigurationExtension.java index 4700636..6d0b54f 100644 --- a/src/main/java/org/springframework/data/keyvalue/repository/config/KeyValueRepositoryConfigurationExtension.java +++ b/src/main/java/org/springframework/data/keyvalue/repository/config/KeyValueRepositoryConfigurationExtension.java @@ -18,6 +18,7 @@ package org.springframework.data.keyvalue.repository.config; import java.util.Collection; import java.util.Collections; import java.util.Map; +import java.util.Optional; import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; @@ -153,16 +154,16 @@ public abstract class KeyValueRepositoryConfigurationExtension extends Repositor registerIfNotAlreadyRegistered(mappingContextDefinition, registry, MAPPING_CONTEXT_BEAN_NAME, configurationSource); - String keyValueTemplateName = configurationSource.getAttribute(KEY_VALUE_TEMPLATE_BEAN_REF_ATTRIBUTE); + Optional keyValueTemplateName = configurationSource.getAttribute(KEY_VALUE_TEMPLATE_BEAN_REF_ATTRIBUTE); // No custom template reference configured and no matching bean definition found - if (getDefaultKeyValueTemplateRef().equals(keyValueTemplateName) - && !registry.containsBeanDefinition(keyValueTemplateName)) { + if (keyValueTemplateName.isPresent() && getDefaultKeyValueTemplateRef().equals(keyValueTemplateName.get()) + && !registry.containsBeanDefinition(keyValueTemplateName.get())) { AbstractBeanDefinition beanDefinition = getDefaultKeyValueTemplateBeanDefinition(configurationSource); if (beanDefinition != null) { - registerIfNotAlreadyRegistered(beanDefinition, registry, keyValueTemplateName, configurationSource.getSource()); + registerIfNotAlreadyRegistered(beanDefinition, registry, keyValueTemplateName.get(), configurationSource.getSource()); } } } diff --git a/src/main/java/org/springframework/data/keyvalue/repository/query/KeyValuePartTreeQuery.java b/src/main/java/org/springframework/data/keyvalue/repository/query/KeyValuePartTreeQuery.java index a28cdd6..0e0d270 100644 --- a/src/main/java/org/springframework/data/keyvalue/repository/query/KeyValuePartTreeQuery.java +++ b/src/main/java/org/springframework/data/keyvalue/repository/query/KeyValuePartTreeQuery.java @@ -16,8 +16,10 @@ package org.springframework.data.keyvalue.repository.query; import java.lang.reflect.Constructor; +import java.util.Optional; import org.springframework.beans.BeanUtils; +import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; @@ -85,7 +87,7 @@ public class KeyValuePartTreeQuery implements RepositoryQuery { ParameterAccessor accessor = new ParametersParameterAccessor(getQueryMethod().getParameters(), parameters); KeyValueQuery query = prepareQuery(parameters); - ResultProcessor processor = queryMethod.getResultProcessor().withDynamicProjection(accessor); + ResultProcessor processor = queryMethod.getResultProcessor().withDynamicProjection(Optional.ofNullable(accessor)); return processor.processResult(doExecute(parameters, query)); } @@ -149,15 +151,15 @@ public class KeyValuePartTreeQuery implements RepositoryQuery { Pageable pageable = accessor.getPageable(); Sort sort = accessor.getSort(); - query.setOffset(pageable == null ? -1 : pageable.getOffset()); + query.setOffset(pageable != null && Pageable.NONE.equals(pageable) ? -1L : pageable.getOffset()); - if (pageable != null) { + if (pageable != null && !Pageable.NONE.equals(pageable)) { query.setRows(pageable.getPageSize()); } else if (instance.getRows() >= 0) { query.setRows(instance.getRows()); } - query.setSort(sort == null ? instance.getSort() : sort); + query.setSort(sort == null || Sort.unsorted().equals(sort) ? instance.getSort() : sort); return query; } diff --git a/src/main/java/org/springframework/data/keyvalue/repository/support/KeyValueRepositoryFactory.java b/src/main/java/org/springframework/data/keyvalue/repository/support/KeyValueRepositoryFactory.java index c76f007..196cc01 100644 --- a/src/main/java/org/springframework/data/keyvalue/repository/support/KeyValueRepositoryFactory.java +++ b/src/main/java/org/springframework/data/keyvalue/repository/support/KeyValueRepositoryFactory.java @@ -15,11 +15,12 @@ */ package org.springframework.data.keyvalue.repository.support; -import static org.springframework.data.querydsl.QueryDslUtils.*; +import static org.springframework.data.querydsl.QuerydslUtils.*; import java.io.Serializable; import java.lang.reflect.Constructor; import java.lang.reflect.Method; +import java.util.Optional; import org.springframework.beans.BeanUtils; import org.springframework.data.keyvalue.core.KeyValueOperations; @@ -28,7 +29,7 @@ import org.springframework.data.keyvalue.repository.query.SpelQueryCreator; import org.springframework.data.mapping.PersistentEntity; import org.springframework.data.mapping.context.MappingContext; import org.springframework.data.projection.ProjectionFactory; -import org.springframework.data.querydsl.QueryDslPredicateExecutor; +import org.springframework.data.querydsl.QuerydslPredicateExecutor; import org.springframework.data.repository.core.EntityInformation; import org.springframework.data.repository.core.NamedQueries; import org.springframework.data.repository.core.RepositoryInformation; @@ -112,7 +113,7 @@ public class KeyValueRepositoryFactory extends RepositoryFactorySupport { @SuppressWarnings("unchecked") public EntityInformation getEntityInformation(Class domainClass) { - PersistentEntity entity = (PersistentEntity) context.getPersistentEntity(domainClass); + PersistentEntity entity = (PersistentEntity) context.getPersistentEntity(domainClass).get(); PersistentEntityInformation entityInformation = new PersistentEntityInformation(entity); return entityInformation; @@ -146,7 +147,7 @@ public class KeyValueRepositoryFactory extends RepositoryFactorySupport { * @return */ private static boolean isQueryDslRepository(Class repositoryInterface) { - return QUERY_DSL_PRESENT && QueryDslPredicateExecutor.class.isAssignableFrom(repositoryInterface); + return QUERY_DSL_PRESENT && QuerydslPredicateExecutor.class.isAssignableFrom(repositoryInterface); } /* @@ -154,9 +155,9 @@ public class KeyValueRepositoryFactory extends RepositoryFactorySupport { * @see org.springframework.data.repository.core.support.RepositoryFactorySupport#getQueryLookupStrategy(org.springframework.data.repository.query.QueryLookupStrategy.Key, org.springframework.data.repository.query.EvaluationContextProvider) */ @Override - protected QueryLookupStrategy getQueryLookupStrategy(Key key, EvaluationContextProvider evaluationContextProvider) { - return new KeyValueQueryLookupStrategy(key, evaluationContextProvider, this.keyValueOperations, this.queryCreator, - this.repositoryQueryType); + protected Optional getQueryLookupStrategy(Key key, EvaluationContextProvider evaluationContextProvider) { + return Optional.of(new KeyValueQueryLookupStrategy(key, evaluationContextProvider, this.keyValueOperations, this.queryCreator, + this.repositoryQueryType)); } /** diff --git a/src/main/java/org/springframework/data/keyvalue/repository/support/QuerydslKeyValueRepository.java b/src/main/java/org/springframework/data/keyvalue/repository/support/QuerydslKeyValueRepository.java index 5019664..0fd9df6 100644 --- a/src/main/java/org/springframework/data/keyvalue/repository/support/QuerydslKeyValueRepository.java +++ b/src/main/java/org/springframework/data/keyvalue/repository/support/QuerydslKeyValueRepository.java @@ -26,7 +26,7 @@ import org.springframework.data.domain.Sort; import org.springframework.data.keyvalue.core.KeyValueOperations; import org.springframework.data.keyvalue.repository.KeyValueRepository; import org.springframework.data.querydsl.EntityPathResolver; -import org.springframework.data.querydsl.QueryDslPredicateExecutor; +import org.springframework.data.querydsl.QuerydslPredicateExecutor; import org.springframework.data.querydsl.SimpleEntityPathResolver; import org.springframework.data.repository.core.EntityInformation; import org.springframework.util.Assert; @@ -49,7 +49,7 @@ import com.querydsl.core.types.dsl.PathBuilder; * @param the identifier type of the domain type */ public class QuerydslKeyValueRepository extends SimpleKeyValueRepository - implements QueryDslPredicateExecutor { + implements QuerydslPredicateExecutor { private static final EntityPathResolver DEFAULT_ENTITY_PATH_RESOLVER = SimpleEntityPathResolver.INSTANCE; diff --git a/src/main/java/org/springframework/data/keyvalue/repository/support/SimpleKeyValueRepository.java b/src/main/java/org/springframework/data/keyvalue/repository/support/SimpleKeyValueRepository.java index 91ff53a..3919a32 100644 --- a/src/main/java/org/springframework/data/keyvalue/repository/support/SimpleKeyValueRepository.java +++ b/src/main/java/org/springframework/data/keyvalue/repository/support/SimpleKeyValueRepository.java @@ -18,6 +18,7 @@ package org.springframework.data.keyvalue.repository.support; import java.io.Serializable; import java.util.ArrayList; import java.util.List; +import java.util.Optional; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; @@ -74,7 +75,7 @@ public class SimpleKeyValueRepository implements Key if (pageable == null) { List result = findAll(); - return new PageImpl(result, null, result.size()); + return new PageImpl(result, Pageable.NONE, result.size()); } Iterable content = operations.findInRange(pageable.getOffset(), pageable.getPageSize(), pageable.getSort(), @@ -96,7 +97,7 @@ public class SimpleKeyValueRepository implements Key if (entityInformation.isNew(entity)) { operations.insert(entity); } else { - operations.update(entityInformation.getId(entity), entity); + operations.update(entityInformation.getId(entity).get(), entity); } return entity; } @@ -120,7 +121,7 @@ public class SimpleKeyValueRepository implements Key * @see org.springframework.data.repository.CrudRepository#findOne(java.io.Serializable) */ @Override - public T findOne(ID id) { + public Optional findOne(ID id) { return operations.findById(id, entityInformation.getJavaType()); } @@ -153,10 +154,10 @@ public class SimpleKeyValueRepository implements Key for (ID id : ids) { - T candidate = findOne(id); + Optional candidate = findOne(id); - if (candidate != null) { - result.add(candidate); + if (candidate.isPresent()) { + result.add(candidate.get()); } } @@ -187,7 +188,7 @@ public class SimpleKeyValueRepository implements Key */ @Override public void delete(T entity) { - delete(entityInformation.getId(entity)); + delete(entityInformation.getId(entity).orElseThrow(() -> new IllegalArgumentException("Cannot delete entity with 'null' id."))); } /* diff --git a/src/test/java/org/springframework/data/keyvalue/core/KeyValueTemplateTests.java b/src/test/java/org/springframework/data/keyvalue/core/KeyValueTemplateTests.java index 57827f9..f2b7c27 100644 --- a/src/test/java/org/springframework/data/keyvalue/core/KeyValueTemplateTests.java +++ b/src/test/java/org/springframework/data/keyvalue/core/KeyValueTemplateTests.java @@ -29,6 +29,7 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.util.List; +import java.util.Optional; import org.junit.After; import org.junit.Before; @@ -113,28 +114,28 @@ public class KeyValueTemplateTests { operations.insert(source); - assertThat(operations.findById("one", ClassWithStringId.class), is(source)); + assertThat(operations.findById("one", ClassWithStringId.class), is(Optional.of(source))); } @Test // DATACMNS-525 public void findByIdShouldReturnObjectWithMatchingIdAndType() { operations.insert("1", FOO_ONE); - assertThat(operations.findById("1", Foo.class), is(FOO_ONE)); + assertThat(operations.findById("1", Foo.class), is(Optional.of(FOO_ONE))); } @Test // DATACMNS-525 - public void findByIdSouldReturnNullIfNoMatchingIdFound() { + public void findByIdSouldReturnOptionalEmptyIfNoMatchingIdFound() { operations.insert("1", FOO_ONE); - assertThat(operations.findById("2", Foo.class), nullValue()); + assertThat(operations.findById("2", Foo.class), is(Optional.empty())); } @Test // DATACMNS-525 - public void findByIdShouldReturnNullIfNoMatchingTypeFound() { + public void findByIdShouldReturnOptionalEmptyIfNoMatchingTypeFound() { operations.insert("1", FOO_ONE); - assertThat(operations.findById("1", Bar.class), nullValue()); + assertThat(operations.findById("1", Bar.class), is(Optional.empty())); } @Test // DATACMNS-525 @@ -163,7 +164,7 @@ public class KeyValueTemplateTests { operations.insert("1", FOO_ONE); operations.update("1", FOO_TWO); - assertThat(operations.findById("1", Foo.class), is(FOO_TWO)); + assertThat(operations.findById("1", Foo.class), is(Optional.of(FOO_TWO))); } @Test // DATACMNS-525 @@ -172,7 +173,7 @@ public class KeyValueTemplateTests { operations.insert("1", FOO_ONE); operations.update("1", BAR_ONE); - assertThat(operations.findById("1", Foo.class), is(FOO_ONE)); + assertThat(operations.findById("1", Foo.class), is(Optional.of(FOO_ONE))); } @Test // DATACMNS-525 @@ -180,7 +181,7 @@ public class KeyValueTemplateTests { operations.insert("1", FOO_ONE); operations.delete("1", Foo.class); - assertThat(operations.findById("1", Foo.class), nullValue()); + assertThat(operations.findById("1", Foo.class), is(Optional.empty())); } @Test // DATACMNS-525 diff --git a/src/test/java/org/springframework/data/keyvalue/core/KeyValueTemplateUnitTests.java b/src/test/java/org/springframework/data/keyvalue/core/KeyValueTemplateUnitTests.java index bc7939a..d762178 100644 --- a/src/test/java/org/springframework/data/keyvalue/core/KeyValueTemplateUnitTests.java +++ b/src/test/java/org/springframework/data/keyvalue/core/KeyValueTemplateUnitTests.java @@ -26,6 +26,7 @@ import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashSet; +import java.util.Optional; import org.junit.Before; import org.junit.Rule; @@ -167,8 +168,8 @@ public class KeyValueTemplateUnitTests { } @Test // DATACMNS-525 - public void findByIdShouldReturnNullWhenNoElementsPresent() { - assertNull(template.findById("1", Foo.class)); + public void findByIdShouldReturnOptionalEmptyWhenNoElementsPresent() { + assertThat(template.findById("1", Foo.class), is(Optional.empty())); } @Test // DATACMNS-525 @@ -214,7 +215,7 @@ public class KeyValueTemplateUnitTests { template.findInRange(1, 5, Foo.class); verify(adapterMock, times(1)).find(captor.capture(), eq(Foo.class.getName()), eq(Foo.class)); - assertThat(captor.getValue().getOffset(), is(1)); + assertThat(captor.getValue().getOffset(), is(1L)); assertThat(captor.getValue().getRows(), is(5)); assertThat(captor.getValue().getCritieria(), nullValue()); } @@ -325,7 +326,7 @@ public class KeyValueTemplateUnitTests { public void insertSouldRespectTypeAliasAndFilterNonMatching() { template.insert("1", ALIASED); - assertThat(template.findById("1", SUBCLASS_OF_ALIASED.getClass()), nullValue()); + assertThat(template.findById("1", SUBCLASS_OF_ALIASED.getClass()), is(Optional.empty())); } @Test(expected = IllegalArgumentException.class) // DATACMNS-525 diff --git a/src/test/java/org/springframework/data/keyvalue/repository/SimpleKeyValueRepositoryUnitTests.java b/src/test/java/org/springframework/data/keyvalue/repository/SimpleKeyValueRepositoryUnitTests.java index 6e5ea4f..7cb9eb7 100644 --- a/src/test/java/org/springframework/data/keyvalue/repository/SimpleKeyValueRepositoryUnitTests.java +++ b/src/test/java/org/springframework/data/keyvalue/repository/SimpleKeyValueRepositoryUnitTests.java @@ -18,7 +18,9 @@ package org.springframework.data.keyvalue.repository; import static org.mockito.Matchers.*; import static org.mockito.Mockito.*; +import java.io.Serializable; import java.util.Arrays; +import java.util.Optional; import org.junit.Before; import org.junit.Test; @@ -91,7 +93,9 @@ public class SimpleKeyValueRepositoryUnitTests { @Test // DATACMNS-525 public void deleteEntity() { - Foo one = repo.save(new Foo("one")); + Foo one = new Foo("one"); + one.id = "1"; + repo.save(one); repo.delete(one); verify(opsMock, times(1)).delete(eq(one.getId()), eq(Foo.class)); @@ -116,6 +120,7 @@ public class SimpleKeyValueRepositoryUnitTests { @Test // DATACMNS-525 public void findAllIds() { + when(opsMock.findById(any(Serializable.class), any(Class.class))).thenReturn(Optional.empty()); repo.findAll(Arrays.asList("one", "two", "three")); verify(opsMock, times(3)).findById(anyString(), eq(Foo.class)); @@ -126,7 +131,7 @@ public class SimpleKeyValueRepositoryUnitTests { repo.findAll(new PageRequest(10, 15)); - verify(opsMock, times(1)).findInRange(eq(150), eq(15), isNull(Sort.class), eq(Foo.class)); + verify(opsMock, times(1)).findInRange(eq(150L), eq(15), eq(Sort.unsorted()), eq(Foo.class)); } @Test // DATACMNS-525 @@ -135,7 +140,7 @@ public class SimpleKeyValueRepositoryUnitTests { Sort sort = new Sort("for", "bar"); repo.findAll(new PageRequest(10, 15, sort)); - verify(opsMock, times(1)).findInRange(eq(150), eq(15), eq(sort), eq(Foo.class)); + verify(opsMock, times(1)).findInRange(eq(150L), eq(15), eq(sort), eq(Foo.class)); } @Test // DATACMNS-525 diff --git a/src/test/java/org/springframework/data/keyvalue/repository/query/KeyValuePartTreeQueryUnitTests.java b/src/test/java/org/springframework/data/keyvalue/repository/query/KeyValuePartTreeQueryUnitTests.java index 4c6c258..e5cb3ea 100644 --- a/src/test/java/org/springframework/data/keyvalue/repository/query/KeyValuePartTreeQueryUnitTests.java +++ b/src/test/java/org/springframework/data/keyvalue/repository/query/KeyValuePartTreeQueryUnitTests.java @@ -88,7 +88,7 @@ public class KeyValuePartTreeQueryUnitTests { KeyValueQuery query = partTreeQuery.prepareQuery(new Object[] { new PageRequest(2, 3) }); - assertThat(query.getOffset(), is(6)); + assertThat(query.getOffset(), is(6L)); assertThat(query.getRows(), is(3)); } diff --git a/src/test/java/org/springframework/data/map/QuerydslKeyValueRepositoryUnitTests.java b/src/test/java/org/springframework/data/map/QuerydslKeyValueRepositoryUnitTests.java index 5699bdf..2133024 100644 --- a/src/test/java/org/springframework/data/map/QuerydslKeyValueRepositoryUnitTests.java +++ b/src/test/java/org/springframework/data/map/QuerydslKeyValueRepositoryUnitTests.java @@ -31,7 +31,7 @@ import org.springframework.data.keyvalue.repository.support.KeyValueRepositoryFa import org.springframework.data.keyvalue.repository.support.QuerydslKeyValueRepository; import org.springframework.data.map.QuerydslKeyValueRepositoryUnitTests.QPersonRepository; import org.springframework.data.querydsl.QSort; -import org.springframework.data.querydsl.QueryDslPredicateExecutor; +import org.springframework.data.querydsl.QuerydslPredicateExecutor; import com.google.common.collect.Lists; @@ -164,5 +164,5 @@ public class QuerydslKeyValueRepositoryUnitTests extends AbstractRepositoryUnitT } static interface QPersonRepository extends org.springframework.data.map.AbstractRepositoryUnitTests.PersonRepository, - QueryDslPredicateExecutor {} + QuerydslPredicateExecutor {} }