alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Akka/Scala example source code file (FSMTransitionHandlerBuilder.java)

This example Akka source code file (FSMTransitionHandlerBuilder.java) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Akka and Scala source code examples by using tags.

All credit for the original source code belongs to akka.io; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Akka tags/keywords

akka, boxedunit, exception, fsmtransitionhandlerbuilder, japi, override, partialfunction, runtime, s, suppresswarnings, unitpfbuilder

The FSMTransitionHandlerBuilder.java Akka example source code

/**
 * Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
 */

package akka.japi.pf;

import scala.PartialFunction;
import scala.runtime.BoxedUnit;
import scala.Tuple2;

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

  private UnitPFBuilder<Tuple2<S, S>> builder =
    new UnitPFBuilder<Tuple2<S, S>>();

  /**
   * Add a case statement that matches on a from state and a to state.
   *
   * @param fromState  the from state to match on, or null for any
   * @param toState  the to state to match on, or null for any
   * @param apply  an action to apply when the states match
   * @return the builder with the case statement added
   */
  public FSMTransitionHandlerBuilder<S> state(final S fromState,
                                              final S toState,
                                              final FI.UnitApplyVoid apply) {
    builder.match(Tuple2.class,
      new FI.TypedPredicate<Tuple2>() {
        @Override
        public boolean defined(Tuple2 t) {
          return (fromState == null || fromState.equals(t._1()))
            && (toState == null || toState.equals(t._2()));
        }
      },
      new FI.UnitApply<Tuple2>() {
        @Override
        public void apply(Tuple2 t) throws Exception {
          apply.apply();
        }
      }
    );
    return this;
  }

  /**
   * Add a case statement that matches on a from state and a to state.
   *
   * @param fromState  the from state to match on, or null for any
   * @param toState  the to state to match on, or null for any
   * @param apply  an action to apply when the states match
   * @return the builder with the case statement added
   */
  public FSMTransitionHandlerBuilder<S> state(final S fromState,
                                              final S toState,
                                              final FI.UnitApply2<S, S> apply) {
    builder.match(Tuple2.class,
      new FI.TypedPredicate<Tuple2>() {
        @Override
        public boolean defined(Tuple2 t) {
          return (fromState == null || fromState.equals(t._1()))
            && (toState == null || toState.equals(t._2()));
        }
      },
      new FI.UnitApply<Tuple2>() {
        @Override
        public void apply(Tuple2 t) throws Exception {
          @SuppressWarnings("unchecked")
          S sf = (S) t._1();
          @SuppressWarnings("unchecked")
          S st = (S) t._2();
          apply.apply(sf, st);
        }
      }
    );
    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<Tuple2<S, S>, BoxedUnit> build() {
    return builder.build();
  }
}

Other Akka source code examples

Here is a short list of links related to this Akka FSMTransitionHandlerBuilder.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.