diff options
author | gjoranv <gjoranv@gmail.com> | 2023-10-24 11:31:21 +0200 |
---|---|---|
committer | gjoranv <gjoranv@gmail.com> | 2023-10-24 11:31:21 +0200 |
commit | 019d35078421a33e579992aca13dbe6f14ca67bf (patch) | |
tree | 552fe07416191476f017b5a55365525d96635622 /controller-api/src/main/java/com | |
parent | ce3f04a365d8d655fafd8fe43c5ffa6868db8cdd (diff) |
Add bill status logic.
Diffstat (limited to 'controller-api/src/main/java/com')
-rw-r--r-- | controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/billing/StatusHistory.java | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/billing/StatusHistory.java b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/billing/StatusHistory.java index 6335ada1396..f0c7f806c8c 100644 --- a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/billing/StatusHistory.java +++ b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/billing/StatusHistory.java @@ -14,6 +14,17 @@ public class StatusHistory { SortedMap<ZonedDateTime, BillStatus> history; public StatusHistory(SortedMap<ZonedDateTime, BillStatus> history) { + // Validate the given history + var iter = history.values().iterator(); + BillStatus next = iter.hasNext() ? iter.next() : null; + while (iter.hasNext()) { + var current = next; + next = iter.next(); + if (! validateStatus(current, next)) { + throw new IllegalArgumentException("Invalid transition from " + current + " to " + next); + } + } + this.history = history; } @@ -32,4 +43,19 @@ public class StatusHistory { return history; } + public void checkValidTransition(BillStatus newStatus) { + if (! validateStatus(current(), newStatus)) { + throw new IllegalArgumentException("Invalid transition from " + current() + " to " + newStatus); + } + } + + private static boolean validateStatus(BillStatus current, BillStatus newStatus) { + return switch(current) { + case OPEN -> true; + case FROZEN -> newStatus != BillStatus.OPEN; // This could be subject to change. + case CLOSED -> newStatus == BillStatus.CLOSED; + case VOID -> newStatus == BillStatus.VOID; + }; + } + } |