In this example, I created a class that implements the EventStore
interface.
public interface EventStore {
void insert(Event event);
void removeAll(String type);
EventIterator query(String type, LocalDateTime startTime, LocalDateTime endTime);
}
Events may be stored in memory, data files, a database, on a remote server, etc. For this example, was implemented an in-memory event event store.
The events in memory was stored in a Syncronized List as below:
public class EventStoreImpl implements EventStore {
// Creating a thread-safe Event List
@Getter
final List<Event> events = Collections.synchronizedList(new ArrayList());
...
}
Collections.synchronizedList() method Returns a synchronized (thread-safe) list backed by the specified list. In order to guarantee serial access, it is critical that all access to the backing list is accomplished through the returned list.