001/*
002 * Copyright (C) 2007 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.eventbus;
018
019import static com.google.common.base.Preconditions.checkNotNull;
020
021import com.google.common.annotations.Beta;
022import com.google.common.base.MoreObjects;
023
024/**
025 * Wraps an event that was posted, but which had no subscribers and thus could
026 * not be delivered.
027 *
028 * <p>Registering a DeadEvent subscriber is useful for debugging or logging, as
029 * it can detect misconfigurations in a system's event distribution.
030 *
031 * @author Cliff Biffle
032 * @since 10.0
033 */
034@Beta
035public class DeadEvent {
036
037  private final Object source;
038  private final Object event;
039
040  /**
041   * Creates a new DeadEvent.
042   *
043   * @param source  object broadcasting the DeadEvent (generally the
044   *                {@link EventBus}).
045   * @param event   the event that could not be delivered.
046   */
047  public DeadEvent(Object source, Object event) {
048    this.source = checkNotNull(source);
049    this.event = checkNotNull(event);
050  }
051
052  /**
053   * Returns the object that originated this event (<em>not</em> the object that
054   * originated the wrapped event).  This is generally an {@link EventBus}.
055   *
056   * @return the source of this event.
057   */
058  public Object getSource() {
059    return source;
060  }
061
062  /**
063   * Returns the wrapped, 'dead' event, which the system was unable to deliver
064   * to any registered subscriber.
065   *
066   * @return the 'dead' event that could not be delivered.
067   */
068  public Object getEvent() {
069    return event;
070  }
071
072  @Override
073  public String toString() {
074    return MoreObjects.toStringHelper(this)
075      .add("source", source)
076      .add("event", event)
077      .toString();
078  }
079}