Repository navigation
Expand file tree
/
Copy pathOrderDomain.java
More file actions
61 lines (44 loc) · 2.37 KB
/
Copy pathOrderDomain.java
File metadata and controls
61 lines (44 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/***************************************************************************************************
* Copyright (c) 2023, Salesforce, Inc. All rights reserved. SPDX-License-Identifier:
* Apache License Version 2.0
* For full license text, see the LICENSE file in the repo root or
* http://www.apache.org/licenses/LICENSE-2.0
**************************************************************************************************/
package com.salesforce.hydra;
/**
* Domain types for the example Order machine.
*
* <p>A Mealy machine has three independent "alphabets", and Hydra gives each its own type parameter
* — {@code Hydra<StateT, EventT, ActionT>}:
*
* <ul>
* <li>{@link OrderState} — where the order <em>is</em> (a position; may carry context).
* <li>{@link OrderEvent} — an <em>input</em>: something that happened <em>to</em> the order (a
* cause; often a bare signal).
* <li>{@link OrderAction} — an <em>output</em>: a command the machine <em>emits</em> on a
* transition (an effect; carries the data the doer needs).
* </ul>
*
* <p>Events and Actions are deliberately <em>different</em> types: you push Events in and receive
* Actions out, and the compiler enforces that one-way arrow.
*/
final class OrderDomain {
private OrderDomain() {}
// ── State: where the order is. Carries the context that later steps need. ──
sealed interface OrderState {}
record Cart() implements OrderState {}
record Placed(long amountCents, String address) implements OrderState {}
record Shipped(long amountCents, String address) implements OrderState {}
record Cancelled() implements OrderState {}
// ── Event: INPUT — something that happened TO the order. May be bare or carry data. ──
sealed interface OrderEvent {}
record Checkout(long amountCents, String address) implements OrderEvent {}
record PaymentSucceeded() implements OrderEvent {} // a bare signal — no data
record PaymentFailed(String reason) implements OrderEvent {}
record Cancel() implements OrderEvent {} // also bare
// ── Action: OUTPUT — a command the machine EMITS on a transition. Always carries data. ──
sealed interface OrderAction {}
record ChargeCard(long amountCents) implements OrderAction {}
record ShipParcel(String address) implements OrderAction {}
record RefundCard(long amountCents) implements OrderAction {}
}