001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.eventbus;
003
004import java.util.logging.Level;
005
006import org.openstreetmap.josm.eventbus.EventBus.DirectExecutor;
007import org.openstreetmap.josm.tools.Logging;
008
009/**
010 * The unique JOSM event bus.
011 */
012public class JosmEventBus {
013
014    private static final EventBus BUS = new EventBus("JOSM", DirectExecutor.INSTANCE, Dispatcher.perThreadDispatchQueue(),
015            (exception, context) -> Logging.logWithStackTrace(Level.SEVERE, exception, "Event bus error in {0}:", context));
016
017    private JosmEventBus() {
018        // Hide default constructor
019    }
020
021    /**
022     * Returns the unique JOSM event bus.
023     * @return the unique JOSM event bus
024     */
025    public static EventBus getBus() {
026        return BUS;
027    }
028    
029    /**
030     * Registers all subscriber methods on {@code object} to receive events.
031     *
032     * @param object object whose subscriber methods should be registered.
033     * @see EventBus#register
034     */
035    public static void register(Object object) {
036        BUS.register(object);
037    }
038
039    /**
040     * Unregisters all subscriber methods on a registered {@code object}.
041     *
042     * @param object object whose subscriber methods should be unregistered.
043     * @throws IllegalArgumentException if the object was not previously registered.
044     * @see EventBus#unregister
045     */
046    public static void unregister(Object object) {
047        BUS.unregister(object);
048    }
049
050    /**
051     * Posts an event to all registered subscribers. This method will return successfully after the
052     * event has been posted to all subscribers, and regardless of any exceptions thrown by
053     * subscribers.
054     *
055     * <p>If no subscribers have been subscribed for {@code event}'s class, and {@code event} is not
056     * already a {@link DeadEvent}, it will be wrapped in a DeadEvent and reposted.
057     *
058     * @param event event to post.
059     * @see EventBus#post
060     */
061    public static void post(Object event) {
062        BUS.post(event);
063    }
064}