I have below Enum as shown below from which I need to call its appropriate method basis on what type of event it is.
public enum EventType {
EventA {
// cannot call this method
public Map<String, Map<String, String>> execute(String eventMapHolder) {
final Map<String, String> holder = parseStringToMap(eventMapHolder);
if (holder.isEmpty() || Strings.isNullOrEmpty(holder.get("m_itemId"))) {
return ImmutableMap.of();
}
String itemId = holder.get("m_itemId");
Map<String, String> clientInfoHolder = getClientInfo(itemId);
holder.putAll(clientInfoHolder);
return ImmutableMap.<String, Map<String, String>>builder().put(EventA.name(), holder)
.build();
}
},
EventB {
// cannot call this method
public Map<String, Map<String, String>> execute(String eventMapHolder) {
final Map<String, String> holder = parseStringToMap(eventMapHolder);
if (holder.isEmpty() || Strings.isNullOrEmpty(holder.get("m_itemId"))) {
return ImmutableMap.of();
}
return ImmutableMap.<String, Map<String, String>>builder().put(EventB.name(), holder)
.build();
}
};
public Map<String, String> parseStringToMap(String eventMapHolder) {
// parse eventMapHolder String to Map
}
public Map<String, String> getClientInfo(final String clientId) {
Map<String, String> clientInfoMap = Maps.newHashMap();
// code to populate this map
return clientInfoMap;
}
public Map<String, String> getDeviceInfo(final String itemId) {
Map<String, String> deviceInfoMap = Maps.newHashMap();
// code to populate this map
return deviceInfoMap;
}
}
Question:
For example: If I get "EventA", then I want to call it's execute method. Similarly if I get "EventB" then I want to call it's execute method and so on. Right now it is not working because of obvious reason as it is not able to find execute method in the same enum.
String eventType = String.valueOf(payload.get("eventType"));
String eventMapHolder = String.valueOf(payload.get("eventMapHolder"));
// this below line gives compilation error as it is not able to find execute method.
Map<String, Map<String, String>> processedMap = EventType.valueOf(eventType).execute(eventMapHolder);
I need to perform certain actions depending on what types of events is passed.
- If
"EventA"is passed, then first I am convertingeventMapHolderstring into Map (let's call this holder) and then I extractitemIdfrom that holder map and then I passitemIdtogetClientInfomethod which in turn returns a Map (let's call this clientInfoHolder). And then I put everything fromclientInfoHoldermap intoholdermap and then finally return another Map based onholderalong with its key. - If
"EventB"is passed, then first I am convertingeventMapHolderstring into Map (let's call this holder) and then I make a new immutable map basis onholdermap along with its key and return that map.
In general I will have more event types in the same enum class and mostly they will do same operation as EventA and EventB.
What is the right and oop way to make this work? How can I call method of that particular enum type?
If there is any other better way or any other design pattern to do this then I am open for suggestions as welll.
Aucun commentaire:
Enregistrer un commentaire