| 1 | package com.reallifedeveloper.common.domain.event; | |
| 2 | ||
| 3 | import java.util.ArrayList; | |
| 4 | import java.util.List; | |
| 5 | ||
| 6 | /** | |
| 7 | * A publisher of domain events that holds subscribers in a list. Publishing is handled | |
| 8 | * synchronously; to handle events asynchronously, a subscriber could send a message to | |
| 9 | * a message queue or store the event for later processing. | |
| 10 | * <p> | |
| 11 | * The idea behind this publisher is that it will be configured once and for all, for | |
| 12 | * example using Spring: | |
| 13 | * | |
| 14 | * <pre> | |
| 15 | * <bean class="th.co.edge.domain.event.SimpleDomainEventPublisher"> | |
| 16 | * <constructor-arg> | |
| 17 | * <list> | |
| 18 | * <bean class="com.foo.FooSubscriber" /> | |
| 19 | * <bean class="com.foo.BarSubscriber" /> | |
| 20 | * </list> | |
| 21 | * </constructor-arg> | |
| 22 | * </bean> | |
| 23 | * </pre> | |
| 24 | * <p> | |
| 25 | * The publisher can then be injected in the classes that need to publish events: | |
| 26 | * | |
| 27 | * <pre> | |
| 28 | * @Autowired | |
| 29 | * private DomainEventSubscriber eventSubscriber; | |
| 30 | * </pre> | |
| 31 | * <p> | |
| 32 | * Used this way in a normal enterprise application, several threads may call the | |
| 33 | * {@link #publish(DomainEvent)} method simultaneously, so the subscribers should | |
| 34 | * be thread safe. | |
| 35 | * | |
| 36 | * @author RealLifeDeveloper | |
| 37 | */ | |
| 38 | public final class SimpleDomainEventPublisher implements DomainEventPublisher { | |
| 39 | ||
| 40 | private List<DomainEventSubscriber<? extends DomainEvent>> subscribers = new ArrayList<>(); | |
| 41 | ||
| 42 | /** | |
| 43 | * Creates a new {@code SimpleDomainEventPublisher} with no subscribers registered. | |
| 44 | * <p> | |
| 45 | * Use the {@link #subscribe(DomainEventSubscriber)} to add subscribers. | |
| 46 | */ | |
| 47 | public SimpleDomainEventPublisher() { | |
| 48 | super(); | |
| 49 | } | |
| 50 | ||
| 51 | /** | |
| 52 | * Creates a new {@code SimpleDomainEventPublisher} with a number of subscribers | |
| 53 | * registered to be notified when events are published. | |
| 54 | * | |
| 55 | * @param subscribers a list of subscribers to notify when events are published | |
| 56 | * @throws IllegalArgumentException if {@code subscribers} is {@code null} | |
| 57 | */ | |
| 58 | public SimpleDomainEventPublisher(List<DomainEventSubscriber<? extends DomainEvent>> subscribers) { | |
| 59 |
1
1. <init> : negated conditional → KILLED |
if (subscribers == null) { |
| 60 | throw new IllegalArgumentException("subscribers must not be null"); | |
| 61 | } | |
| 62 | this.subscribers = new ArrayList<>(subscribers); | |
| 63 | } | |
| 64 | ||
| 65 | /** | |
| 66 | * Publishes a domain event, i.e., calls the {@link DomainEventSubscriber#handleEvent(DomainEvent)} | |
| 67 | * method for each registered subscriber. | |
| 68 | * | |
| 69 | * @param event the domain event to publish | |
| 70 | * @throws IllegalArgumentException if {@code event} is {@code null} | |
| 71 | */ | |
| 72 | @Override | |
| 73 | public void publish(DomainEvent event) { | |
| 74 |
1
1. publish : negated conditional → KILLED |
if (event == null) { |
| 75 | throw new IllegalArgumentException("event must not be null"); | |
| 76 | } | |
| 77 | for (DomainEventSubscriber<? extends DomainEvent> subscriber : subscribers) { | |
| 78 |
1
1. publish : negated conditional → KILLED |
if (subscriber.eventType().isAssignableFrom(event.getClass())) { |
| 79 | @SuppressWarnings("unchecked") | |
| 80 | DomainEventSubscriber<DomainEvent> s = (DomainEventSubscriber<DomainEvent>) subscriber; | |
| 81 |
1
1. publish : removed call to com/reallifedeveloper/common/domain/event/DomainEventSubscriber::handleEvent → KILLED |
s.handleEvent(event); |
| 82 | } | |
| 83 | } | |
| 84 | } | |
| 85 | ||
| 86 | /** | |
| 87 | * Registers an event handler with this publisher. | |
| 88 | * | |
| 89 | * @param subscriber the event handler to register | |
| 90 | * @throws IllegalArgumentException if {@code subscriber} is {@code null} | |
| 91 | */ | |
| 92 | @Override | |
| 93 | public void subscribe(DomainEventSubscriber<? extends DomainEvent> subscriber) { | |
| 94 |
1
1. subscribe : negated conditional → KILLED |
if (subscriber == null) { |
| 95 | throw new IllegalArgumentException("subscriber must not be null"); | |
| 96 | } | |
| 97 | subscribers.add(subscriber); | |
| 98 | } | |
| 99 | ||
| 100 | } | |
Mutations | ||
| 59 |
1.1 |
|
| 74 |
1.1 |
|
| 78 |
1.1 |
|
| 81 |
1.1 |
|
| 94 |
1.1 |