I have this Enum from which I am calling the appropriate execute
method based on what type of enum (eventType
) is passed.
public enum EventType {
EventA {
@Override
public Map<String, Map<String, String>> execute(Map<String, String> eventMapHolder) {
if (eventMapHolder.isEmpty()) {
return ImmutableMap.of();
}
String clientId = eventMapHolder.get("clientId");
Map<String, String> clientInfoHolder = getClientInfo(clientId);
eventMapHolder.putAll(clientInfoHolder);
return ImmutableMap.<String, Map<String, String>>builder().put(EventA.name(), eventMapHolder)
.build();
}
},
EventB {
@Override
public Map<String, Map<String, String>> execute(Map<String, String> eventMapHolder) {
if (eventMapHolder.isEmpty()) {
return ImmutableMap.of();
}
return ImmutableMap.<String, Map<String, String>>builder().put(EventB.name(), eventMapHolder)
.build();
}
},
EventC {
@Override
public Map<String, Map<String, String>> execute(Map<String, String> eventMapHolder) {
if (eventMapHolder.isEmpty()) {
return ImmutableMap.of();
}
String clientId = eventMapHolder.get("clientId");
Map<String, String> clientInfoHolder = getClientInfo(clientId);
eventMapHolder.putAll(clientInfoHolder);
return ImmutableMap.<String, Map<String, String>>builder().put(EventC.name(), eventMapHolder)
.build();
}
},
EventD {
@Override
public Map<String, Map<String, String>> execute(Map<String, String> eventMapHolder) {
if (eventMapHolder.isEmpty()) {
return ImmutableMap.of();
}
Map<String, Map<String, String>> holder = new HashMap<>();
String clientId = eventMapHolder.get("clientId");
Map<String, String> clientInfoHolder = getClientInfo(clientId);
eventMapHolder.putAll(clientInfoHolder);
holder.put(EventD.name(), eventMapHolder);
Map<String, String> processInfoHolder = getProcessInfo(clientId);
holder.put(EventE.name(), processInfoHolder);
return Collections.unmodifiableMap(holder);
}
},
EventE {
@Override
public Map<String, Map<String, String>> execute(Map<String, String> eventMapHolder) {
return ImmutableMap.of();
}
};
public abstract Map<String, Map<String, String>> execute(String eventMapHolder);
public Map<String, String> getClientInfo(final String clientId) {
// code to populate the map and return it
}
public Map<String, String> getProcessInfo(final String clientId) {
// code to populate the map and return it
}
}
For example: If I get "EventA"
, then I am calling its execute
method. Similarly, if I get "EventB"
then I am callings its execute
method, and so on.
String eventType = String.valueOf(payload.get("eventType"));
Map<String, String> eventMapHolder = payload.get("eventMapHolder");
Map<String, Map<String, String>> processedMap = EventType.valueOf(eventType).execute(eventMapHolder);
In general, I will have more event types (around 10-12) in the same enum class and mostly they will do the same operation as EventA
, EventB
, EventC
and EventD
. In my above enum, I have EventE
just to be used as a constant name. Its execute method won't be called at all since I have an abstract execute method, so I have to override it. As you can see, in my EventD
, I am populating maps using EventD
and EventE
as the key.
Question:
Now as you can see, code in the execute
method of EventA
and EventC
are identical but the only difference is what I put as "key" (event name)
in the returned immutable map. Similarly, there might be another event as well which will have the same exact code as EventD
. Is there any way to remove that duplicated code but still achieve the same functionality in the enum?
The idea is, based on what type of event is passed, I want to call its execute method, perform some operation and try to avoid duplication if possible. Also, I am thinking of moving getClientInfo
and getProcessInfo
in some other utility class so that this Enum class should have execution code only.
If there is any other better way or any other design pattern to do this then I am open for suggestions as well which can help me remove duplicated code.