diff --git a/src/main/java/me/goudham/EventManager.java b/src/main/java/me/goudham/EventManager.java new file mode 100644 index 0000000..145bb30 --- /dev/null +++ b/src/main/java/me/goudham/EventManager.java @@ -0,0 +1,53 @@ +package me.goudham; + +import java.awt.image.BufferedImage; +import java.util.ArrayList; +import java.util.List; +import me.goudham.event.ClipboardEvent; + +/** + * Stores all eventListeners and produces notifications that are to be consumed by users using {@link MyClipboard} + */ +class EventManager { + List eventListeners = new ArrayList<>(); + + /** + * Adds a {@link ClipboardEvent} to the {@code eventListeners} + * + * @param clipboardEvent The {@link ClipboardEvent} to be added + */ + void subscribe(ClipboardEvent clipboardEvent) { + eventListeners.add(clipboardEvent); + } + + /** + * Removes a {@link ClipboardEvent} from the {@code eventListeners} + * + * @param clipboardEvent The {@link ClipboardEvent} to be removed + */ + void unsubscribe(ClipboardEvent clipboardEvent) { + eventListeners.remove(clipboardEvent); + } + + /** + * Produces {@link String} change notifications to all consumers listening + * + * @param stringContent {@link String} to be consumed + */ + void notifyStringEvent(String stringContent) { + for (ClipboardEvent clipboardEvent : eventListeners) { + clipboardEvent.onCopyString(stringContent); + } + } + + /** + * Produces {@link BufferedImage} change notifications to all consumers listening + * + * @param imageContent {@link BufferedImage} to be consumed + */ + void notifyImageEvent(BufferedImage imageContent) { + for (ClipboardEvent clipboardEvent : eventListeners) { + clipboardEvent.onCopyImage(imageContent); + } + } +}