Scene description
Suppose we have an e-commerce system that contains two core state enumerations:
- Order Status (OrderStatusEnum)- Describe the life cycle of orders in the e-commerce system
- Logistics Status (ShippingStatusEnum)- Describe the flow status of orders in the logistics system
These two states are closely related but not exactly the same, and we need to establish a transformation relationship between them.
Enumeration definition
1. Order status enumeration
/** * Order status enumeration */ public enum OrderStatusEnum { UNPAID(0, "To be paid"), PAID(1, "Paid"), PACKAGED(2, "Packed"), SHIPPED(3, "Shipped"), DELIVERED(4, "Delivered"), COMPLETED(5, "Completed"), CANCELLED(10, "Canceled"), REFUNDING(11, "Refund"), REFUNDED(12, "Refunded"); private final int code; private final String description; OrderStatusEnum(int code, String description) { = code; = description; } // Get enum instance according to code public static OrderStatusEnum fromCode(int code) { for (OrderStatusEnum status : values()) { if ( == code) { return status; } } throw new IllegalArgumentException("Invalid order status code: " + code); } // Convert to logistics state public ShippingStatusEnum toShippingStatus() { switch (this) { case UNPAID: case PAID: case CANCELLED: case REFUNDING: case REFUNDED: return null; //These order statuses do not have corresponding logistics status case PACKAGED: return ; case SHIPPED: return ShippingStatusEnum.IN_TRANSIT; case DELIVERED: return ; case COMPLETED: return ; default: throw new IllegalStateException("Unknown order status: " + this); } } // getters public int getCode() { return code; } public String getDescription() { return description; } }
2. Logistics status enumeration
/** * Logistics status enumeration */ public enum ShippingStatusEnum { PREPARING(1, "Preparing"), IN_TRANSIT(2, "In transit"), DELIVERED(3, "Delivered"), COMPLETED(4, "Completed"), RETURNING(10, "Returning"), RETURNED(11, "Returned"); private final int code; private final String description; ShippingStatusEnum(int code, String description) { = code; = description; } // Get enum instance according to code public static ShippingStatusEnum fromCode(int code) { for (ShippingStatusEnum status : values()) { if ( == code) { return status; } } throw new IllegalArgumentException("Invalid logistics status code: " + code); } // Convert to order status public OrderStatusEnum toOrderStatus() { switch (this) { case PREPARING: return ; case IN_TRANSIT: return ; case DELIVERED: return ; case COMPLETED: return ; case RETURNING: return ; case RETURNED: return ; default: throw new IllegalStateException("Unknown logistics status: " + this); } } // getters public int getCode() { return code; } public String getDescription() { return description; } }
Analysis of key design points
-
State mapping relationship:
- Not all order statuses have corresponding logistics status (such as to be paid, cancelled, etc.)
- Logistics status may trigger changes in order status (such as the corresponding refund in return)
-
Conversion method design:
- Each enumeration class implements conversion methods internally, knowing how to convert to another state
- Intuitive method naming:
toShippingStatus()
andtoOrderStatus()
-
null value processing:
- Return null when there is no corresponding state, the caller needs to deal with this situation
-
Exception handling:
- Throw clear exception information on illegal status codes
Example of usage
// Order status to logistics statusOrderStatusEnum orderStatus = ; ShippingStatusEnum shippingStatus = (); ("Order Status: " + () + " → Logistics Status: " + (shippingStatus != null ? () : "none")); // Logistics status to order statusShippingStatusEnum shippingStatus = ; OrderStatusEnum orderStatus = (); ("Logistics Status: " + () + " → Order Status: " + ());
Best Practice Recommendations
Keep the conversion logic simple: If the conversion logic becomes complicated, consider introducing a state mode
Documented mapping relationships: explicitly document the state mapping relationship in the enumeration class or project document
Unit Testing: Write unit tests for state transitions to ensure the conversion logic is correct
Consider performance: For high-frequency calls, the conversion results can be cached
Extensibility: Consider possible new statuses in the future when designing
Summarize
By implementing the state transition method inside the enumeration, we implement:
- High cohesion design: Transformation logic is defined together with state
- Easy to maintain: only one place to modify when the status changes
- Type safety: Check status transitions at compile time
- Clear code: intuitive conversion method calls
This model is not only suitable for order and logistics states, but can also be applied to various scenarios where state transition is required, such as workflow state, payment state, etc.
This is the end of this article about the detailed explanation of Java's method of implementing enumeration state conversion. For more related Java enumeration state conversion content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!