Reputation: 29
Ok, this is the example. I know this requirement can be covered in a better way, please see it as an example to illustrate the need.
Module1Messages and Module2Message are the enums. The intention is to have a method (getAllModuleMessages) to get all the messages from any module in a json array structure.
Once this said, and the things that can be improved are:
The internal getMessagesAndCodes in each enum are clones, just the reference to its own enum is what differ. Is possible to move it out and be unique receiving the enum from where you want to perform this action?
In getAllModuleMessages, the list of enums is static. Is it possible to get the list by family?
I read that maybe what I'm looking for is a Interface. Can you introduce me in Interfaces for enums? will it resolve all those needs?
Thanks again
public enum Module1Messages{MOD1_MESSAGE_1("Message 1 for module 1", "Mensaje 1 para modulo 1");
private Module1Messages(String msgCodeEn, String msgCodeEs){
this.message_en=msgCodeEn;
this.message_es=msgCodeEs;
}
public String getMessageEN(){return this.message_en;}
public String getMessageES(){return this.message_es;}
public static JSONArray getMessagesAndCodes(){
JSONArray jArr=new JSONArray();
for (Module1Messages curMsgCode: Module1Messages.values()){
JSONObject jObj=new JSONObject();
jObj.put("message_code", curMsgCode.name());
jObj.put("English", curMsgCode.message_en);
jObj.put("Spanish", curMsgCode.message_es);
jArr.add(jObj);
}
return jArr;
}
private final String message_en;
private final String message_es;
}
private Module2Messages(String msgCodeEn, String msgCodeEs){
this.message_en=msgCodeEn;
this.message_es=msgCodeEs;
}
public String getMessageEN(){return this.message_en;}
public String getMessageES(){return this.message_es;}
public static JSONArray getMessagesAndCodes(){
JSONArray jArr=new JSONArray();
for (Module2Messages curMsgCode: Module2Messages.values()){
JSONObject jObj=new JSONObject();
jObj.put("message_code", curMsgCode.name());
jObj.put("English", curMsgCode.message_en);
jObj.put("Spanish", curMsgCode.message_es);
jArr.add(jObj);
}
return jArr;
}
private final String message_en;
private final String message_es;
}
public JSONArray getAllModuleMessages(){
JSONArray jArr=new JSONArray();
jArr.add(Module1Messages.getMessagesAndCodes());
jArr.add(Module2Messages.getMessagesAndCodes());
return jArr;
}
Upvotes: 1
Views: 125
Reputation: 1460
Your enums can implement Interfaces as you've said, for this particular example it would be something like:
public interface LanguageMessages {
String getCode();
String getEnglish();
String getSpanish();
}
public enum Module1Messages implements LanguageMessages {
...
getCode() { return name() }
getEnglish() { return messageEn; } // naming conventions
getSpanish() { return messageSp; }
}
public enum Module2Messages implements LanguageMessages {
...
getCode() { return name() }
getEnglish() { return messageEn; } // naming conventions
getSpanish() { return messageSp; }
}
There is obviously a little bit of overlap, but now you can loop through your values as LanguageMessages
:
public static JSONArray getMessagesAndCodes(LanguageMessages[]... messages){
JSONArray jArr=new JSONArray();
for (LanguageMessages[] currMessages: messages){
for (LanguageMessages message : currMessages) {
JSONObject jObj=new JSONObject();
jObj.put("message_code", message.getCode());
jObj.put("English", message.getEnglish());
jObj.put("Spanish", message.getSpanish());
jArr.add(jObj);
}
}
return jArr;
}
Now just figure out how you actually want to call it:
getMessagesAndCodes(Module1Messages.values(), Modul2Messages.values());
Upvotes: 2