All Downloads are FREE. Search and download functionalities are using the official Maven repository.

akka.japi.pf.FSMStopBuilder Maven / Gradle / Ivy

/**
 * Copyright (C) 2009-2014 Typesafe Inc. 
 */

package akka.japi.pf;

import akka.actor.FSM;
import scala.PartialFunction;
import scala.runtime.BoxedUnit;

/**
 * Builder used to create a partial function for {@link akka.actor.FSM#onTermination}.
 *
 * @param  the state type
 * @param  the data type
 *
 * This is an EXPERIMENTAL feature and is subject to change until it has received more real world testing.
 */
public class FSMStopBuilder {

  private UnitPFBuilder> builder =
    new UnitPFBuilder>();

  /**
   * Add a case statement that matches on an {@link FSM.Reason}.
   *
   * @param reason  the reason for the termination
   * @param apply  an action to apply to the event and state data if there is a match
   * @return the builder with the case statement added
   */
  public FSMStopBuilder stop(final FSM.Reason reason,
                                   final FI.UnitApply2 apply) {
    builder.match(FSM.StopEvent.class,
      new FI.TypedPredicate() {
        @Override
        public boolean defined(FSM.StopEvent e) {
          return reason.equals(e.reason());
        }
      },
      new FI.UnitApply() {
        public void apply(FSM.StopEvent e) throws Exception {
          @SuppressWarnings("unchecked")
          S s = (S) e.currentState();
          @SuppressWarnings("unchecked")
          D d = (D) e.stateData();
          apply.apply(s, d);
        }
      }
    );

    return this;
  }

  /**
   * Add a case statement that matches on a reason type.
   *
   * @param reasonType  the reason type to match on
   * @param apply  an action to apply to the reason, event and state data if there is a match
   * @param 

the reason type to match on * @return the builder with the case statement added */ public

FSMStopBuilder stop(final Class

reasonType, final FI.UnitApply3 apply) { return this.stop(reasonType, new FI.TypedPredicate

() { @Override public boolean defined(P p) { return true; } }, apply); } /** * Add a case statement that matches on a reason type and a predicate. * * @param reasonType the reason type to match on * @param apply an action to apply to the reason, event and state data if there is a match * @param predicate a predicate that will be evaluated on the reason if the type matches * @param

the reason type to match on * @return the builder with the case statement added */ public

FSMStopBuilder stop(final Class

reasonType, final FI.TypedPredicate

predicate, final FI.UnitApply3 apply) { builder.match(FSM.StopEvent.class, new FI.TypedPredicate() { @Override public boolean defined(FSM.StopEvent e) { if (reasonType.isInstance(e.reason())) { @SuppressWarnings("unchecked") P p = (P) e.reason(); return predicate.defined(p); } else { return false; } } }, new FI.UnitApply() { public void apply(FSM.StopEvent e) throws Exception { @SuppressWarnings("unchecked") P p = (P) e.reason(); @SuppressWarnings("unchecked") S s = (S) e.currentState(); @SuppressWarnings("unchecked") D d = (D) e.stateData(); apply.apply(p, s, d); } } ); return this; } /** * Build a {@link scala.PartialFunction} from this builder. * After this call the builder will be reset. * * @return a PartialFunction for this builder. */ public PartialFunction, BoxedUnit> build() { return builder.build(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy