aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/enums/MessageStatus.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/de/thedevstack/conversationsplus/enums/MessageStatus.java')
-rw-r--r--src/main/java/de/thedevstack/conversationsplus/enums/MessageStatus.java54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/main/java/de/thedevstack/conversationsplus/enums/MessageStatus.java b/src/main/java/de/thedevstack/conversationsplus/enums/MessageStatus.java
new file mode 100644
index 00000000..8a31a325
--- /dev/null
+++ b/src/main/java/de/thedevstack/conversationsplus/enums/MessageStatus.java
@@ -0,0 +1,54 @@
+package de.thedevstack.conversationsplus.enums;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+
+/**
+ */
+public enum MessageStatus {
+ WAITING,
+ TRANSMITTING,
+ TRANSMITTED,
+ FAILED,
+ CANCELED,
+ DISPLAYED,
+ RECEIVED;
+
+ static {
+ /*
+ * Initialization of valid next states in this block,
+ * because the enum values need to be known at initialization.
+ * If trying to initialize in Constructor, then only forward transitions are possible.,
+ * no way to handle jumping back.
+ */
+ WAITING.initValidNextStates(TRANSMITTING);
+ TRANSMITTING.initValidNextStates(FAILED, TRANSMITTED, CANCELED);
+ CANCELED.initValidNextStates(WAITING);
+ FAILED.initValidNextStates(WAITING);
+
+ // Restrict some transitions to be only valid for outgoing messages
+ TRANSMITTED.initValidNextStatesForDirection(MessageDirection.OUT, RECEIVED, DISPLAYED);
+ RECEIVED.initValidNextStatesForDirection(MessageDirection.OUT, DISPLAYED);
+ }
+
+ private List<MessageStatus> validNextStates;
+ private HashMap<MessageDirection, List<MessageStatus>> directionRestricted = new HashMap<>();
+
+ private void initValidNextStates(MessageStatus... validNextStates) {
+ this.validNextStates = Arrays.asList(validNextStates);
+ }
+
+ private void initValidNextStatesForDirection(MessageDirection direction, MessageStatus... newStatus) {
+ this.directionRestricted.put(direction, Arrays.asList(newStatus));
+ }
+
+ public boolean isTransitionToStateAllowed(MessageStatus newStatus, MessageDirection direction) {
+ boolean hasNoValidNextStates = this.validNextStates.isEmpty() && (!this.directionRestricted.containsKey(direction) || this.directionRestricted.get(direction).isEmpty());
+ boolean isValidNextState = this.validNextStates.contains(newStatus);
+ boolean isValidNextStateForDirection = (this.directionRestricted.containsKey(direction) && this.directionRestricted.get(direction).contains(newStatus));
+ return hasNoValidNextStates // no valid next states -> every new state is allowed
+ || isValidNextState // if newStatus is conditionless
+ || isValidNextStateForDirection; // if the newStatus is valid for the direction
+ }
+}