Nested enumeration in Java?
I want to define some enumerations for the various Ajax services provided in my web application, such as:
Enum Service { REGISTER,LOGIN,NEWS,FAQ }
However, each of these enumerations will have a state, such as failed, loaded, etc So I hope to use register LOADED,LOGIN. Loaded, etc. to activate events on my event bus However, each state enumeration must be unique I.e Register. Loaded must be consistent with FAQ Loaded is different, and so on
Edit: in addition, I must be able to store all States in the same hash map, such as register Loaded and login Loaded must be stored in the same hash map And the parent service enumeration, i.e. logo, register, etc., must also be stored in the same hash map
What is the best way to achieve this goal?
Solution
After some comments from OP, I updated my code The usage syntax becomes ugly and may not work properly in the switching block, but I believe this can achieve the goal of OP:
import java.util.*; public class HelloWorld { public static void main(String []args) { // Different services are not equal System.out.println(Service.REGISTER + "==" + Service.LOGIN + " :: " + Service.REGISTER.equals(Service.LOGIN)); // Same service is equal System.out.println(Service.REGISTER + "==" + Service.REGISTER + " :: " + Service.REGISTER.equals(Service.REGISTER)); // Even after changing the state Service tmp = Service.REGISTER; Service.REGISTER.setState(Service.ServiceState.State.Failed); System.out.println(Service.REGISTER + "==" + tmp + " :: " + Service.REGISTER.equals(tmp)); Service.REGISTER.setState(Service.ServiceState.State.LOADED); // Different service,same state is not equal System.out.println(Service.REGISTER + "." + Service.REGISTER.state + "==" + Service.LOGIN + "." + Service.LOGIN.state + " :: " + Service.REGISTER.state.equals(Service.LOGIN.state)); // Same service,same state is equal (even when changing state around) Service.ServiceState.State temp = Service.REGISTER.getState(); Service.REGISTER.setState(Service.ServiceState.State.LOADED); System.out.println(Service.REGISTER + "." + Service.REGISTER.state + "==" + tmp + "." + temp + " :: " + temp.equals(Service.REGISTER.getState())); // Same service,different state is not equal Service.REGISTER.setState(Service.ServiceState.State.Failed); System.out.println(Service.REGISTER + "." + Service.REGISTER.state + "==" + tmp + "." + temp + " :: " + temp.equals(Service.REGISTER.getState())); // Both service and state can be used as map keys Map<Service,String> map = new HashMap<Service,String>(); Map<Service.ServiceState.State,String> map2 = new HashMap<Service.ServiceState.State,String>(); } } enum Service { REGISTER(),LOGIN(),NEWS(),FAQ(); public ServiceState state; Service() { this.state = new ServiceState(); } public void setState(ServiceState.State s) { state.state = s; } public ServiceState.State getState() { return state.state; } public static class ServiceState { public enum State { LOADED,Failed } public State state = State.LOADED; public ServiceState(){} public String toString() { return state.toString(); } public int hashCode() { return state.hashCode(); } public boolean equals(Object obj) { return state.equals(obj); } } }
Output:
REGISTER==LOGIN :: false REGISTER==REGISTER :: true REGISTER==REGISTER :: true REGISTER.LOADED==LOGIN.LOADED :: false REGISTER.LOADED==REGISTER.LOADED :: true REGISTER.Failed==REGISTER.LOADED :: false
Original answer:
It's not possible to use the syntax you want, but Java enumeration is basically just a class with another name (in fact, once compiled, they are classes that extend Java. Lang. enum < e extensions enum < E > >)
public class HelloWorld { public static void main(String []args) { Service a = Service.REGISTER; a.state = Service.ServiceState.LOADED; System.out.println(a); } } enum Service { REGISTER(),FAQ(); public ServiceState state; Service() { this.state = ServiceState.LOADED; } public String toString() { return super.toString() + "." + state.toString(); } public enum ServiceState { LOADED,Failed } }
Output: