cmsc433.p1
Class BasicLog

java.lang.Object
  extended by cmsc433.p1.BasicLog
All Implemented Interfaces:
Log

public class BasicLog
extends java.lang.Object
implements Log

A basic implementation of the Log interface. Stores a log of bounded size. This class should be thread-safe.


Constructor Summary
BasicLog(int logSize)
          Constructor specifies the size of the log.
 
Method Summary
 void add(LogRecord record)
          Adds a record to the log.
 java.util.List<LogRecord> flush()
          Returns a list of all records in the log.
 java.util.List<LogRecord> getAll(long windowMS)
          Returns all LogRecords whose timestamps are within a window of time; if the specified window is 0, all of the records are returned.
 void register(Listener<LogRecord> listener)
          Registers a new listener with this log.
 java.lang.String toString()
           
 void unregister(Listener<LogRecord> listener)
          Removes a previously-registered listener.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

BasicLog

public BasicLog(int logSize)
Constructor specifies the size of the log.

Parameters:
logSize - the number of records we can store in the log. Use 0 to make the log unlimited in size.
Method Detail

add

public void add(LogRecord record)
Adds a record to the log. If the log is at capacity, then the oldest record is discarded to make room for the new one.

Specified by:
add in interface Log
Parameters:
record - The record to be added.

getAll

public java.util.List<LogRecord> getAll(long windowMS)
Returns all LogRecords whose timestamps are within a window of time; if the specified window is 0, all of the records are returned. Note that inclusion is based on the timestamp of the LogRecord, not the time at which the record was stored in the Log.

Specified by:
getAll in interface Log
Parameters:
windowMS - return all records from the last [windowMS] milliseconds. If [windowMS] is 0, it returns all of the records; if it is less than 0, no records are returned.

flush

public java.util.List<LogRecord> flush()
Returns a list of all records in the log. Also clears the log.

Specified by:
flush in interface Log

register

public void register(Listener<LogRecord> listener)
Registers a new listener with this log. The listener will be notified whenever add is called.

Specified by:
register in interface Log
Parameters:
listener - The listener to register.

unregister

public void unregister(Listener<LogRecord> listener)
Removes a previously-registered listener.

Specified by:
unregister in interface Log
Parameters:
listener - The listener to remove. If this listener is not present, then unregister behaves as a no-op.

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object