diff options
Diffstat (limited to 'src/main/model/util/EventLog.java')
-rw-r--r-- | src/main/model/util/EventLog.java | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/src/main/model/util/EventLog.java b/src/main/model/util/EventLog.java new file mode 100644 index 0000000..507c9b2 --- /dev/null +++ b/src/main/model/util/EventLog.java @@ -0,0 +1,60 @@ +package model.util; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; + +/** + * Represents a log of alarm system events. + * We use the Singleton Design Pattern to ensure that there is only + * one EventLog in the system and that the system has global access + * to the single instance of the EventLog. + */ +public class EventLog implements Iterable<Event> { + /** the only EventLog in the system (Singleton Design Pattern) */ + private static EventLog theLog; + private Collection<Event> events; + + /** + * Prevent external construction. + * (Singleton Design Pattern). + */ + private EventLog() { + events = new ArrayList<Event>(); + } + + /** + * Gets instance of EventLog - creates it + * if it doesn't already exist. + * (Singleton Design Pattern) + * @return instance of EventLog + */ + public static EventLog getInstance() { + if (theLog == null) { + theLog = new EventLog(); + } + + return theLog; + } + + /** + * Adds an event to the event log. + * @param e the event to be added + */ + public void logEvent(Event e) { + events.add(e); + } + + /** + * Clears the event log and logs the event. + */ + public void clear() { + events.clear(); + logEvent(new Event("Event log cleared.")); + } + + @Override + public Iterator<Event> iterator() { + return events.iterator(); + } +} |