/*
 * Copyright 2016-present Facebook, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package com.facebook.buck.event.listener;

import com.facebook.buck.event.BroadcastEvent;
import com.facebook.buck.event.BuckEventBus;

import java.util.HashSet;
import java.util.Set;

public class BroadcastEventListener {

  private Set<BuckEventBus> eventBuses;

  public BroadcastEventListener() {
    eventBuses = new HashSet<>();
  }

  public void broadcast(BroadcastEvent event) {
    if (eventBuses.isEmpty()) {
      throw new RuntimeException("No available eventBus to broadcast event: " +
          event.getEventName());
    }
    postToAllBuses(event);
  }

  public BroadcastEventBusClosable addEventBus(BuckEventBus eventBus) {
    eventBuses.add(eventBus);
    return new BroadcastEventBusClosable(this, eventBus);
  }

  private void postToAllBuses(BroadcastEvent event) {
    for (BuckEventBus eventBus : eventBuses) {
      if (event.isConfigured()) {
        eventBus.postWithoutConfiguring(event);
      } else {
        eventBus.post(event);
      }
    }
  }

  private void removeEventBus(BuckEventBus eventBus) {
    eventBuses.remove(eventBus);
  }

  public class BroadcastEventBusClosable implements AutoCloseable {
    BroadcastEventListener listener;
    BuckEventBus eventBus;

    public BroadcastEventBusClosable (
        BroadcastEventListener listener,
        BuckEventBus bus) {
      this.listener = listener;
      this.eventBus = bus;
    }

    public BuckEventBus getBuckEventBus() {
      return eventBus;
    }

    @Override
    public void close() {
      listener.removeEventBus(eventBus);
    }
  }
}

@@ -785,16 +785,122 @@
 l.Set;%0A%0A
+import javax.annotation.concurrent.GuardedBy;%0Aimport javax.annotation.concurrent.ThreadSafe;%0A%0A@ThreadSafe%0A
 public c
@@ -930,16 +930,37 @@
 ener %7B%0A%0A
+  @GuardedBy(%22this%22)%0A
   privat
@@ -1072,24 +1072,37 @@
 %7D%0A%0A  public 
+synchronized 
 void broadca
@@ -1287,457 +1287,397 @@
     
-postToAllBuses(event);%0A  %7D%0A%0A  public BroadcastEventBusClosable addEventBus(BuckEventBus eventBus) %7B%0A    eventBuses.add(eventBus);%0A    return new BroadcastEventBusClosable(this, eventBus);%0A  %7D%0A%0A  private void postToAllBuses(BroadcastEvent event) %7B%0A    for (BuckEventBus eventBus : eventBuses) %7B%0A      if (event.isConfigured()) %7B%0A        eventBus.postWithoutConfiguring(event);%0A      %7D else %7B%0A        eventBus.post(event);%0A      %7D%0A    %7D%0A  %7D%0A%0A  private
+for (BuckEventBus eventBus : eventBuses) %7B%0A      if (event.isConfigured()) %7B%0A        eventBus.postWithoutConfiguring(event);%0A      %7D else %7B%0A        eventBus.post(event);%0A      %7D%0A    %7D%0A  %7D%0A%0A  public synchronized BroadcastEventBusClosable addEventBus(BuckEventBus eventBus) %7B%0A    eventBuses.add(eventBus);%0A    return new BroadcastEventBusClosable(this, eventBus);%0A  %7D%0A%0A  private synchronized
  voi

Make BroadcastEventListener threadsafe