View Javadoc
1   /*
2    * Copyright (C) 2007 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  
15  package com.google.common.eventbus;
16  
17  import static com.google.common.base.Preconditions.checkNotNull;
18  
19  import com.google.common.annotations.Beta;
20  import com.google.common.base.MoreObjects;
21  
22  /**
23   * Wraps an event that was posted, but which had no subscribers and thus could not be delivered.
24   *
25   * <p>Registering a DeadEvent subscriber is useful for debugging or logging, as it can detect
26   * misconfigurations in a system's event distribution.
27   *
28   * @author Cliff Biffle
29   * @since 10.0
30   */
31  @Beta
32  public class DeadEvent {
33  
34    private final Object source;
35    private final Object event;
36  
37    /**
38     * Creates a new DeadEvent.
39     *
40     * @param source object broadcasting the DeadEvent (generally the {@link EventBus}).
41     * @param event the event that could not be delivered.
42     */
43    public DeadEvent(Object source, Object event) {
44      this.source = checkNotNull(source);
45      this.event = checkNotNull(event);
46    }
47  
48    /**
49     * Returns the object that originated this event (<em>not</em> the object that originated the
50     * wrapped event). This is generally an {@link EventBus}.
51     *
52     * @return the source of this event.
53     */
54    public Object getSource() {
55      return source;
56    }
57  
58    /**
59     * Returns the wrapped, 'dead' event, which the system was unable to deliver to any registered
60     * subscriber.
61     *
62     * @return the 'dead' event that could not be delivered.
63     */
64    public Object getEvent() {
65      return event;
66    }
67  
68    @Override
69    public String toString() {
70      return MoreObjects.toStringHelper(this)
71        .add("source", source)
72        .add("event", event)
73        .toString();
74    }
75  }