DATAJDBC-454 - Polishing.
Reformat code. Tweak reference doc wording. Original pull request: #199.
This commit is contained in:
@@ -28,8 +28,8 @@ import org.springframework.context.ApplicationEvent;
|
||||
public abstract class AbstractRelationalEvent<E> extends ApplicationEvent implements RelationalEvent<E> {
|
||||
|
||||
/**
|
||||
* Creates an event with the given source.
|
||||
* The source might be an entity or an id of an entity, depending on the actual event subclass.
|
||||
* Creates an event with the given source. The source might be an entity or an id of an entity, depending on the
|
||||
* actual event subclass.
|
||||
*
|
||||
* @param source must not be {@literal null}.
|
||||
*/
|
||||
|
||||
@@ -24,11 +24,13 @@ import org.springframework.core.GenericTypeResolver;
|
||||
* Base class to implement domain class specific {@link ApplicationListener} classes.
|
||||
*
|
||||
* @param <E>
|
||||
* @author Jens Schauder
|
||||
* @since 2.0
|
||||
*/
|
||||
public class AbstractRelationalEventListener<E> implements ApplicationListener<AbstractRelationalEvent<?>> {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(AbstractRelationalEventListener.class);
|
||||
|
||||
private final Class<?> domainClass;
|
||||
|
||||
/**
|
||||
|
||||
@@ -19,8 +19,8 @@ import org.springframework.data.relational.core.conversion.AggregateChange;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
* Gets published after deletion of an entity. It will have a {@link Identifier} identifier. If the entity is {@literal null} or
|
||||
* not depends on the delete method used.
|
||||
* Gets published after deletion of an entity. It will have a {@link Identifier} identifier. If the entity is
|
||||
* {@literal null} or not depends on the delete method used.
|
||||
*
|
||||
* @author Jens Schauder
|
||||
* @since 2.0
|
||||
@@ -38,6 +38,4 @@ public class AfterDeleteEvent<E> extends RelationalDeleteEvent<E> {
|
||||
public AfterDeleteEvent(Identifier id, @Nullable E instance, AggregateChange<E> change) {
|
||||
super(id, instance, change);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -34,8 +34,6 @@ public class BeforeConvertEvent<E> extends RelationalSaveEvent<E> {
|
||||
* aggregate is considered new in {@link AggregateChange#getKind()}. Must not be {@literal null}.
|
||||
*/
|
||||
public BeforeConvertEvent(E instance, AggregateChange<E> change) {
|
||||
|
||||
super(instance, change);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,5 +36,4 @@ public class BeforeDeleteEvent<E> extends RelationalDeleteEvent<E> {
|
||||
public BeforeDeleteEvent(Identifier id, @Nullable E entity, AggregateChange<E> change) {
|
||||
super(id, entity, change);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -23,17 +23,16 @@ import org.springframework.data.relational.core.conversion.AggregateChange;
|
||||
*
|
||||
* @author Jens Schauder
|
||||
*/
|
||||
public class BeforeSaveEvent<E> extends RelationalSaveEvent<E>{
|
||||
public class BeforeSaveEvent<E> extends RelationalSaveEvent<E> {
|
||||
|
||||
private static final long serialVersionUID = -4935804431519314116L;
|
||||
|
||||
/**
|
||||
* @param instance the entity about to get saved. Must not be {@literal null}.
|
||||
* @param change the {@link AggregateChange} that is going to get applied to the database. Must not be {@literal null}.
|
||||
*
|
||||
* @param change the {@link AggregateChange} that is going to get applied to the database. Must not be
|
||||
* {@literal null}.
|
||||
*/
|
||||
public BeforeSaveEvent(E instance, AggregateChange<E> change) {
|
||||
super(instance, change);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,6 @@
|
||||
*/
|
||||
package org.springframework.data.relational.core.mapping.event;
|
||||
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
import org.springframework.data.relational.core.conversion.AggregateChange;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
@@ -23,16 +22,17 @@ import org.springframework.util.Assert;
|
||||
/**
|
||||
* Super class for events produced during deleting an aggregate. Such events have an {@link Identifier} and an
|
||||
* {@link AggregateChange} and may also have an entity if the entity was provided to the method performing the delete.
|
||||
*
|
||||
*
|
||||
* @author Jens Schauder
|
||||
* @since 2.0
|
||||
*/
|
||||
public abstract class RelationalDeleteEvent<E> extends AbstractRelationalEvent<E> implements WithId<E>, WithAggregateChange<E> {
|
||||
public abstract class RelationalDeleteEvent<E> extends AbstractRelationalEvent<E>
|
||||
implements WithId<E>, WithAggregateChange<E> {
|
||||
|
||||
private static final long serialVersionUID = -8071323168471611098L;
|
||||
|
||||
private final Identifier id;
|
||||
@Nullable private final E entity;
|
||||
private final @Nullable E entity;
|
||||
private final AggregateChange<E> change;
|
||||
|
||||
/**
|
||||
@@ -69,7 +69,7 @@ public abstract class RelationalDeleteEvent<E> extends AbstractRelationalEvent<E
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<? extends E> getType() {
|
||||
public Class<E> getType() {
|
||||
return change.getEntityType();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,5 +37,5 @@ public interface RelationalEvent<E> {
|
||||
* @return the type of the entity to which the event relates.
|
||||
* @since 2.0
|
||||
*/
|
||||
Class<? extends E> getType();
|
||||
Class<E> getType();
|
||||
}
|
||||
|
||||
@@ -15,11 +15,9 @@
|
||||
*/
|
||||
package org.springframework.data.relational.core.mapping.event;
|
||||
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
|
||||
/**
|
||||
* An event that is guaranteed to have an entity.
|
||||
*
|
||||
*
|
||||
* @author Jens Schauder
|
||||
*/
|
||||
public class RelationalEventWithEntity<E> extends AbstractRelationalEvent<E> implements WithEntity<E> {
|
||||
@@ -42,8 +40,9 @@ public class RelationalEventWithEntity<E> extends AbstractRelationalEvent<E> imp
|
||||
return entity;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Class<? extends E> getType() {
|
||||
return (Class<? extends E>) entity.getClass();
|
||||
public Class<E> getType() {
|
||||
return (Class<E>) entity.getClass();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ import org.springframework.util.Assert;
|
||||
/**
|
||||
* Events triggered during saving of an aggregate. Events of this type always have an {@link AggregateChange} and an
|
||||
* entity.
|
||||
*
|
||||
*
|
||||
* @author Jens Schauder
|
||||
* @since 2.0
|
||||
*/
|
||||
@@ -34,7 +34,6 @@ public abstract class RelationalSaveEvent<E> extends RelationalEventWithEntity<E
|
||||
super(entity);
|
||||
|
||||
Assert.notNull(change, "Change must not be null");
|
||||
|
||||
this.change = change;
|
||||
}
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ import org.springframework.data.relational.core.conversion.AggregateChange;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link AbstractRelationalEventListener}.
|
||||
*
|
||||
*
|
||||
* @author Mark Paluch
|
||||
* @author Jens Schauder
|
||||
*/
|
||||
@@ -33,7 +33,7 @@ public class AbstractRelationalEventListenerUnitTests {
|
||||
|
||||
List<String> events = new ArrayList<>();
|
||||
EventListenerUnderTest listener = new EventListenerUnderTest();
|
||||
private DummyEntity dummyEntity = new DummyEntity();
|
||||
DummyEntity dummyEntity = new DummyEntity();
|
||||
|
||||
@Test // DATAJDBC-454
|
||||
public void afterLoad() {
|
||||
@@ -91,7 +91,7 @@ public class AbstractRelationalEventListenerUnitTests {
|
||||
String notADummyEntity = "I'm not a dummy entity";
|
||||
|
||||
listener.onApplicationEvent(
|
||||
new AfterDeleteEvent<>(Identifier.of(23), notADummyEntity, AggregateChange.forDelete(notADummyEntity)));
|
||||
new AfterDeleteEvent<>(Identifier.of(23), String.class, AggregateChange.forDelete(notADummyEntity)));
|
||||
|
||||
assertThat(events).isEmpty();
|
||||
}
|
||||
|
||||
@@ -609,7 +609,7 @@ For example, the following listener gets invoked before an aggregate gets saved:
|
||||
[source,java]
|
||||
----
|
||||
@Bean
|
||||
public ApplicationListener<BeforeSaveEvent> loggingSaves() {
|
||||
public ApplicationListener<BeforeSaveEvent<Object>> loggingSaves() {
|
||||
|
||||
return event -> {
|
||||
|
||||
@@ -621,7 +621,7 @@ public ApplicationListener<BeforeSaveEvent> loggingSaves() {
|
||||
====
|
||||
|
||||
If you want to handle events only for a specific domain type you may derive your listener from `AbstractRelationalEventListener` and overwrite one or more of the `onXXX` methods,
|
||||
where `XXX` stands for an event type. It will only get invoked for events related to the domain type you used in the declaration and provides typed events, so no casting of the entity is required.
|
||||
where `XXX` stands for an event type. Callback methods will only get invoked for events related to the domain type and their subtypes so you don't require further casting.
|
||||
|
||||
====
|
||||
[source,java]
|
||||
@@ -630,7 +630,7 @@ public class PersonLoadListener extends AbstractRelationalEventListener<Person>
|
||||
|
||||
@Override
|
||||
protected void onAfterLoad(AfterLoadEvent<Person> personLoad) {
|
||||
LOG.info(personLoad.getEntity().setLoadTimeStamp(new Date());
|
||||
LOG.info(personLoad.getEntity());
|
||||
}
|
||||
}
|
||||
----
|
||||
|
||||
Reference in New Issue
Block a user