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.base.MoreObjects;
020
021/**
022 * Wraps an event that was posted, but which had no subscribers and thus could not be delivered.
023 *
024 * <p>Registering a DeadEvent subscriber is useful for debugging or logging, as it can detect
025 * misconfigurations in a system's event distribution.
026 *
027 * @author Cliff Biffle
028 * @since 10.0
029 */
030@ElementTypesAreNonnullByDefault
031public class DeadEvent {
032
033  private final Object source;
034  private final Object event;
035
036  /**
037   * Creates a new DeadEvent.
038   *
039   * @param source object broadcasting the DeadEvent (generally the {@link EventBus}).
040   * @param event the event that could not be delivered.
041   */
042  public DeadEvent(Object source, Object event) {
043    this.source = checkNotNull(source);
044    this.event = checkNotNull(event);
045  }
046
047  /**
048   * Returns the object that originated this event (<em>not</em> the object that originated the
049   * wrapped event). This is generally an {@link EventBus}.
050   *
051   * @return the source of this event.
052   */
053  public Object getSource() {
054    return source;
055  }
056
057  /**
058   * Returns the wrapped, 'dead' event, which the system was unable to deliver to any registered
059   * subscriber.
060   *
061   * @return the 'dead' event that could not be delivered.
062   */
063  public Object getEvent() {
064    return event;
065  }
066
067  @Override
068  public String toString() {
069    return MoreObjects.toStringHelper(this).add("source", source).add("event", event).toString();
070  }
071}