All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opentcs.util.event.SimpleEventBus Maven / Gradle / Ivy

There is a newer version: 6.4.0
Show newest version
/**
 * Copyright (c) The openTCS Authors.
 *
 * This program is free software and subject to the MIT license. (For details,
 * see the licensing information (LICENSE.txt) you should have received with
 * this copy of the software.)
 */
package org.opentcs.util.event;

import static java.util.Objects.requireNonNull;

import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A plain implementation of {@link EventBus}.
 */
public class SimpleEventBus
    implements
      EventBus {

  /**
   * This class's logger.
   */
  private static final Logger LOG = LoggerFactory.getLogger(SimpleEventBus.class);
  /**
   * The listeners.
   */
  private final Set listeners = new CopyOnWriteArraySet<>();

  /**
   * Creates a new instance.
   */
  public SimpleEventBus() {
  }

  @Override
  public void onEvent(Object event) {
    try {
      for (EventHandler listener : listeners) {
        listener.onEvent(event);
      }
    }
    catch (Exception exc) {
      LOG.warn("Exception thrown by event handler", exc);
    }
  }

  @Override
  public void subscribe(EventHandler listener) {
    requireNonNull(listener, "listener");

    listeners.add(listener);
  }

  @Override
  public void unsubscribe(EventHandler listener) {
    requireNonNull(listener, "listener");

    listeners.remove(listener);
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy