I have the following interface:
public interface Moon {
// Enums can not extend any class in Java,
// thus I am implementing an interface
double getMass();
double getDiameter();
}
that is implemented by my enums (I omitted the constructor and methods implementation)
public enum Saturn implements Moon {
ATLAS(30.2, 6.6),
PAN(28.2, 4.95);
private double Diameter;
private double Mass;
// constructor and methods implementation
}
and similarly enums for other planets:
public enum Mars implements Moon {
PHOBOS(22.2, 1.08),
DEIMOS(12.6, 2);
private double Diameter;
private double Mass;
// constructor and methods implementation
}
Question
Having two strings:
String planetName = "Mars";
String moonName = "PHOBOS";
How can I obtain the particular enum in a smart way?
In the case of only one enum class it is a simple use of the valueOf
method, but how can I check all enums that implement a particular interface?
Moon something = <??planetName??>.valueOf(moonName);