com.github.grzesiek_galezowski.test_environment.buffer.implementation.BufferItemNotificationSubscribers Maven / Gradle / Ivy
package com.github.grzesiek_galezowski.test_environment.buffer.implementation;
import com.github.grzesiek_galezowski.test_environment.buffer.interfaces.BufferObserver;
import com.github.grzesiek_galezowski.test_environment.buffer.interfaces.ItemSubscriber;
import lombok.val;
import org.assertj.core.api.Condition;
import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
public class BufferItemNotificationSubscribers {
private final BufferObserver observer;
private final List, Condition>>
subscriberEntries = new ArrayList<>();
public BufferItemNotificationSubscribers(
final BufferObserver observer) {
this.observer = observer;
}
public void notifySubscribersAbout(final T object) {
for (val subscriberEntry : subscriberEntries) {
val subscriber = subscriberEntry.getKey();
val condition = subscriberEntry.getValue();
try {
if(condition.matches(object)) {
subscriber.itemStored(object);
}
} catch (Throwable t) {
observer.exceptionWhileNotifyingSubscriberAboutStoredItem(
subscriber, object);
}
}
}
public void add(final ItemSubscriber subscriber) {
subscriberEntries.add(new SimpleImmutableEntry<>(
subscriber, trueCondition()));
}
public void add(final ItemSubscriber subscriber, final Condition condition) {
subscriberEntries.add(new SimpleImmutableEntry<>(
subscriber, condition
));
}
public Condition trueCondition() {
return new Condition<>(t -> true, "");
}
}