-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from Team-40-IDATT2003/75-mvc-and-publishersub…
…scriber 75 mvc and publishersubscriber
- Loading branch information
Showing
17 changed files
with
813 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -54,3 +54,6 @@ failsafe-reports/ | |
| # ===== Coverage tools ===== | ||
| coverage/ | ||
| jacoco.exec | ||
|
|
||
| # ====== Stock data files ======= # | ||
| .txt | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/service/event/EventData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package edu.ntnu.idi.idatt2003.g40.mappe.service.event; | ||
|
|
||
| /** | ||
| * Data object sent through the event system by the {@link EventManager}. | ||
| * | ||
| * @param <T> the type of data this object represents. | ||
| * @param eventType the event type this object represents. | ||
| * @param data the data this object contains. | ||
| * | ||
| */ | ||
| public record EventData<T>(EventType eventType, T data) { | ||
|
|
||
| } |
57 changes: 57 additions & 0 deletions
57
src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/service/event/EventManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package edu.ntnu.idi.idatt2003.g40.mappe.service.event; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.EnumMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Event broker in the pub/sub model. | ||
| * | ||
| * <p>Read more about pub/sub | ||
| * <a href = "https://www.geeksforgeeks.org/system-design/what-is-pub-sub/"> | ||
| * Here | ||
| * </a> | ||
| * </p> | ||
| * | ||
| * @see EventSubscriber | ||
| * @see EventPublisher | ||
| * | ||
| */ | ||
| public final class EventManager { | ||
|
|
||
| /** | ||
| * Map where key is {@link EventType} and value is list of {@link EventSubscriber} objects. | ||
| * | ||
| * <p>Used to identify which subscribers to fire the event towards.</p> | ||
| * | ||
| */ | ||
| private final Map<EventType, List<EventSubscriber>> subscriberMap = | ||
| new EnumMap<>(EventType.class); | ||
|
|
||
| /** | ||
| * Method for adding a subscriber to the subscriber map given an event type. | ||
| * | ||
| * @param subscriber the {@link EventSubscriber} object to add. | ||
| * @param eventType the {@link EventType} this subscriber should subscribe to. | ||
| * | ||
| * | ||
| */ | ||
| public void addSubscriber(final EventSubscriber subscriber, final EventType eventType) { | ||
| subscriberMap.computeIfAbsent(eventType, k -> new ArrayList<>()).add(subscriber); | ||
| } | ||
|
|
||
| /** | ||
| * Method for invoking event to all subscriber of that type. | ||
| * | ||
| * @param <T> the type of event data to send. | ||
| * @param data the data to send. | ||
| * | ||
| * | ||
| */ | ||
| public <T> void invokeEvent(final EventData<T> data) { | ||
| for (EventSubscriber e : subscriberMap.get(data.eventType())) { | ||
| e.handleEvent(data); | ||
| } | ||
| } | ||
| } |
20 changes: 20 additions & 0 deletions
20
src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/service/event/EventPublisher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package edu.ntnu.idi.idatt2003.g40.mappe.service.event; | ||
|
|
||
| /** | ||
| * Interface representing a publisher in the pub/sub model (observer pattern). | ||
| * | ||
| * @see EventManager | ||
| * @see EventSubscriber | ||
| */ | ||
| public interface EventPublisher { | ||
|
|
||
| /** | ||
| * Method for triggering (invoking) the event. | ||
| * | ||
| * @param <T> the type of event data to invoke. | ||
| * @param data the data to invoke. | ||
| * @param eventManager the {@link EventManager} object to use as broker when invoking event. | ||
| */ | ||
| <T> void invoke(EventData<T> data, EventManager eventManager); | ||
| } | ||
|
|
18 changes: 18 additions & 0 deletions
18
src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/service/event/EventSubscriber.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package edu.ntnu.idi.idatt2003.g40.mappe.service.event; | ||
|
|
||
| /** | ||
| * Interface representing a subscriber in a pub/sub model. | ||
| * | ||
| * @see EventManager | ||
| * @see EventSubscriber | ||
| */ | ||
| public interface EventSubscriber { | ||
|
|
||
| /** | ||
| * Method for handling the event subscribed to. | ||
| * | ||
| * @param <T> the type of event data to handle. | ||
| * @param data the actual data to handle. | ||
| */ | ||
| <T> void handleEvent(EventData<T> data); | ||
| } |
38 changes: 38 additions & 0 deletions
38
src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/service/event/EventType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package edu.ntnu.idi.idatt2003.g40.mappe.service.event; | ||
|
|
||
| /** | ||
| * Enum representing different event types. | ||
| * | ||
| * <p>Examples:</p> | ||
| * <ul> | ||
| * <li>Scene changes</li> | ||
| * <li>Database operations</li> | ||
| * <li>Other</li> | ||
| * </ul> | ||
| * | ||
| * | ||
| */ | ||
| public enum EventType { | ||
|
|
||
| /** | ||
| * Event type representing events that causes the current scene to change. | ||
| * | ||
| * <p>Primarily handled by the active | ||
| * {@link edu.ntnu.idi.idatt2003.g40.mappe.view.ViewManager} object.</p> | ||
| * | ||
| * @see edu.ntnu.idi.idatt2003.g40.mappe.view.ViewManager | ||
| * | ||
| */ | ||
| SCENE_CHANGE, | ||
|
|
||
| /** | ||
| * Event type representing events that causes the scene to change to the previous one. | ||
| * | ||
| * <p>Primarily handled by the active | ||
| * {@link edu.ntnu.idi.idatt2003.g40.mappe.view.ViewManager} object.</p> | ||
| * | ||
| * @see edu.ntnu.idi.idatt2003.g40.mappe.view.ViewManager | ||
| * | ||
| */ | ||
| SCENE_BACK, | ||
| } |
16 changes: 16 additions & 0 deletions
16
src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/utils/ConfigValues.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package edu.ntnu.idi.idatt2003.g40.mappe.utils; | ||
|
|
||
| public enum ConfigValues { | ||
| VIEWPORT_WIDTH(600), | ||
| VIEWPORT_HEIGHT(600); | ||
|
|
||
| private int value; | ||
|
|
||
| ConfigValues(final int value) { | ||
| this.value = value; | ||
| } | ||
|
|
||
| public int getValue() { | ||
| return value; | ||
| } | ||
| } |
77 changes: 77 additions & 0 deletions
77
src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/ViewController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| package edu.ntnu.idi.idatt2003.g40.mappe.view; | ||
|
|
||
| import edu.ntnu.idi.idatt2003.g40.mappe.service.event.EventData; | ||
| import edu.ntnu.idi.idatt2003.g40.mappe.service.event.EventManager; | ||
| import edu.ntnu.idi.idatt2003.g40.mappe.service.event.EventPublisher; | ||
|
|
||
| /** | ||
| * Handles logic and event publishing for {@link ViewElement} objects. | ||
| * | ||
| * <p>Implements {@link EventPublisher}</p> | ||
| * | ||
| * @param <T1> The type of {@link ViewElement} this controller is attached to. We use generics | ||
| * because we want to call methods that may be specific for certain view element | ||
| * objects, and we want to reduce casting. | ||
| * @see ViewElement | ||
| * @see EventPublisher | ||
| * @see EventManager | ||
| * | ||
| */ | ||
| public abstract class ViewController<T1 extends ViewElement<?>> | ||
| implements EventPublisher { | ||
|
|
||
| /** | ||
| * The {@link ViewElement} object this controller is associated with. | ||
| * | ||
| */ | ||
| private final T1 viewElement; | ||
| /** | ||
| * The {@link EventManager} object to send events to. | ||
| * | ||
| */ | ||
| private final EventManager eventManager; | ||
|
|
||
| /** | ||
| * Constructor. | ||
| * | ||
| * @param viewElement the instance of type T1 this controller is attached to. | ||
| * @param eventManager the {@link EventManager} object this controller communicates with. | ||
| * | ||
| */ | ||
| protected ViewController(final T1 viewElement, final EventManager eventManager) { | ||
| this.viewElement = viewElement; | ||
| this.eventManager = eventManager; | ||
| initInteractions(); | ||
| } | ||
|
|
||
| /** | ||
| * Getter method for the event manager. | ||
| * | ||
| * @return the {@link EventManager} object. | ||
| * | ||
| */ | ||
| protected EventManager getEventManager() { | ||
| return eventManager; | ||
| } | ||
|
|
||
| /** | ||
| * Getter method for the current view element. | ||
| * | ||
| * @return the {@link ViewElement} object. | ||
| * | ||
| */ | ||
| public T1 getViewElement() { | ||
| return viewElement; | ||
| } | ||
|
|
||
| /** | ||
| * Abstract method to initialize logic for the view element. | ||
| * | ||
| */ | ||
| protected abstract void initInteractions(); | ||
|
|
||
| @Override | ||
| public final <T> void invoke(final EventData<T> data, final EventManager eventManager) { | ||
| eventManager.invokeEvent(data); | ||
| } | ||
| } |
39 changes: 39 additions & 0 deletions
39
src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/ViewData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package edu.ntnu.idi.idatt2003.g40.mappe.view; | ||
|
|
||
| /** | ||
| * Object containing data about {@link ViewElement} objects. | ||
| * | ||
| * <p>Sent in {@link edu.ntnu.idi.idatt2003.g40.mappe.service.event.EventData} | ||
| * objects of type SCENE_CHANGE</p> | ||
| * | ||
| * <p>Handled by the {@link ViewManager} to update and change views</p> | ||
| * | ||
| */ | ||
| public class ViewData { | ||
|
|
||
| /** | ||
| * Name of the scene. | ||
| * | ||
| */ | ||
| private final String sceneName; | ||
|
|
||
| /** | ||
| * Constructor. | ||
| * | ||
| * @param sceneName the name of the scene. | ||
| * | ||
| */ | ||
| public ViewData(final String sceneName) { | ||
| this.sceneName = sceneName; | ||
| } | ||
|
|
||
| /** | ||
| * Getter method for the scene name. | ||
| * | ||
| * @return scene name. | ||
| * | ||
| */ | ||
| public String getSceneName() { | ||
| return sceneName; | ||
| } | ||
| } |
Oops, something went wrong.