diff options
author | Ulf Adams <ulfjack@google.com> | 2015-10-27 17:04:10 +0000 |
---|---|---|
committer | David Chen <dzc@google.com> | 2015-10-27 19:44:07 +0000 |
commit | d0e65435f4efcf0560f89b54b135f00f290822ef (patch) | |
tree | f003e5e9202ae1b69312de441193f734e4c54d8e | |
parent | 726415ca0addac817217bb6d3c6c67c14576304b (diff) |
Make EventCollector thread-safe for modifications.
--
MOS_MIGRATED_REVID=106407898
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/events/AbstractEventHandler.java | 5 | ||||
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/events/EventCollector.java | 25 |
2 files changed, 16 insertions, 14 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/events/AbstractEventHandler.java b/src/main/java/com/google/devtools/build/lib/events/AbstractEventHandler.java index a7f5e1d442..652600c683 100644 --- a/src/main/java/com/google/devtools/build/lib/events/AbstractEventHandler.java +++ b/src/main/java/com/google/devtools/build/lib/events/AbstractEventHandler.java @@ -16,11 +16,10 @@ package com.google.devtools.build.lib.events; import java.util.Set; /** - * An abstract event handler that keeps track of the event mask. Events - * matching the mask will be handled. + * An abstract event handler that keeps track of the event mask. Events matching the mask will be + * handled. */ public abstract class AbstractEventHandler implements EventHandler { - private final Set<EventKind> mask; /** diff --git a/src/main/java/com/google/devtools/build/lib/events/EventCollector.java b/src/main/java/com/google/devtools/build/lib/events/EventCollector.java index 509083ae86..0d309439db 100644 --- a/src/main/java/com/google/devtools/build/lib/events/EventCollector.java +++ b/src/main/java/com/google/devtools/build/lib/events/EventCollector.java @@ -23,13 +23,11 @@ import java.util.Iterator; import java.util.Set; /** - * An {@link EventHandler} that collects all events it encounters, and makes - * them available via the {@link Iterable} interface. The collected events - * contain not just the original event information but also the location - * context. + * An {@link EventHandler} that collects all events it encounters, and makes them available via the + * {@link Iterable} interface. The collected events contain not just the original event information + * but also the location context. */ -public class EventCollector extends AbstractEventHandler implements Iterable<Event> { - +public final class EventCollector extends AbstractEventHandler implements Iterable<Event> { private final Collection<Event> collected; /** @@ -66,20 +64,25 @@ public class EventCollector extends AbstractEventHandler implements Iterable<Eve * Implements {@link EventHandler#handle(Event)}. */ @Override - public void handle(Event event) { + public synchronized void handle(Event event) { if (getEventMask().contains(event.getKind())) { collected.add(event); } } /** - * Returns an iterator over the collected events. + * Returns an iterator over the collected events. This must not be called in a scenario where + * there may still be concurrent modifications to the collector. */ @Override public Iterator<Event> iterator() { return collected.iterator(); } + /** + * Returns an iterator over the collected events of the given kind. This must not be called in a + * scenario where there may still be concurrent modifications to the collector. + */ public Iterable<Event> filtered(final EventKind eventKind) { return Iterables.filter(collected, new Predicate<Event>() { @Override @@ -92,19 +95,19 @@ public class EventCollector extends AbstractEventHandler implements Iterable<Eve /** * Returns the number of events collected. */ - public int count() { + public synchronized int count() { return collected.size(); } /* * Clears the collected events */ - public void clear() { + public synchronized void clear() { collected.clear(); } @Override - public String toString() { + public synchronized String toString() { return "EventCollector: " + Iterables.toString(collected); } } |