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 38ed642..8d73945 100644 --- a/src/main/java/org/springframework/data/keyvalue/core/KeyValueTemplate.java +++ b/src/main/java/org/springframework/data/keyvalue/core/KeyValueTemplate.java @@ -18,7 +18,6 @@ package org.springframework.data.keyvalue.core; import java.io.Serializable; import java.util.ArrayList; import java.util.Collections; -import java.util.HashSet; import java.util.List; import java.util.Set; @@ -55,11 +54,12 @@ public class KeyValueTemplate implements KeyValueOperations, ApplicationEventPub private final KeyValueAdapter adapter; private final MappingContext, ? extends KeyValuePersistentProperty> mappingContext; private final IdentifierGenerator identifierGenerator; - private ApplicationEventPublisher eventPublisher; - private boolean publishEvents = true; - private final Set> eventTypesToPublish = new HashSet>(0); private PersistenceExceptionTranslator exceptionTranslator = DEFAULT_PERSISTENCE_EXCEPTION_TRANSLATOR; + private ApplicationEventPublisher eventPublisher; + private boolean publishEvents = true; + private @SuppressWarnings("rawtypes") Set> eventTypesToPublish = Collections + .emptySet(); /** * Create new {@link KeyValueTemplate} using the given {@link KeyValueAdapter} with a default @@ -88,6 +88,42 @@ public class KeyValueTemplate implements KeyValueOperations, ApplicationEventPub this.identifierGenerator = DefaultIdentifierGenerator.INSTANCE; } + /** + * Set the {@link PersistenceExceptionTranslator} used for converting {@link RuntimeException}. + * + * @param exceptionTranslator must not be {@literal null}. + */ + public void setExceptionTranslator(PersistenceExceptionTranslator exceptionTranslator) { + + Assert.notNull(exceptionTranslator, "ExceptionTranslator must not be null."); + this.exceptionTranslator = exceptionTranslator; + } + + /** + * Define the event types to publish via {@link ApplicationEventPublisher}. + * + * @param eventTypesToPublish use {@literal null} or {@link Collections#emptySet()} to stop publishing. + */ + @SuppressWarnings("rawtypes") + public void setEventTypesToPublish(Set> eventTypesToPublish) { + + if (CollectionUtils.isEmpty(eventTypesToPublish)) { + this.publishEvents = false; + } else { + this.publishEvents = true; + this.eventTypesToPublish = Collections.unmodifiableSet(eventTypesToPublish); + } + } + + /* + * (non-Javadoc) + * @see org.springframework.context.ApplicationEventPublisherAware#setApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) + */ + @Override + public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) { + this.eventPublisher = applicationEventPublisher; + } + /* * (non-Javadoc) * @see org.springframework.data.keyvalue.core.KeyValueOperations#insert(java.lang.Object) @@ -440,58 +476,17 @@ public class KeyValueTemplate implements KeyValueOperations, ApplicationEventPub this.adapter.clear(); } - /** - * Set the {@link PersistenceExceptionTranslator} used for converting {@link RuntimeException}. - * - * @param exceptionTranslator must not be {@literal null}. - */ - public void setExceptionTranslator(PersistenceExceptionTranslator exceptionTranslator) { - - Assert.notNull(exceptionTranslator, "ExceptionTranslator must not be null."); - this.exceptionTranslator = exceptionTranslator; - } - - /* - * (non-Javadoc) - * @see org.springframework.context.ApplicationEventPublisherAware#setApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) - */ - @Override - public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) { - this.eventPublisher = applicationEventPublisher; - } - - /** - * Define the event types to publish via {@link ApplicationEventPublisher}. - * - * @param eventTypesToPublish use {@literal null} or {@link Collections#emptySet()} to stop publishing. - */ - public void setEventTypesToPublish(Set> eventTypesToPublish) { - - this.eventTypesToPublish.clear(); - - if (!CollectionUtils.isEmpty(eventTypesToPublish)) { - - this.publishEvents = true; - this.eventTypesToPublish.addAll(eventTypesToPublish); - } else { - this.publishEvents = false; - } - } - private String resolveKeySpace(Class type) { return this.mappingContext.getPersistentEntity(type).getKeySpace(); } - private static boolean typeCheck(Class requiredType, Object candidate) { - return candidate == null ? true : ClassUtils.isAssignable(requiredType, candidate.getClass()); - } - private RuntimeException resolveExceptionIfPossible(RuntimeException e) { DataAccessException translatedException = exceptionTranslator.translateExceptionIfPossible(e); return translatedException != null ? translatedException : e; } + @SuppressWarnings("rawtypes") private void potentiallyPublishEvent(KeyValueEvent event) { if (eventPublisher == null) { @@ -503,4 +498,7 @@ public class KeyValueTemplate implements KeyValueOperations, ApplicationEventPub } } + private static boolean typeCheck(Class requiredType, Object candidate) { + return candidate == null ? true : ClassUtils.isAssignable(requiredType, candidate.getClass()); + } } 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 55f9a95..59ee40b 100644 --- a/src/test/java/org/springframework/data/keyvalue/core/KeyValueTemplateUnitTests.java +++ b/src/test/java/org/springframework/data/keyvalue/core/KeyValueTemplateUnitTests.java @@ -40,8 +40,8 @@ import org.springframework.context.ApplicationEventPublisher; import org.springframework.dao.DuplicateKeyException; import org.springframework.dao.InvalidDataAccessApiUsageException; import org.springframework.data.annotation.Id; -import org.springframework.data.keyvalue.TypeWithCustomComposedKeySpaceAnnotation; import org.springframework.data.keyvalue.SubclassOfTypeWithCustomComposedKeySpaceAnnotation; +import org.springframework.data.keyvalue.TypeWithCustomComposedKeySpaceAnnotation; import org.springframework.data.keyvalue.core.event.KeyValueEvent; import org.springframework.data.keyvalue.core.event.KeyValueEvent.AfterDeleteEvent; import org.springframework.data.keyvalue.core.event.KeyValueEvent.AfterDropKeySpaceEvent; @@ -58,6 +58,7 @@ import org.springframework.util.ObjectUtils; /** * @author Christoph Strobl * @author Thomas Darimont + * @author Oliver Gierke */ @RunWith(MockitoJUnitRunner.class) public class KeyValueTemplateUnitTests { @@ -66,9 +67,10 @@ public class KeyValueTemplateUnitTests { private static final Foo FOO_ONE = new Foo("one"); private static final Foo FOO_TWO = new Foo("two"); - private static final TypeWithCustomComposedKeySpaceAnnotation ALIASED = new TypeWithCustomComposedKeySpaceAnnotation("super"); - private static final SubclassOfTypeWithCustomComposedKeySpaceAnnotation SUBCLASS_OF_ALIASED = new SubclassOfTypeWithCustomComposedKeySpaceAnnotation("sub"); - + private static final TypeWithCustomComposedKeySpaceAnnotation ALIASED = new TypeWithCustomComposedKeySpaceAnnotation( + "super"); + private static final SubclassOfTypeWithCustomComposedKeySpaceAnnotation SUBCLASS_OF_ALIASED = new SubclassOfTypeWithCustomComposedKeySpaceAnnotation( + "sub"); private static final KeyValueQuery STRING_QUERY = new KeyValueQuery("foo == 'two'"); private @Mock KeyValueAdapter adapterMock; @@ -695,7 +697,7 @@ public class KeyValueTemplateUnitTests { assertThat(captor.getValue().getKeyspace(), is(Foo.class.getName())); } - @SuppressWarnings("unchecked") + @SuppressWarnings("rawtypes") private void setEventsToPublish(Class... events) { template.setEventTypesToPublish(new HashSet>(Arrays.asList(events))); }