diff --git a/src/main/asciidoc/key-value-repositories.adoc b/src/main/asciidoc/key-value-repositories.adoc index aa2fa6e..145d5a9 100644 --- a/src/main/asciidoc/key-value-repositories.adoc +++ b/src/main/asciidoc/key-value-repositories.adoc @@ -20,7 +20,7 @@ interface KeyValueOperations { void delete(Class type); <3> - T findById(Serializable id, Class type); <4> + T findById(Object id, Class type); <4> Iterable findAllOf(Class type); <5> diff --git a/src/main/java/org/springframework/data/keyvalue/core/AbstractKeyValueAdapter.java b/src/main/java/org/springframework/data/keyvalue/core/AbstractKeyValueAdapter.java index 08a98e9..907d4a0 100644 --- a/src/main/java/org/springframework/data/keyvalue/core/AbstractKeyValueAdapter.java +++ b/src/main/java/org/springframework/data/keyvalue/core/AbstractKeyValueAdapter.java @@ -24,6 +24,7 @@ import org.springframework.data.keyvalue.core.query.KeyValueQuery; * {@literal count} execution to. * * @author Christoph Strobl + * @author Mark Paluch */ public abstract class AbstractKeyValueAdapter implements KeyValueAdapter { @@ -43,7 +44,7 @@ public abstract class AbstractKeyValueAdapter implements KeyValueAdapter { */ protected AbstractKeyValueAdapter(QueryEngine engine) { - this.engine = engine != null ? engine : new SpelQueryEngine<>(); + this.engine = engine != null ? engine : new SpelQueryEngine(); this.engine.registerAdapter(this); } @@ -62,7 +63,7 @@ public abstract class AbstractKeyValueAdapter implements KeyValueAdapter { */ @Override public T get(Object id, String keyspace, Class type) { - return (T) get(id, keyspace); + return type.cast(get(id, keyspace)); } /* @@ -71,7 +72,7 @@ public abstract class AbstractKeyValueAdapter implements KeyValueAdapter { */ @Override public T delete(Object id, String keyspace, Class type) { - return (T) delete(id, keyspace); + return type.cast(delete(id, keyspace)); } /* 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 ae68d76..98b7675 100644 --- a/src/main/java/org/springframework/data/keyvalue/core/KeyValueOperations.java +++ b/src/main/java/org/springframework/data/keyvalue/core/KeyValueOperations.java @@ -25,14 +25,14 @@ import org.springframework.data.mapping.context.MappingContext; /** * Interface that specifies a basic set of key/value operations. Implemented by {@link KeyValueTemplate}. - * + * * @author Christoph Strobl */ public interface KeyValueOperations extends DisposableBean { /** * Add given object. Object needs to have id property to which a generated value will be assigned. - * + * * @param objectToInsert * @return */ @@ -40,7 +40,7 @@ public interface KeyValueOperations extends DisposableBean { /** * Add object with given id. - * + * * @param id must not be {@literal null}. * @param objectToInsert must not be {@literal null}. */ @@ -49,7 +49,7 @@ public interface KeyValueOperations extends DisposableBean { /** * Get all elements of given type. Respects {@link KeySpace} if present and therefore returns all elements that can be * assigned to requested type. - * + * * @param type must not be {@literal null}. * @return empty iterable if no elements found. */ @@ -58,7 +58,7 @@ public interface KeyValueOperations extends DisposableBean { /** * Get all elements ordered by sort. Respects {@link KeySpace} if present and therefore returns all elements that can * be assigned to requested type. - * + * * @param sort must not be {@literal null}. * @param type must not be {@literal null}. * @return @@ -68,16 +68,16 @@ public interface KeyValueOperations extends DisposableBean { /** * Get element of given type with given id. Respects {@link KeySpace} if present and therefore returns all elements * that can be assigned to requested type. - * + * * @param id must not be {@literal null}. * @param type must not be {@literal null}. - * @return null if not found. + * @return {@link Optional#empty()} if not found. */ Optional findById(Object id, Class type); /** * Execute operation against underlying store. - * + * * @param action must not be {@literal null}. * @return */ @@ -86,7 +86,7 @@ public interface KeyValueOperations extends DisposableBean { /** * Get all elements matching the given query.
* Respects {@link KeySpace} if present and therefore returns all elements that can be assigned to requested type.. - * + * * @param query must not be {@literal null}. * @param type must not be {@literal null}. * @return empty iterable if no match found. @@ -96,7 +96,7 @@ public interface KeyValueOperations extends DisposableBean { /** * Get all elements in given range. Respects {@link KeySpace} if present and therefore returns all elements that can * be assigned to requested type. - * + * * @param offset * @param rows * @param type must not be {@literal null}. @@ -107,7 +107,7 @@ public interface KeyValueOperations extends DisposableBean { /** * Get all elements in given range ordered by sort. Respects {@link KeySpace} if present and therefore returns all * elements that can be assigned to requested type. - * + * * @param offset * @param rows * @param sort @@ -130,7 +130,7 @@ public interface KeyValueOperations extends DisposableBean { /** * Remove all elements of type. Respects {@link KeySpace} if present and therefore removes all elements that can be * assigned to requested type. - * + * * @param type must not be {@literal null}. */ void delete(Class type); @@ -143,7 +143,7 @@ public interface KeyValueOperations extends DisposableBean { /** * Delete item of type with given id. - * + * * @param id must not be {@literal null}. * @param type must not be {@literal null}. * @return the deleted item or {@literal null} if no match found. @@ -153,7 +153,7 @@ public interface KeyValueOperations extends DisposableBean { /** * Total number of elements with given type available. Respects {@link KeySpace} if present and therefore counts all * elements that can be assigned to requested type. - * + * * @param type must not be {@literal null}. * @return */ @@ -162,7 +162,7 @@ public interface KeyValueOperations extends DisposableBean { /** * Total number of elements matching given query. Respects {@link KeySpace} if present and therefore counts all * elements that can be assigned to requested type. - * + * * @param query * @param type * @return 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 85b1193..2de2620 100644 --- a/src/main/java/org/springframework/data/keyvalue/core/KeyValueTemplate.java +++ b/src/main/java/org/springframework/data/keyvalue/core/KeyValueTemplate.java @@ -67,7 +67,7 @@ public class KeyValueTemplate implements KeyValueOperations, ApplicationEventPub * @param adapter must not be {@literal null}. */ public KeyValueTemplate(KeyValueAdapter adapter) { - this(adapter, new KeyValueMappingContext()); + this(adapter, new KeyValueMappingContext<>()); } /** @@ -149,28 +149,24 @@ public class KeyValueTemplate implements KeyValueOperations, ApplicationEventPub * @see org.springframework.data.keyvalue.core.KeyValueOperations#insert(java.lang.Object, java.lang.Object) */ @Override - public void insert(final Object id, final Object objectToInsert) { + public void insert(Object id, Object objectToInsert) { Assert.notNull(id, "Id for object to be inserted must not be null!"); Assert.notNull(objectToInsert, "Object to be inserted must not be null!"); - final String keyspace = resolveKeySpace(objectToInsert.getClass()); + String keyspace = resolveKeySpace(objectToInsert.getClass()); potentiallyPublishEvent(KeyValueEvent.beforeInsert(id, keyspace, objectToInsert.getClass(), objectToInsert)); - execute(new KeyValueCallback() { + execute((KeyValueCallback) adapter -> { - @Override - 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)); - } - - adapter.put(id, objectToInsert, keyspace); - return null; + if (adapter.contains(id, keyspace)) { + throw new DuplicateKeyException( + String.format("Cannot insert existing object with id %s!. Please use update.", id)); } + + adapter.put(id, objectToInsert, keyspace); + return null; }); potentiallyPublishEvent(KeyValueEvent.afterInsert(id, keyspace, objectToInsert.getClass(), objectToInsert)); @@ -199,22 +195,16 @@ public class KeyValueTemplate implements KeyValueOperations, ApplicationEventPub * @see org.springframework.data.keyvalue.core.KeyValueOperations#update(java.lang.Object, java.lang.Object) */ @Override - public void update(final Object id, final Object objectToUpdate) { + public void update(Object id, Object objectToUpdate) { Assert.notNull(id, "Id for object to be inserted must not be null!"); Assert.notNull(objectToUpdate, "Object to be updated must not be null!"); - final String keyspace = resolveKeySpace(objectToUpdate.getClass()); + String keyspace = resolveKeySpace(objectToUpdate.getClass()); potentiallyPublishEvent(KeyValueEvent.beforeUpdate(id, keyspace, objectToUpdate.getClass(), objectToUpdate)); - Object existing = execute(new KeyValueCallback() { - - @Override - public Object doInKeyValue(KeyValueAdapter adapter) { - return adapter.put(id, objectToUpdate, keyspace); - } - }); + Object existing = execute(adapter -> adapter.put(id, objectToUpdate, keyspace)); potentiallyPublishEvent( KeyValueEvent.afterUpdate(id, keyspace, objectToUpdate.getClass(), objectToUpdate, existing)); @@ -225,7 +215,7 @@ public class KeyValueTemplate implements KeyValueOperations, ApplicationEventPub * @see org.springframework.data.keyvalue.core.KeyValueOperations#findAllOf(java.lang.Class) */ @Override - public Iterable findAll(final Class type) { + public Iterable findAll(Class type) { Assert.notNull(type, "Type to fetch must not be null!"); @@ -258,29 +248,24 @@ public class KeyValueTemplate implements KeyValueOperations, ApplicationEventPub * @see org.springframework.data.keyvalue.core.KeyValueOperations#findById(java.lang.Object, java.lang.Class) */ @Override - public Optional findById(final Object id, final Class type) { + public Optional findById(Object id, 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!"); - final String keyspace = resolveKeySpace(type); + String keyspace = resolveKeySpace(type); potentiallyPublishEvent(KeyValueEvent.beforeGet(id, keyspace, type)); - T result = execute(new KeyValueCallback() { + T result = execute(adapter -> { - @SuppressWarnings("unchecked") - @Override - public T doInKeyValue(KeyValueAdapter adapter) { + Object value = adapter.get(id, keyspace, type); - Object result = adapter.get(id, keyspace, type); - - if (result == null || typeCheck(type, result)) { - return (T) result; - } - - return null; + if (value == null || typeCheck(type, value)) { + return type.cast(value); } + + return null; }); potentiallyPublishEvent(KeyValueEvent.afterGet(id, keyspace, type, result)); @@ -293,22 +278,18 @@ public class KeyValueTemplate implements KeyValueOperations, ApplicationEventPub * @see org.springframework.data.keyvalue.core.KeyValueOperations#delete(java.lang.Class) */ @Override - public void delete(final Class type) { + public void delete(Class type) { Assert.notNull(type, "Type to delete must not be null!"); - final String keyspace = resolveKeySpace(type); + String keyspace = resolveKeySpace(type); potentiallyPublishEvent(KeyValueEvent.beforeDropKeySpace(keyspace, type)); - execute(new KeyValueCallback() { + execute((KeyValueCallback) adapter -> { - @Override - public Void doInKeyValue(KeyValueAdapter adapter) { - - adapter.deleteAllOf(keyspace); - return null; - } + adapter.deleteAllOf(keyspace); + return null; }); potentiallyPublishEvent(KeyValueEvent.afterDropKeySpace(keyspace, type)); @@ -333,22 +314,16 @@ public class KeyValueTemplate implements KeyValueOperations, ApplicationEventPub * @see org.springframework.data.keyvalue.core.KeyValueOperations#delete(java.lang.Object, java.lang.Class) */ @Override - public T delete(final Object id, final Class type) { + public T delete(Object id, Class type) { Assert.notNull(id, "Id for object to be deleted must not be null!"); Assert.notNull(type, "Type to delete must not be null!"); - final String keyspace = resolveKeySpace(type); + String keyspace = resolveKeySpace(type); potentiallyPublishEvent(KeyValueEvent.beforeDelete(id, keyspace, type)); - T result = execute(new KeyValueCallback() { - - @Override - public T doInKeyValue(KeyValueAdapter adapter) { - return (T) adapter.delete(id, keyspace, type); - } - }); + T result = execute(adapter -> (T) adapter.delete(id, keyspace, type)); potentiallyPublishEvent(KeyValueEvent.afterDelete(id, keyspace, type, result)); @@ -387,29 +362,24 @@ public class KeyValueTemplate implements KeyValueOperations, ApplicationEventPub * @see org.springframework.data.keyvalue.core.KeyValueOperations#find(org.springframework.data.keyvalue.core.query.KeyValueQuery, java.lang.Class) */ @Override - public Iterable find(final KeyValueQuery query, final Class type) { + public Iterable find(KeyValueQuery query, Class type) { - return execute(new KeyValueCallback>() { + return execute((KeyValueCallback>) adapter -> { - @SuppressWarnings("unchecked") - @Override - public Iterable doInKeyValue(KeyValueAdapter adapter) { - - Iterable result = adapter.find(query, resolveKeySpace(type), type); - if (result == null) { - return Collections.emptySet(); - } - - List filtered = new ArrayList<>(); - - for (Object candidate : result) { - if (typeCheck(type, candidate)) { - filtered.add((T) candidate); - } - } - - return filtered; + Iterable result = adapter.find(query, resolveKeySpace(type), type); + if (result == null) { + return Collections.emptySet(); } + + List filtered = new ArrayList<>(); + + for (Object candidate : result) { + if (typeCheck(type, candidate)) { + filtered.add(type.cast(candidate)); + } + } + + return filtered; }); } @@ -448,15 +418,9 @@ public class KeyValueTemplate implements KeyValueOperations, ApplicationEventPub * @see org.springframework.data.keyvalue.core.KeyValueOperations#count(org.springframework.data.keyvalue.core.query.KeyValueQuery, java.lang.Class) */ @Override - public long count(final KeyValueQuery query, final Class type) { + public long count(KeyValueQuery query, Class type) { - return execute(new KeyValueCallback() { - - @Override - public Long doInKeyValue(KeyValueAdapter adapter) { - return adapter.count(query, resolveKeySpace(type)); - } - }); + return execute(adapter -> adapter.count(query, resolveKeySpace(type))); } /* @@ -500,6 +464,6 @@ public class KeyValueTemplate implements KeyValueOperations, ApplicationEventPub } private static boolean typeCheck(Class requiredType, Object candidate) { - return candidate == null ? true : ClassUtils.isAssignable(requiredType, candidate.getClass()); + return candidate == null || ClassUtils.isAssignable(requiredType, candidate.getClass()); } } 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 e049764..58d2d2d 100644 --- a/src/main/java/org/springframework/data/keyvalue/core/SpelQueryEngine.java +++ b/src/main/java/org/springframework/data/keyvalue/core/SpelQueryEngine.java @@ -17,7 +17,6 @@ package org.springframework.data.keyvalue.core; import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; import java.util.Comparator; import java.util.List; @@ -32,9 +31,10 @@ import org.springframework.expression.spel.standard.SpelExpressionParser; * * @author Christoph Strobl * @author Oliver Gierke + * @author Mark Paluch * @param */ -class SpelQueryEngine extends QueryEngine> { +class SpelQueryEngine extends QueryEngine> { private static final SpelExpressionParser PARSER = new SpelExpressionParser(); @@ -63,13 +63,13 @@ class SpelQueryEngine extends QueryEngine sortAndFilterMatchingRange(Iterable source, SpelCriteria criteria, Comparator sort, long offset, int rows) { List tmp = IterableConverter.toList(source); if (sort != null) { - Collections.sort(tmp, sort); + tmp.sort(sort); } return filterMatchingRange(tmp, criteria, offset, rows); diff --git a/src/main/java/org/springframework/data/keyvalue/core/event/KeyValueEvent.java b/src/main/java/org/springframework/data/keyvalue/core/event/KeyValueEvent.java index 67a71d3..b044174 100644 --- a/src/main/java/org/springframework/data/keyvalue/core/event/KeyValueEvent.java +++ b/src/main/java/org/springframework/data/keyvalue/core/event/KeyValueEvent.java @@ -54,126 +54,126 @@ public class KeyValueEvent extends ApplicationEvent { * Create new {@link BeforeGetEvent}. * * @param id - * @param keySpace + * @param keyspace * @param type * @return */ - public static BeforeGetEvent beforeGet(Object id, String keySpace, Class type) { - return new BeforeGetEvent<>(id, keySpace, type); + public static BeforeGetEvent beforeGet(Object id, String keyspace, Class type) { + return new BeforeGetEvent<>(id, keyspace, type); } /** * Create new {@link AfterGetEvent}. * * @param id - * @param keySpace + * @param keyspace * @param type * @param value * @return */ - public static AfterGetEvent afterGet(Object id, String keySpace, Class type, T value) { - return new AfterGetEvent<>(id, keySpace, type, value); + public static AfterGetEvent afterGet(Object id, String keyspace, Class type, T value) { + return new AfterGetEvent<>(id, keyspace, type, value); } /** * Create new {@link BeforeInsertEvent}. * * @param id - * @param keySpace + * @param keyspace * @param type * @param value * @return */ - public static BeforeInsertEvent beforeInsert(Object id, String keySpace, Class type, T value) { - return new BeforeInsertEvent<>(id, keySpace, type, value); + public static BeforeInsertEvent beforeInsert(Object id, String keyspace, Class type, T value) { + return new BeforeInsertEvent<>(id, keyspace, type, value); } /** * Create new {@link AfterInsertEvent}. * * @param id - * @param keySpace + * @param keyspace * @param type * @param value * @return */ - public static AfterInsertEvent afterInsert(Object id, String keySpace, Class type, T value) { - return new AfterInsertEvent<>(id, keySpace, type, value); + public static AfterInsertEvent afterInsert(Object id, String keyspace, Class type, T value) { + return new AfterInsertEvent<>(id, keyspace, type, value); } /** * Create new {@link BeforeUpdateEvent}. * * @param id - * @param keySpace + * @param keyspace * @param type * @param value * @return */ - public static BeforeUpdateEvent beforeUpdate(Object id, String keySpace, Class type, T value) { - return new BeforeUpdateEvent<>(id, keySpace, type, value); + public static BeforeUpdateEvent beforeUpdate(Object id, String keyspace, Class type, T value) { + return new BeforeUpdateEvent<>(id, keyspace, type, value); } /** * Create new {@link AfterUpdateEvent}. * * @param id - * @param keySpace + * @param keyspace * @param type * @param actualValue * @param previousValue * @return */ - public static AfterUpdateEvent afterUpdate(Object id, String keySpace, Class type, T actualValue, + public static AfterUpdateEvent afterUpdate(Object id, String keyspace, Class type, T actualValue, Object previousValue) { - return new AfterUpdateEvent<>(id, keySpace, type, actualValue, previousValue); + return new AfterUpdateEvent<>(id, keyspace, type, actualValue, previousValue); } /** * Create new {@link BeforeDropKeySpaceEvent}. * - * @param keySpace + * @param keyspace * @param type * @return */ - public static BeforeDropKeySpaceEvent beforeDropKeySpace(String keySpace, Class type) { - return new BeforeDropKeySpaceEvent<>(keySpace, type); + public static BeforeDropKeySpaceEvent beforeDropKeySpace(String keyspace, Class type) { + return new BeforeDropKeySpaceEvent<>(keyspace, type); } /** * Create new {@link AfterDropKeySpaceEvent}. * - * @param keySpace + * @param keyspace * @param type * @return */ - public static AfterDropKeySpaceEvent afterDropKeySpace(String keySpace, Class type) { - return new AfterDropKeySpaceEvent<>(keySpace, type); + public static AfterDropKeySpaceEvent afterDropKeySpace(String keyspace, Class type) { + return new AfterDropKeySpaceEvent<>(keyspace, type); } /** * Create new {@link BeforeDeleteEvent}. * * @param id - * @param keySpace + * @param keyspace * @param type * @return */ - public static BeforeDeleteEvent beforeDelete(Object id, String keySpace, Class type) { - return new BeforeDeleteEvent<>(id, keySpace, type); + public static BeforeDeleteEvent beforeDelete(Object id, String keyspace, Class type) { + return new BeforeDeleteEvent<>(id, keyspace, type); } /** * Create new {@link AfterDeleteEvent}. * * @param id - * @param keySpace + * @param keyspace * @param type * @param value * @return */ - public static AfterDeleteEvent afterDelete(Object id, String keySpace, Class type, T value) { - return new AfterDeleteEvent<>(id, keySpace, type, value); + public static AfterDeleteEvent afterDelete(Object id, String keyspace, Class type, T value) { + return new AfterDeleteEvent<>(id, keyspace, type, value); } /** @@ -186,9 +186,9 @@ public class KeyValueEvent extends ApplicationEvent { private Object key; private Class type; - protected KeyBasedEvent(Object key, String keySpace, Class type) { + protected KeyBasedEvent(Object key, String keyspace, Class type) { - super(type, keySpace); + super(type, keyspace); this.key = key; this.type = type; } @@ -225,8 +225,8 @@ public class KeyValueEvent extends ApplicationEvent { private final T payload; - public KeyBasedEventWithPayload(Object key, String keySpace, Class type, T payload) { - super(key, keySpace, type); + public KeyBasedEventWithPayload(Object key, String keyspace, Class type, T payload) { + super(key, keyspace, type); this.payload = payload; } @@ -249,8 +249,8 @@ public class KeyValueEvent extends ApplicationEvent { @SuppressWarnings("serial") public static class BeforeGetEvent extends KeyBasedEvent { - protected BeforeGetEvent(Object key, String keySpace, Class type) { - super(key, keySpace, type); + protected BeforeGetEvent(Object key, String keyspace, Class type) { + super(key, keyspace, type); } } @@ -279,8 +279,8 @@ public class KeyValueEvent extends ApplicationEvent { @SuppressWarnings("serial") public static class BeforeInsertEvent extends KeyBasedEventWithPayload { - public BeforeInsertEvent(Object key, String keySpace, Class type, T payload) { - super(key, keySpace, type, payload); + public BeforeInsertEvent(Object key, String keyspace, Class type, T payload) { + super(key, keyspace, type, payload); } } @@ -294,8 +294,8 @@ public class KeyValueEvent extends ApplicationEvent { @SuppressWarnings("serial") public static class AfterInsertEvent extends KeyBasedEventWithPayload { - public AfterInsertEvent(Object key, String keySpace, Class type, T payload) { - super(key, keySpace, type, payload); + public AfterInsertEvent(Object key, String keyspace, Class type, T payload) { + super(key, keyspace, type, payload); } } @@ -308,8 +308,8 @@ public class KeyValueEvent extends ApplicationEvent { @SuppressWarnings("serial") public static class BeforeUpdateEvent extends KeyBasedEventWithPayload { - public BeforeUpdateEvent(Object key, String keySpace, Class type, T payload) { - super(key, keySpace, type, payload); + public BeforeUpdateEvent(Object key, String keyspace, Class type, T payload) { + super(key, keyspace, type, payload); } } @@ -324,8 +324,8 @@ public class KeyValueEvent extends ApplicationEvent { private final Object existing; - public AfterUpdateEvent(Object key, String keySpace, Class type, T payload, Object existing) { - super(key, keySpace, type, payload); + public AfterUpdateEvent(Object key, String keyspace, Class type, T payload, Object existing) { + super(key, keyspace, type, payload); this.existing = existing; } @@ -357,8 +357,8 @@ public class KeyValueEvent extends ApplicationEvent { @SuppressWarnings("serial") public static class BeforeDeleteEvent extends KeyBasedEvent { - public BeforeDeleteEvent(Object key, String keySpace, Class type) { - super(key, keySpace, type); + public BeforeDeleteEvent(Object key, String keyspace, Class type) { + super(key, keyspace, type); } } @@ -371,13 +371,13 @@ public class KeyValueEvent extends ApplicationEvent { @SuppressWarnings("serial") public static class AfterDeleteEvent extends KeyBasedEventWithPayload { - public AfterDeleteEvent(Object key, String keySpace, Class type, T payload) { - super(key, keySpace, type, payload); + public AfterDeleteEvent(Object key, String keyspace, Class type, T payload) { + super(key, keyspace, type, payload); } } /** - * {@link KeyValueEvent} before removing all elements in a given {@literal keySpace}. + * {@link KeyValueEvent} before removing all elements in a given {@literal keyspace}. * * @author Christoph Strobl * @param @@ -385,8 +385,8 @@ public class KeyValueEvent extends ApplicationEvent { @SuppressWarnings("serial") public static class BeforeDropKeySpaceEvent extends KeyValueEvent { - public BeforeDropKeySpaceEvent(String keySpace, Class type) { - super(type, keySpace); + public BeforeDropKeySpaceEvent(String keyspace, Class type) { + super(type, keyspace); } @Override @@ -398,7 +398,7 @@ public class KeyValueEvent extends ApplicationEvent { } /** - * {@link KeyValueEvent} after removing all elements in a given {@literal keySpace}. + * {@link KeyValueEvent} after removing all elements in a given {@literal keyspace}. * * @author Christoph Strobl * @param @@ -406,8 +406,8 @@ public class KeyValueEvent extends ApplicationEvent { @SuppressWarnings("serial") public static class AfterDropKeySpaceEvent extends KeyValueEvent { - public AfterDropKeySpaceEvent(String keySpace, Class type) { - super(type, keySpace); + public AfterDropKeySpaceEvent(String keyspace, Class type) { + super(type, keyspace); } @Override 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 1ea8b6f..2df83f6 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 @@ -17,7 +17,6 @@ package org.springframework.data.keyvalue.repository.support; import static org.springframework.data.querydsl.QuerydslUtils.QUERY_DSL_PRESENT; -import java.io.Serializable; import java.lang.reflect.Constructor; import java.lang.reflect.Method; import java.util.Optional; @@ -126,7 +125,7 @@ public class KeyValueRepositoryFactory extends RepositoryFactorySupport { @Override protected Object getTargetRepository(RepositoryInformation repositoryInformation) { - EntityInformation entityInformation = getEntityInformation(repositoryInformation.getDomainType()); + EntityInformation entityInformation = getEntityInformation(repositoryInformation.getDomainType()); return super.getTargetRepositoryViaReflection(repositoryInformation, entityInformation, keyValueOperations); } diff --git a/src/main/java/org/springframework/data/keyvalue/repository/support/KeyValueRepositoryFactoryBean.java b/src/main/java/org/springframework/data/keyvalue/repository/support/KeyValueRepositoryFactoryBean.java index 4eb898d..705dc03 100644 --- a/src/main/java/org/springframework/data/keyvalue/repository/support/KeyValueRepositoryFactoryBean.java +++ b/src/main/java/org/springframework/data/keyvalue/repository/support/KeyValueRepositoryFactoryBean.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2016 the original author or authors. + * Copyright 2014-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,8 +15,6 @@ */ package org.springframework.data.keyvalue.repository.support; -import java.io.Serializable; - import org.springframework.data.keyvalue.core.KeyValueOperations; import org.springframework.data.keyvalue.repository.KeyValueRepository; import org.springframework.data.keyvalue.repository.config.QueryCreatorType; @@ -30,11 +28,12 @@ import org.springframework.util.Assert; /** * {@link org.springframework.beans.factory.FactoryBean} to create {@link KeyValueRepository}. - * + * * @author Christoph Strobl * @author Oliver Gierke + * @author Mark Paluch */ -public class KeyValueRepositoryFactoryBean, S, ID extends Serializable> +public class KeyValueRepositoryFactoryBean, S, ID> extends RepositoryFactoryBeanSupport { private KeyValueOperations operations; @@ -43,7 +42,7 @@ public class KeyValueRepositoryFactoryBean, S, ID ex /** * Creates a new {@link KeyValueRepositoryFactoryBean} for the given repository interface. - * + * * @param repositoryInterface must not be {@literal null}. */ public KeyValueRepositoryFactoryBean(Class repositoryInterface) { @@ -52,7 +51,7 @@ public class KeyValueRepositoryFactoryBean, S, ID ex /** * Configures the {@link KeyValueOperations} to be used for the repositories. - * + * * @param operations must not be {@literal null}. */ public void setKeyValueOperations(KeyValueOperations operations) { @@ -73,7 +72,7 @@ public class KeyValueRepositoryFactoryBean, S, ID ex /** * Configures the {@link QueryCreatorType} to be used. - * + * * @param queryCreator must not be {@literal null}. */ public void setQueryCreator(Class> queryCreator) { 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 99cec6f..84b242a 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 @@ -17,7 +17,6 @@ package org.springframework.data.keyvalue.repository.support; import static org.springframework.data.keyvalue.repository.support.KeyValueQuerydslUtils.*; -import java.io.Serializable; import java.util.Optional; import org.springframework.dao.IncorrectResultSizeDataAccessException; @@ -51,7 +50,7 @@ import com.querydsl.core.types.dsl.PathBuilder; * @param the domain type to manage * @param the identifier type of the domain type */ -public class QuerydslKeyValueRepository extends SimpleKeyValueRepository +public class QuerydslKeyValueRepository extends SimpleKeyValueRepository 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 3849342..74d1396 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 @@ -119,7 +119,7 @@ public class SimpleKeyValueRepository implements KeyValueRepository findById(ID id) { @@ -128,7 +128,7 @@ public class SimpleKeyValueRepository implements KeyValueRepository implements KeyValueRepository(Arrays.asList(events))); } + @Data + @AllArgsConstructor static class Foo { String foo; - - public Foo(String foo) { - this.foo = foo; - } - - public String getFoo() { - return foo; - } - - @Override - public int hashCode() { - return ObjectUtils.nullSafeHashCode(this.foo); - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (!(obj instanceof Foo)) { - return false; - } - Foo other = (Foo) obj; - return ObjectUtils.nullSafeEquals(this.foo, other.foo); - } - } + @Data + @AllArgsConstructor class Bar { String bar; - - public Bar(String bar) { - this.bar = bar; - } - - public String getBar() { - return bar; - } - - @Override - public int hashCode() { - return ObjectUtils.nullSafeHashCode(this.bar); - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (!(obj instanceof Bar)) { - return false; - } - Bar other = (Bar) obj; - return ObjectUtils.nullSafeEquals(this.bar, other.bar); - } - } + @Data static class ClassWithStringId { @Id String id; String value; - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ObjectUtils.nullSafeHashCode(this.id); - result = prime * result + ObjectUtils.nullSafeHashCode(this.value); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (!(obj instanceof ClassWithStringId)) { - return false; - } - ClassWithStringId other = (ClassWithStringId) obj; - if (!ObjectUtils.nullSafeEquals(this.id, other.id)) { - return false; - } - if (!ObjectUtils.nullSafeEquals(this.value, other.value)) { - return false; - } - return true; - } - } } diff --git a/src/test/java/org/springframework/data/keyvalue/core/SpelQueryEngineUnitTests.java b/src/test/java/org/springframework/data/keyvalue/core/SpelQueryEngineUnitTests.java index d071c17..119e9d0 100644 --- a/src/test/java/org/springframework/data/keyvalue/core/SpelQueryEngineUnitTests.java +++ b/src/test/java/org/springframework/data/keyvalue/core/SpelQueryEngineUnitTests.java @@ -45,6 +45,7 @@ import org.springframework.expression.spel.support.StandardEvaluationContext; * * @author Martin Macko * @author Oliver Gierke + * @author Mark Paluch */ @RunWith(MockitoJUnitRunner.class) public class SpelQueryEngineUnitTests { @@ -54,14 +55,14 @@ public class SpelQueryEngineUnitTests { @Mock KeyValueAdapter adapter; - SpelQueryEngine engine; + SpelQueryEngine engine; Iterable people = Arrays.asList(BOB_WITH_FIRSTNAME, MIKE_WITHOUT_FIRSTNAME); @Before public void setUp() { - engine = new SpelQueryEngine<>(); + engine = new SpelQueryEngine(); engine.registerAdapter(adapter); } @@ -102,7 +103,7 @@ public class SpelQueryEngineUnitTests { return new SpelCriteria(creator.createQuery().getCriteria(), new StandardEvaluationContext(args)); } - static interface PersonRepository { + interface PersonRepository { Person findByFirstname(String firstname); } 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 8526133..8e1a011 100644 --- a/src/test/java/org/springframework/data/keyvalue/repository/SimpleKeyValueRepositoryUnitTests.java +++ b/src/test/java/org/springframework/data/keyvalue/repository/SimpleKeyValueRepositoryUnitTests.java @@ -22,7 +22,9 @@ import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.*; -import java.io.Serializable; +import lombok.Data; +import lombok.NoArgsConstructor; + import java.util.Arrays; import java.util.Optional; @@ -86,7 +88,7 @@ public class SimpleKeyValueRepositoryUnitTests { public void multipleSave() { Foo one = new Foo("one"); - Foo two = new Foo("one"); + Foo two = new Foo("two"); repo.saveAll(Arrays.asList(one, two)); verify(opsMock, times(1)).insert(eq(one)); @@ -121,25 +123,28 @@ public class SimpleKeyValueRepositoryUnitTests { } @Test // DATACMNS-525 + @SuppressWarnings("unchecked") public void findAllIds() { - when(opsMock.findById(any(Serializable.class), any(Class.class))).thenReturn(Optional.empty()); + when(opsMock.findById(any(), any(Class.class))).thenReturn(Optional.empty()); repo.findAllById(Arrays.asList("one", "two", "three")); verify(opsMock, times(3)).findById(anyString(), eq(Foo.class)); } @Test // DATAKV-186 + @SuppressWarnings("unchecked") public void existsByIdReturnsFalseForEmptyOptional() { - when(opsMock.findById(any(Serializable.class), any(Class.class))).thenReturn(Optional.empty()); + when(opsMock.findById(any(), any(Class.class))).thenReturn(Optional.empty()); assertThat(repo.existsById("one"), is(false)); } @Test // DATAKV-186 + @SuppressWarnings("unchecked") public void existsByIdReturnsTrueWhenOptionalValuePresent() { - when(opsMock.findById(any(Serializable.class), any(Class.class))).thenReturn(Optional.of(new Foo())); + when(opsMock.findById(any(), any(Class.class))).thenReturn(Optional.of(new Foo())); assertTrue(repo.existsById("one")); } @@ -168,6 +173,8 @@ public class SimpleKeyValueRepositoryUnitTests { verify(opsMock, times(1)).findAll(eq(Foo.class)); } + @Data + @NoArgsConstructor static class Foo { private @Id String id; @@ -175,65 +182,20 @@ public class SimpleKeyValueRepositoryUnitTests { private String name; private Bar bar; - public Foo() { - - } - public Foo(String name) { this.name = name; } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public Long getLongValue() { - return longValue; - } - - public void setLongValue(Long longValue) { - this.longValue = longValue; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public Bar getBar() { - return bar; - } - - public void setBar(Bar bar) { - this.bar = bar; - } - } + @Data static class Bar { private String bar; - - public String getBar() { - return bar; - } - - public void setBar(String bar) { - this.bar = bar; - } } @Persistent static class WithNumericId { @Id Integer id; - } } diff --git a/src/test/java/org/springframework/data/keyvalue/repository/support/KeyValueRepositoryFactoryBeanUnitTests.java b/src/test/java/org/springframework/data/keyvalue/repository/support/KeyValueRepositoryFactoryBeanUnitTests.java index c1051ad..2a630b4 100644 --- a/src/test/java/org/springframework/data/keyvalue/repository/support/KeyValueRepositoryFactoryBeanUnitTests.java +++ b/src/test/java/org/springframework/data/keyvalue/repository/support/KeyValueRepositoryFactoryBeanUnitTests.java @@ -19,8 +19,6 @@ import static org.hamcrest.CoreMatchers.*; import static org.junit.Assert.*; import static org.mockito.Mockito.*; -import java.io.Serializable; - import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -33,8 +31,9 @@ import org.springframework.data.repository.query.parser.AbstractQueryCreator; /** * Unit tests for {@link KeyValueRepositoryFactoryBean}. - * + * * @author Oliver Gierke + * @author Mark Paluch */ public class KeyValueRepositoryFactoryBeanUnitTests { @@ -44,7 +43,7 @@ public class KeyValueRepositoryFactoryBeanUnitTests { @Before public void setUp() { - this.factoryBean = new KeyValueRepositoryFactoryBean, Object, Serializable>( + this.factoryBean = new KeyValueRepositoryFactoryBean, Object, Object>( SampleRepository.class); } @@ -87,8 +86,7 @@ public class KeyValueRepositoryFactoryBeanUnitTests { Class> creatorType = (Class>) mock( AbstractQueryCreator.class).getClass(); - Class queryType = (Class) mock(KeyValuePartTreeQuery.class) - .getClass(); + Class queryType = mock(KeyValuePartTreeQuery.class).getClass(); factoryBean.setQueryCreator(creatorType); factoryBean.setKeyValueOperations(mock(KeyValueOperations.class)); @@ -102,5 +100,5 @@ public class KeyValueRepositoryFactoryBeanUnitTests { factoryBean.setQueryType(null); } - interface SampleRepository extends Repository {} + interface SampleRepository extends Repository {} }