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

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

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

package akka.japi.pf;

/**
 * A builder for {@link scala.PartialFunction}.
 *
 * @param  the input type, that this PartialFunction will be applied to
 * @param  the return type, that the results of the application will have
 */
public final class PFBuilder extends AbstractPFBuilder {

  /** Create a PFBuilder. */
  public PFBuilder() {}

  /**
   * Add a new case statement to this builder.
   *
   * @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
   */
  public 

PFBuilder match(final Class

type, FI.Apply apply) { return matchUnchecked(type, apply); } /** * Add a new case statement to this builder 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) -> {}. * * @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 */ @SuppressWarnings("unchecked") public PFBuilder matchUnchecked(final Class type, FI.Apply apply) { FI.Predicate predicate = new FI.Predicate() { @Override public boolean defined(Object o) { return type.isInstance(o); } }; addStatement(new CaseStatement(predicate, (FI.Apply) apply)); return this; } /** * Add a new case statement to this builder. * * @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 and the predicate returns * true * @return a builder with the case statement added */ public

PFBuilder match( final Class

type, final FI.TypedPredicate

predicate, final FI.Apply apply) { return matchUnchecked(type, predicate, apply); } /** * Add a new case statement to this builder 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) -> {}. * * @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 and the predicate returns * true * @return a builder with the case statement added */ @SuppressWarnings("unchecked") public PFBuilder matchUnchecked( final Class type, final FI.TypedPredicate predicate, final FI.Apply apply) { FI.Predicate fiPredicate = new FI.Predicate() { @Override public boolean defined(Object o) { if (!type.isInstance(o)) return false; else return ((FI.TypedPredicate) predicate).defined(o); } }; addStatement(new CaseStatement(fiPredicate, (FI.Apply) apply)); return this; } /** * Add a new case statement to this builder. * * @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 */ public

PFBuilder matchEquals(final P object, final FI.Apply apply) { addStatement( new CaseStatement( new FI.Predicate() { @Override public boolean defined(Object o) { return object.equals(o); } }, apply)); return this; } /** * Add a new case statement to this builder, that matches any argument. * * @param apply an action to apply to the argument * @return a builder with the case statement added */ public PFBuilder matchAny(final FI.Apply apply) { addStatement( new CaseStatement( new FI.Predicate() { @Override public boolean defined(Object o) { return true; } }, apply)); return this; } }