Android Open Source - SnapetsAndroid Event Producer






From Project

Back to project page SnapetsAndroid.

License

The source code is released under:

Apache License

If you think the Android project SnapetsAndroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2012 Square, Inc./*  w w w . j  a va2s . c om*/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.squareup.otto;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Wraps a 'producer' method on a specific object.
 *
 * <p> This class only verifies the suitability of the method and event type if something fails.  Callers are expected
 * to verify their uses of this class.
 *
 * @author Jake Wharton
 */
class EventProducer {

  /** Object sporting the producer method. */
  private final Object target;
  /** Producer method. */
  private final Method method;
  /** Object hash code. */
  private final int hashCode;
  /** Should this producer produce events? */
  private boolean valid = true;

  EventProducer(Object target, Method method) {
    if (target == null) {
      throw new NullPointerException("EventProducer target cannot be null.");
    }
    if (method == null) {
      throw new NullPointerException("EventProducer method cannot be null.");
    }

    this.target = target;
    this.method = method;
    method.setAccessible(true);

    // Compute hash code eagerly since we know it will be used frequently and we cannot estimate the runtime of the
    // target's hashCode call.
    final int prime = 31;
    hashCode = (prime + method.hashCode()) * prime + target.hashCode();
  }

  public boolean isValid() {
    return valid;
  }

  /**
   * If invalidated, will subsequently refuse to produce events.
   *
   * Should be called when the wrapped object is unregistered from the Bus.
   */
  public void invalidate() {
    valid = false;
  }

  /**
   * Invokes the wrapped producer method.
   *
   * @throws java.lang.IllegalStateException  if previously invalidated.
   * @throws java.lang.reflect.InvocationTargetException  if the wrapped method throws any {@link Throwable} that is not
   *     an {@link Error} ({@code Error}s are propagated as-is).
   */
  public Object produceEvent() throws InvocationTargetException {
    if (!valid) {
      throw new IllegalStateException(toString() + " has been invalidated and can no longer produce events.");
    }
    try {
      return method.invoke(target);
    } catch (IllegalAccessException e) {
      throw new AssertionError(e);
    } catch (InvocationTargetException e) {
      if (e.getCause() instanceof Error) {
        throw (Error) e.getCause();
      }
      throw e;
    }
  }

  @Override public String toString() {
    return "[EventProducer " + method + "]";
  }

  @Override public int hashCode() {
    return hashCode;
  }

  @Override public boolean equals(Object obj) {
    if (this == obj) {
      return true;
    }

    if (obj == null) {
      return false;
    }

    if (getClass() != obj.getClass()) {
      return false;
    }

    final EventProducer other = (EventProducer) obj;

    return method.equals(other.method) && target == other.target;
  }

}




Java Source Code List

com.keyconsultant.parse.logintutorial.CreateAccountFragment.java
com.keyconsultant.parse.logintutorial.LoginActivity.java
com.keyconsultant.parse.logintutorial.LoginFragment.java
com.keyconsultant.parse.logintutorial.MainActivity.java
com.keyconsultant.parse.logintutorial.activity.BaseActivity.java
com.keyconsultant.parse.logintutorial.activity.StatusView.java
com.keyconsultant.parse.logintutorial.error.UnknownErrorDialogFactory.java
com.keyconsultant.parse.logintutorial.event.ErrorEvent.java
com.keyconsultant.parse.logintutorial.forgotpassword.ForgotPasswordDialogFragment.java
com.keyconsultant.parse.logintutorial.fragment.BaseFragment.java
com.keyconsultant.parse.logintutorial.model.manager.BaseManager.java
com.keyconsultant.parse.logintutorial.model.user.UserManager.java
com.keyconsultant.parse.logintutorial.model.user.User.java
com.keyconsultant.parse.logintutorial.model.user.authenticate.AuthenticateUserErrorEvent.java
com.keyconsultant.parse.logintutorial.model.user.authenticate.AuthenticateUserStartEvent.java
com.keyconsultant.parse.logintutorial.model.user.authenticate.AuthenticateUserSuccessEvent.java
com.keyconsultant.parse.logintutorial.model.user.authenticate.UserForgotPasswordErrorEvent.java
com.keyconsultant.parse.logintutorial.model.user.authenticate.UserForgotPasswordStartEvent.java
com.keyconsultant.parse.logintutorial.model.user.authenticate.UserForgotPasswordSuccessEvent.java
com.squareup.otto.AnnotatedHandlerFinder.java
com.squareup.otto.BusProvider.java
com.squareup.otto.Bus.java
com.squareup.otto.DeadEvent.java
com.squareup.otto.EventHandler.java
com.squareup.otto.EventProducer.java
com.squareup.otto.HandlerFinder.java
com.squareup.otto.Produce.java
com.squareup.otto.Subscribe.java
com.squareup.otto.ThreadEnforcer.java