001/*
002 * Copyright (C) 2013 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005 * use this file except in compliance with the License. You may obtain a copy of
006 * 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, WITHOUT
012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013 * License for the specific language governing permissions and limitations under
014 * the License.
015 */
016package com.google.common.eventbus;
017
018import static com.google.common.base.Preconditions.checkNotNull;
019
020import java.lang.reflect.Method;
021
022/**
023 * Context for an exception thrown by a subscriber.
024 *
025 * @since 16.0
026 */
027public class SubscriberExceptionContext {
028  private final EventBus eventBus;
029  private final Object event;
030  private final Object subscriber;
031  private final Method subscriberMethod;
032
033  /**
034   * @param eventBus The {@link EventBus} that handled the event and the
035   *     subscriber. Useful for broadcasting a a new event based on the error.
036   * @param event The event object that caused the subscriber to throw.
037   * @param subscriber The source subscriber context.
038   * @param subscriberMethod the subscribed method.
039   */
040  SubscriberExceptionContext(EventBus eventBus, Object event, Object subscriber,
041      Method subscriberMethod) {
042    this.eventBus = checkNotNull(eventBus);
043    this.event = checkNotNull(event);
044    this.subscriber = checkNotNull(subscriber);
045    this.subscriberMethod = checkNotNull(subscriberMethod);
046  }
047
048  /**
049   * @return The {@link EventBus} that handled the event and the subscriber.
050   *     Useful for broadcasting a a new event based on the error.
051   */
052  public EventBus getEventBus() {
053    return eventBus;
054  }
055
056  /**
057   * @return The event object that caused the subscriber to throw.
058   */
059  public Object getEvent() {
060    return event;
061  }
062
063  /**
064   * @return The object context that the subscriber was called on.
065   */
066  public Object getSubscriber() {
067    return subscriber;
068  }
069
070  /**
071   * @return The subscribed method that threw the exception.
072   */
073  public Method getSubscriberMethod() {
074    return subscriberMethod;
075  }
076}