As you can see below, I have three declared enums, and each class has a method called getEnumByName()
which revives a name and returns the enum which has that name.
I have noticed that I am duplicating the same functionality of this method on each enum.
Is there any way to change this method to a generic one, which receives the given enum's type and does the same logic?
public class Enums {
public enum A {
APPLY("Apply", "abcde");
private String id;
private String name;
A(String name, String id) {
this.name = name;
this.id = id;
}
public static A getEnumByName(String name) throws Exception {
for (A instance : A.values()) {
if (instance.getName().equals(name)) return instance;
}
throw new Exception("There is no operations matches :" + name);
}
public String getName() {
return name;
}
public String getId() {
return id;
}
}
public enum B {
APPLY("Apply", "1"),
SAVE("Save", "2"),
REVERT("Revert", "2"),
REVERT_CHILD("Revert->Revert", "4"),
REVERT_APPLY("Revert->Revert Apply", "5"),
SYNC("Sync", "6"),
OPERATIONS("Operations", "7"),
IMPORT("Import", "8"),
EXPORT("Export", "9"),
DIFF("Diff", "10");
private String id;
private String name;
B(String name, String id) {
this.name = name;
this.id = id;
}
public static B getEnumByName(String name) throws Exception {
for (B instance : B.values()) {
if (instance.getName().equals(name)) return instance;
}
throw new Exception("There is no operations matches :" + name);
}
public String getName() {
return name;
}
public String getId() {
return id;
}
}
public enum C {
UPDATE_POLICES("Update Policies", "A"),
OPERATIONS("Operations", "B"),
IMPORT_CONFIGURATION_FILE("Import Configuration File", "c"),
EXPORT_CONFIGURATION_FILE("Export Configuration File", "d"),
EXPORT_LOG_SUPPORT_FILE("Export Log Support File", "f"),
EXPORT_TECHNICAL_SUPPORT_FILE("Export Technical Support File", "g"),
UPDATE_SOFTWARE_VERSION("Update Software Version", "g"),
UPDATE_SECURITY_SINGAUTES("Update Security Signatures", "h"),
DIFF("Diff", "k");
private String id;
private String name;
C(String name, String id) {
this.name = name;
this.id = id;
}
public static C getEnumByName(String name) throws Exception {
for (C instance : C.values()) {
if (instance.getName().equals(name)) return instance;
}
throw new Exception("There is no operations matches :" + name);
}
public String getName() {
return name;
}
public String getId() {
return id;
}
}
}