/*
 * 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<BroadcastEventBusClosable> eventBusesClosable;

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

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

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

  private void postToAllBuses(BroadcastEvent event) {
    for (BroadcastEventBusClosable eventBusClosable : eventBusesClosable) {
      if (event.isConfigured()) {
        eventBusClosable.getBuckEventBus().postWithoutConfiguring(event);
      } else {
        eventBusClosable.getBuckEventBus().post(event);
      }
    }
  }

  private void removeEventBus(BuckEventBus eventBus) {
    for (BroadcastEventBusClosable eventBusClosable : eventBusesClosable) {
      if (eventBusClosable.getBuckEventBus().equals(eventBus)) {
        eventBusesClosable.remove(eventBusClosable);
        return;
      }
    }
  }

  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);
    }
  }
}

@@ -843,32 +843,19 @@
 et%3CB
-roadcastEventBusClosable
+uckEventBus
 %3E ev
@@ -862,24 +862,16 @@
 entBuses
-Closable
 ;%0A%0A  pub
@@ -915,24 +915,16 @@
 entBuses
-Closable
  = new H
@@ -1006,24 +1006,16 @@
 entBuses
-Closable
 .isEmpty
@@ -1251,52 +1251,44 @@
     
-BroadcastEventBusClosable eventBusClosable =
+eventBuses.add(eventBus);%0A    return
  new
@@ -1335,83 +1335,8 @@
 s);%0A
-    eventBusesClosable.add(eventBusClosable);%0A    return eventBusClosable;%0A
   %7D%0A
@@ -1404,32 +1404,19 @@
 r (B
-roadcastEventBusClosable
+uckEventBus
  eve
@@ -1416,32 +1416,24 @@
 Bus eventBus
-Closable
  : eventBuse
@@ -1429,32 +1429,24 @@
 : eventBuses
-Closable
 ) %7B%0A      if
@@ -1491,34 +1491,8 @@
 tBus
-Closable.getBuckEventBus()
 .pos
@@ -1554,34 +1554,8 @@
 tBus
-Closable.getBuckEventBus()
 .pos
@@ -1646,227 +1646,36 @@
     
-for (BroadcastEventBusClosable eventBusClosable : eventBusesClosable) %7B%0A      if (eventBusClosable.getBuckEventBus().equals(eventBus)) %7B%0A        eventBusesClosable.remove(eventBusClosable);%0A        return;%0A      %7D%0A    %7D
+eventBuses.remove(eventBus);
 %0A  %7D

Store BuckEventBus not BroadcastEventBusClosable