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