📜 ⬆️ ⬇️

Solving the problem of lack of switch (String) in Java using enum

It happened once that my good colleague who is programming in Flex was transferred to a project where Ext GWT was used. From time to time I had to answer the questions: “How to do this and that in Java?” And “Why is this not there and how can we get around this?”. The ActionScript, in which my colleague was used to writing, was more flexible in comparison with Java and allowed me to not really bother about coercion and description of variable types. What was his surprise when in the switch construction it was impossible to make comparisons of variables of type String. Although, it would seem, there are many technical situations where such a switch would be useful (processing different scenarios depending on the contents of the passed String variable).

Fortunately, currently this functionality is implemented in Java 7. However, due to the fact that the transition to Java 7 will not be perfect instantly in most projects, I want to share my own “aesthetic” way with pleasure in order to avoid cumbersome if structures. .then..else.

Suppose that we have a type variable that can take the values ​​RequestOne, RequestTwo, RequestThree, and depending on this, different scenarios will be executed.
')
In the simplest case, the code of the method processing the request could look like this:

public void processRequest(String type) { if (type.equals("RequestOne")) { scenarioOne(); } else if (type.equals("RequestTwo")) { scenarioTwo(); } else if (type.equals("RequestThree")) { scenarioThree(); } } 


The code can be inconvenient for reading if the type will accept long lines or there may be several options for the type for one script. In this case, the enumerated type comes to the rescue: enum. Describes the enumerated type RequestType as follows:

 public enum RequestType { SCENARIO_ONE("RequestOne"), SCENARIO_TWO("RequestTwo"), SCENARIO_THREE("RequestThree"); private String typeValue; private RequestType(String type) { typeValue = type; } static public RequestType getType(String pType) { for (RequestType type: RequestType.values()) { if (type.getTypeValue().equals(pType)) { return type; } } throw new RuntimeException("unknown type"); } public String getTypeValue() { return typeValue; } } 


And then the processRequest (String) method is converted as follows:

 public void processRequest(String type) { RequestType request = RequestType.getType(type); switch(request) { case SCENARIO_ONE: scenarioOne(); break; case SCENARIO_TWO: scenarioTwo(); break; case SCENARIO_THREE: scenarioThree(); break; } } 


Of course, the static getType method in the RequestType enumeration type is not optimal, especially if we are talking about a large number of possible type values. But with this approach, it turns out to keep a list of type string values ​​in one place and the switch construction looks succinctly. Accordingly, adding new type values ​​and editing old ones can happen an order of magnitude faster, which leads to an increase in labor productivity.

In the end, I agree, this is not the best solution in terms of performance, but a fairly good example of using the listed types for the case described above.

Source: https://habr.com/ru/post/128390/


All Articles