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 */ 030public class DeadEvent { 031 032 private final Object source; 033 private final Object event; 034 035 /** 036 * Creates a new DeadEvent. 037 * 038 * @param source object broadcasting the DeadEvent (generally the {@link EventBus}). 039 * @param event the event that could not be delivered. 040 */ 041 public DeadEvent(Object source, Object event) { 042 this.source = checkNotNull(source); 043 this.event = checkNotNull(event); 044 } 045 046 /** 047 * Returns the object that originated this event (<em>not</em> the object that originated the 048 * wrapped event). This is generally an {@link EventBus}. 049 * 050 * @return the source of this event. 051 */ 052 public Object getSource() { 053 return source; 054 } 055 056 /** 057 * Returns the wrapped, 'dead' event, which the system was unable to deliver to any registered 058 * subscriber. 059 * 060 * @return the 'dead' event that could not be delivered. 061 */ 062 public Object getEvent() { 063 return event; 064 } 065 066 @Override 067 public String toString() { 068 return MoreObjects.toStringHelper(this).add("source", source).add("event", event).toString(); 069 } 070}