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

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

/*
 * Copyright (C) 2009-2020 Lightbend Inc. 
 */

package akka.japi.pf;

import scala.MatchError;
import scala.PartialFunction;

/**
 * Version of {@link scala.PartialFunction} that can be built during runtime from Java.
 *
 * @param  the input type, that this PartialFunction will be applied to
 * @param  the return type, that the results of the application will have
 */
public class Match extends AbstractMatch {

  /**
   * Convenience function to create a {@link PFBuilder} 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 PFBuilder#match(Class, FI.Apply)
   */
  public static  PFBuilder match(final Class

type, final FI.Apply apply) { return new PFBuilder().match(type, apply); } /** * Convenience function to create a {@link PFBuilder} with the first case statement added without * compile time type check of the parameters. Should normally not be used, but when matching on * class with generic type argument it can be useful, e.g. List.class and * (List<String> list) -> {}. * * @see PFBuilder#matchUnchecked(Class, FI.Apply) */ public static PFBuilder matchUnchecked( final Class type, final FI.Apply apply) { return new PFBuilder().matchUnchecked(type, apply); } /** * Convenience function to create a {@link PFBuilder} 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 matches * @return a builder with the case statement added * @see PFBuilder#match(Class, FI.TypedPredicate, FI.Apply) */ public static PFBuilder match( final Class

type, final FI.TypedPredicate

predicate, final FI.Apply apply) { return new PFBuilder().match(type, predicate, apply); } /** * Convenience function to create a {@link PFBuilder} with the first case statement added without * compile time type check of the parameters. Should normally not be used, but when matching on * class with generic type argument it can be useful, e.g. List.class and * (List<String> list) -> {}. * * @see PFBuilder#matchUnchecked(Class, FI.TypedPredicate, FI.Apply) */ public static PFBuilder matchUnchecked( final Class type, final FI.TypedPredicate predicate, final FI.Apply apply) { return new PFBuilder().matchUnchecked(type, predicate, apply); } /** * Convenience function to create a {@link PFBuilder} 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 PFBuilder#matchEquals(Object, FI.Apply) */ public static PFBuilder matchEquals(final P object, final FI.Apply apply) { return new PFBuilder().matchEquals(object, apply); } /** * Convenience function to create a {@link PFBuilder} with the first case statement added. * * @param apply an action to apply to the argument * @return a builder with the case statement added * @see PFBuilder#matchAny(FI.Apply) */ public static PFBuilder matchAny(final FI.Apply apply) { return new PFBuilder().matchAny(apply); } /** * Create a {@link Match} from the builder. * * @param builder a builder representing the partial function * @return a {@link Match} that can be reused */ public static final Match create(PFBuilder builder) { return new Match(builder.build()); } Match(PartialFunction statements) { super(statements); } /** * Convenience function to make the Java code more readable. * *

* *


   *   Match<X, Y> matcher = Match.create(...);
   *
   *   Y someY = matcher.match(obj);
   * 
* * @param i the argument to apply the match to * @return the result of the application * @throws MatchError if there is no match */ public R match(I i) throws MatchError { return statements.apply(i); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy