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

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

This example Akka source code file (UnitMatch.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

abstractmatch, akka, boxedunit, class, f, japi, matcherror, p, runtime, unitmatch, unitpfbuilder

The UnitMatch.java Akka example source code

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

package akka.japi.pf;

import scala.MatchError;
import scala.PartialFunction;
import scala.runtime.BoxedUnit;

/**
 * Version of {@link scala.PartialFunction} that can be built during
 * runtime from Java.
 * This is a specialized version of {@link UnitMatch} to map java
 * void methods to {@link scala.runtime.BoxedUnit}.
 *
 * @param <I> the input type, that this PartialFunction will be applied to
 *
 * This is an EXPERIMENTAL feature and is subject to change until it has received more real world testing.
 */
public class UnitMatch<I> extends AbstractMatch<I, BoxedUnit> {

  /**
   * Convenience function to create a {@link UnitPFBuilder} with the first
   * case statement added.
   *
   * @param type  a type to match the argument against
   * @param apply  an action to apply to the argument if the type matches
   * @return a builder with the case statement added
   * @see UnitPFBuilder#match(Class, FI.UnitApply)
   */
  public static final <F, P> UnitPFBuilder<F> match(final Class<P> type, FI.UnitApply<P> apply) {
    return new UnitPFBuilder<F>().match(type, apply);
  }

  /**
   * Convenience function to create a {@link UnitPFBuilder} with the first
   * case statement added.
   *
   * @param type  a type to match the argument against
   * @param predicate  a predicate that will be evaluated on the argument if the type matches
   * @param apply  an action to apply to the argument if the type and predicate matches
   * @return a builder with the case statement added
   * @see UnitPFBuilder#match(Class, FI.TypedPredicate, FI.UnitApply)
   */
  public static <F, P> UnitPFBuilder<F> match(final Class<P> type,
                                              final FI.TypedPredicate<P> predicate,
                                              final FI.UnitApply<P> apply) {
    return new UnitPFBuilder<F>().match(type, predicate, apply);
  }

  /**
   * Convenience function to create a {@link UnitPFBuilder} with the first
   * case statement added.
   *
   * @param object  the object to compare equals with
   * @param apply  an action to apply to the argument if the object compares equal
   * @return a builder with the case statement added
   * @see UnitPFBuilder#matchEquals(Object, FI.UnitApply)
   */
  public static <F, P> UnitPFBuilder<F> matchEquals(final P object,
                                                    final FI.UnitApply<P> apply) {
    return new UnitPFBuilder<F>().matchEquals(object, apply);
  }

  /**
   * Convenience function to create a {@link UnitPFBuilder} with the first
   * case statement added.
   *
   * @param object  the object to compare equals with
   * @param predicate  a predicate that will be evaluated on the argument the object compares equal
   * @param apply  an action to apply to the argument if the object compares equal
   * @return a builder with the case statement added
   * @see UnitPFBuilder#matchEquals(Object, FI.UnitApply)
   */
  public static <F, P> UnitPFBuilder<F> matchEquals(final P object,
                                                    final FI.TypedPredicate<P> predicate,
                                                    final FI.UnitApply<P> apply) {
    return new UnitPFBuilder<F>().matchEquals(object, predicate, apply);
  }

  /**
   * Convenience function to create a {@link UnitPFBuilder} with the first
   * case statement added.
   *
   * @param apply  an action to apply to the argument
   * @return a builder with the case statement added
   * @see UnitPFBuilder#matchAny(FI.UnitApply)
   */
  public static <F> UnitPFBuilder<F> matchAny(final FI.UnitApply<Object> apply) {
    return new UnitPFBuilder<F>().matchAny(apply);
  }

  /**
   * Create a {@link UnitMatch} from the builder.
   *
   * @param builder  a builder representing the partial function
   * @return a {@link UnitMatch} that can be reused
   */
  public static <F> UnitMatch<F> create(UnitPFBuilder<F> builder) {
    return new UnitMatch<F>(builder.build());
  }

  private UnitMatch(PartialFunction<I, BoxedUnit> statements) {
    super(statements);
  }

  /**
   * Convenience function to make the Java code more readable.
   *
   * <pre><code>
   *   UnitMatcher<X> matcher = UnitMatcher.create(...);
   *
   *   matcher.match(obj);
   * </code></pre>
   *
   * @param i  the argument to apply the match to
   * @throws scala.MatchError  if there is no match
   */
  public void match(I i) throws MatchError {
    statements.apply(i);
  }
}

Other Akka source code examples

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

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

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 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.