The Itinera bookJava edition

Part 2: Getting started, Chapter 20

An optional input

What you want to do

Some orders come with a discount, and most do not, so you want the shop to charge the price less the discount, or the full price when there is none. Working out what an order costs and charging a card are two rules of the business, so you want each in a step of its own: the charge should charge an amount, and know nothing of prices or discounts.

The code

The pricing is a new step. It asks for the order's price, and for the discount as an Optional<Long>, an optional input. It gives back the price less the discount, or the full price when the discount is empty, under amount.

Java, from the program ch20_an_optional_input
/**
 * Works out what an order costs: its price, less its discount if it has one, given back as the
 * amount to charge.
 */
@Step
final class Price implements ItineraStep {
  private final long price;
  private final Optional<Long> discount;

  Price(@Input long price, @Input Optional<Long> discount) {
    this.price = price;
    this.discount = discount;
  }

  public Outcome run(Contributor contributor) {
    contributor.contribute("amount", price - discount.orElse(0L));
    return succeed();
  }
}

The charge now asks for that amount, and charges it. There is no price or discount in it.

Java, from the program ch20_an_optional_input
/**
 * Charges an order's amount to its card through the payment client, and gives back its receipt, and
 * the gift card's code when the order is a gift card.
 */
@Step
final class Charge implements ItineraStep {
  private final String order;
  private final long amount;
  private final String card;
  private final boolean giftCard;

  Charge(@Input String order, @Input long amount, @Input String card, @Input boolean giftCard) {
    this.order = order;
    this.amount = amount;
    this.card = card;
    this.giftCard = giftCard;
  }

  public Outcome run(@Service Payments payments, Contributor contributor) throws NetworkDown {
    if (amount <= 0) return fail("nothing-to-charge");
    Optional<String> receipt = payments.charge(order, card, amount);
    if (receipt.isEmpty()) return fail("card-expired");
    contributor.contribute("receipt", receipt.orElseThrow());
    if (giftCard) contributor.contribute("code", "G-" + order);
    return succeed();
  }
}

The payment client it asks for is chapter 14's Payments.

The workflow lists the pricing first, under the name price, so the charge reads the amount it gave back:

Java, from the program ch20_an_optional_input
/**
 * The shop's workflow: it works out what the order costs, then charges it, repairing a charge that
 * fails and auditing one that succeeds, then ships, and shipping may be tried once more. Once a
 * journey has ended, the customer is told, through the channel the shop configures.
 */
@Workflow(
    name = "orders",
    reporters = {Log.class, Desk.class},
    policies = {Notify.class})
@WithStep(step = Price.class, name = "price")
@WithStep(
    step = Charge.class,
    name = "charge",
    rescue = RenewCard.class,
    policies = {Audit.class, Outage.class})
@WithStep(
    step = Ship.class,
    name = "ship",
    retryBudget = 1,
    policies = {HeavyParcels.class})
final class Orders implements ItineraWorkflow, Messages {
  private final String channel;

  Orders(String channel) {
    this.channel = channel;
  }

  @Override
  public void tell(String customer, String message) {
    System.out.println(channel + " to " + customer + ": " + message);
  }

  /** The bank, as the payment client of the journey. */
  @Provider(key = "payments")
  public Provided<Payments> bank() {
    return provide(new Bank(2026));
  }

  /** A carrier for every attempt of a step: the main carrier first, the backup carrier after. */
  @Provider(perAttempt = true, key = "carrier")
  public Provided<Carrier> carriers(Attempt attempt) {
    return attempt.isFirst() ? provide(new MainCarrier()) : provide(new BackupCarrier());
  }

  /** Names each journey after its order. */
  @IdGenerator
  public String orderNumber(DataBag data) {
    String order = data.read("order", TypeToken.of(String.class)).orNull();
    if (order == null) throw new IllegalStateException("an order without a number");
    return order;
  }
}

The other steps, policies and reporters it names are as earlier chapters show them: RenewCard in chapter 14, the shipping, Outage and HeavyParcels in chapter 17, the log in chapter 18, with the parts that page points to, Notify and the role Messages in chapter 19 and the desk in chapter 11. The bank's provider is chapter 12's, and the carriers' provider chapter 13's, with chapter 12's Carrier.

The audit of chapter 15 wrote down the order's price, which is no longer what was charged. It now asks for the amount, from the data bag:

Java, from the program ch20_an_optional_input
/**
 * Writes an audit line for every step it is attached to that succeeds: the amount the order was
 * charged, and the receipt the step gave back.
 */
@StepPolicy(name = "audit")
final class Audit implements ItineraStepPolicy {
  @OnStepSuccess
  public OnSuccess charged(
      StepName step, @FromWorkflow long amount, @FromStep String receipt, Contributor contributor) {
    contributor.contribute("audit", step + " took " + amount + ", receipt " + receipt);
    return keepDefault();
  }
}

The initial data has a discount, as an Optional<Long> too, which the record's shorter constructors leave empty. Ann's order comes with a discount of 10, and the others with none.

Java, from the program ch20_an_optional_input
/** What the shop knows about an order when its journey starts. */
record NewOrder(
    String order,
    String customer,
    long price,
    Optional<Long> discount,
    long weight,
    String card,
    boolean giftCard) {
  /** An order without a discount. */
  NewOrder(String order, String customer, long price, long weight, String card, boolean giftCard) {
    this(order, customer, price, Optional.empty(), weight, card, giftCard);
  }

  /** An order without a discount that isn't a gift card. */
  NewOrder(String order, String customer, long price, long weight, String card) {
    this(order, customer, price, weight, card, false);
  }
}

It runs the same five orders as chapter 19. It prints:

What ch20_an_optional_input prints, recorded in ch20_an_optional_input.txt
Ann's order:
log orders A-1 #1 journey_started order, customer, price, discount, weight, card, giftCard
log orders A-1 #2 attempt_started price, attempt 1
log orders A-1 #3 step_succeeded price
log orders A-1 #4 contribution_committed amount
log orders A-1 #5 attempt_started charge, attempt 1
log orders A-1 #6 service_provided charge gets payments from bank, a ch20_an_optional_input.Bank
the bank charges 30 for order A-1
log orders A-1 #7 step_succeeded charge
log orders A-1 #8 contribution_committed receipt
log orders A-1 #9 hook_called audit: on step success of charge
log orders A-1 #10 contribution_committed audit
log orders A-1 #11 attempt_started ship, attempt 1
log orders A-1 #12 service_provided ship gets carrier from carriers, a ch20_an_optional_input.MainCarrier
the main carrier books order A-1
log orders A-1 #13 step_info ship: the parcel leaves with receipt R-A-1
log orders A-1 #14 step_succeeded ship
log orders A-1 #15 contribution_committed tracking
log orders A-1 #16 optional_input_absent code
email to Ann: Order A-1 is on its way, tracking T-A-1.
log orders A-1 #17 hook_called notify: on workflow success
log orders A-1 #18 journey_succeeded
the journey succeeded, with receipt R-A-1 and tracking T-A-1
audit: charge took 30, receipt R-A-1
Gil's order:
log orders A-7 #1 journey_started order, customer, price, weight, card, giftCard
log orders A-7 #2 attempt_started price, attempt 1
log orders A-7 #3 optional_input_absent discount
log orders A-7 #4 step_succeeded price
log orders A-7 #5 contribution_committed amount
log orders A-7 #6 attempt_started charge, attempt 1
log orders A-7 #7 service_provided charge gets payments from bank, a ch20_an_optional_input.Bank
the bank charges 45 for order A-7
log orders A-7 #8 step_succeeded charge
log orders A-7 #9 contribution_committed receipt
log orders A-7 #10 hook_called audit: on step success of charge
log orders A-7 #11 contribution_committed audit
log orders A-7 #12 attempt_started ship, attempt 1
log orders A-7 #13 service_provided ship gets carrier from carriers, a ch20_an_optional_input.MainCarrier
the main carrier books order A-7
log orders A-7 #14 step_failed ship: too-heavy
log orders A-7 #15 hook_called heavyParcels: on step retry of ship
log orders A-7 #16 step_retrying ship
log orders A-7 #17 attempt_started ship, attempt 2
log orders A-7 #18 service_provided ship gets carrier from carriers, a ch20_an_optional_input.BackupCarrier
the backup carrier books order A-7
log orders A-7 #19 step_info ship: the parcel leaves with receipt R-A-7
log orders A-7 #20 step_succeeded ship
log orders A-7 #21 contribution_committed tracking
log orders A-7 #22 optional_input_absent code
email to Gil: Order A-7 is on its way, tracking B-A-7.
log orders A-7 #23 hook_called notify: on workflow success
log orders A-7 #24 journey_succeeded
the journey succeeded, with receipt R-A-7 and tracking B-A-7
audit: charge took 45, receipt R-A-7
Jo's order:
log orders A-8 #1 journey_started order, customer, price, weight, card, giftCard
log orders A-8 #2 attempt_started price, attempt 1
log orders A-8 #3 optional_input_absent discount
log orders A-8 #4 step_succeeded price
log orders A-8 #5 contribution_committed amount
log orders A-8 #6 attempt_started charge, attempt 1
log orders A-8 #7 service_provided charge gets payments from bank, a ch20_an_optional_input.Bank
the bank declines card jo-2024: it has expired
log orders A-8 #8 step_failed charge: card-expired
log orders A-8 #9 step_rescuing charge: failure, rescued by charge_rescue
log orders A-8 #10 attempt_started charge_rescue, attempt 1
the bank says card jo-2024 was replaced by jo-2029
log orders A-8 #11 step_succeeded charge_rescue
log orders A-8 #12 contribution_committed card
log orders A-8 #13 data_overwritten card
log orders A-8 #14 step_rescued charge by charge_rescue
log orders A-8 #15 attempt_started charge, attempt 2
the bank charges 30 for order A-8
log orders A-8 #16 step_succeeded charge
log orders A-8 #17 contribution_committed receipt
log orders A-8 #18 hook_called audit: on step success of charge
log orders A-8 #19 contribution_committed audit
log orders A-8 #20 attempt_started ship, attempt 1
log orders A-8 #21 service_provided ship gets carrier from carriers, a ch20_an_optional_input.MainCarrier
the main carrier books order A-8
log orders A-8 #22 step_info ship: the parcel leaves with receipt R-A-8
log orders A-8 #23 step_succeeded ship
log orders A-8 #24 contribution_committed tracking
log orders A-8 #25 optional_input_absent code
email to Jo: Order A-8 is on its way, tracking T-A-8.
log orders A-8 #26 hook_called notify: on workflow success
log orders A-8 #27 journey_succeeded
the journey succeeded, with receipt R-A-8 and tracking T-A-8
audit: charge took 30, receipt R-A-8
Kim's order:
log orders A-9 #1 journey_started order, customer, price, weight, card, giftCard
log orders A-9 #2 attempt_started price, attempt 1
log orders A-9 #3 optional_input_absent discount
log orders A-9 #4 step_succeeded price
log orders A-9 #5 contribution_committed amount
log orders A-9 #6 attempt_started charge, attempt 1
log orders A-9 #7 service_provided charge gets payments from bank, a ch20_an_optional_input.Bank
the bank charges 25 for order A-9
log orders A-9 #8 step_succeeded charge
log orders A-9 #9 contribution_committed receipt
log orders A-9 #10 contribution_committed code
log orders A-9 #11 hook_called audit: on step success of charge
log orders A-9 #12 contribution_committed audit
log orders A-9 #13 attempt_started ship, attempt 1
log orders A-9 #14 service_provided ship gets carrier from carriers, a ch20_an_optional_input.MainCarrier
log orders A-9 #15 step_skipped ship: nothing-to-ship
log orders A-9 #16 contributions_discarded ship
log orders A-9 #17 optional_input_absent tracking
email to Kim: Order A-9 is paid: your gift card's code is G-A-9.
log orders A-9 #18 hook_called notify: on workflow success
log orders A-9 #19 journey_succeeded
the journey succeeded, with receipt R-A-9 and tracking none
audit: charge took 25, receipt R-A-9
Lee's order:
log orders A-10 #1 journey_started order, customer, price, weight, card, giftCard
log orders A-10 #2 attempt_started price, attempt 1
log orders A-10 #3 optional_input_absent discount
log orders A-10 #4 step_succeeded price
log orders A-10 #5 contribution_committed amount
log orders A-10 #6 attempt_started charge, attempt 1
log orders A-10 #7 service_provided charge gets payments from bank, a ch20_an_optional_input.Bank
the bank declines card lee-2019: it has expired
log orders A-10 #8 step_failed charge: card-expired
log orders A-10 #9 step_rescuing charge: failure, rescued by charge_rescue
log orders A-10 #10 attempt_started charge_rescue, attempt 1
the bank says nothing replaced card lee-2019
log orders A-10 #11 step_failed charge_rescue: not-replaced
log orders A-10 #12 step_given_up charge_rescue: failure
email to Lee: Sorry, order A-10 could not be completed.
log orders A-10 #13 hook_called notify: on workflow failure
log orders A-10 #14 journey_failed charge_rescue
desk: order A-10 failed at charge_rescue
the journey failed, with receipt none and tracking none
audit: none

How it works

  1. An input's type says whether it is required. An input of any type but Optional is required, as every input so far has been. An input of type Optional, such as Optional<Long>, is optional, as a hook's request is in chapter 18: the step is built with the value when the data bag holds one, and with an empty Optional when it holds none.
  2. Initial data leaves out what an order doesn't have. A component of the initial data that is an empty Optional adds no key to the data bag. Ann's journey starts with discount among its keys; Gil's, Jo's, Kim's and Lee's don't.
  3. An absent optional input is recorded. When the pricing is built for an order without a discount, the executor records optional_input_absent, naming discount, as it does for a hook's request in chapter 18. The pricing has one attempt, so each journey without a discount records it once.
  4. The step decides what the absence means. For the pricing, no discount means the full price, so it uses discount.orElse(0L). The executor only says the value isn't there; it is the step's business to know what to do without it. A step that can work without a value asks for it as optional; one that can't asks for it as required.
  5. Each rule of the business is a step. The bank charged Ann 30, the price less her discount, but the charge never saw either: it charged the amount the pricing contributed, read from the data bag like any input. A shop that priced its orders another way, with a voucher or a price per kilogram, would change the pricing, and use the same charge. A step holds one rule of the business; two rules are two steps, and the workflow puts them in order.
  6. The audit writes down what was charged. It reads amount with @FromWorkflow, since the pricing contributed it and the charge didn't, so it writes down what was charged, whatever it was worked out from.

The warehouse is changing how it describes parcels, though. From now on, each order comes with its parcel as one value, holding its weight and its size, and the shipping, which asks only for a weight, can't read that.

Outcomes

We have shown how a step asks for a value it can do without, and how the pricing became a step of its own.

The usual way With Itinera
An optional value is often passed as a default, such as a discount of 0, so the code cannot tell "no discount" from "a discount of nothing". An optional input is empty when the data bag has no value, and the executor records that it was absent, by key.
The code that charges a card often works out the amount too, so a new way of pricing changes the charging. The pricing and the charge are two steps, and the charge only charges the amount it is given.