Reputation: 1041
I have a common process with different progress values and step number, depending on the user.
To solve this I've made an interface :
public interface Progress {
int getTotalStepNumber();
int getIndex();
String getMessage();
@Override
String toString();
}
So a step process implementation is like this, litteraly, it is an enumeration of the steps for this process :
public enum ProgressImplementationOfProcessOne implements Progress {
STEP_ONE(1, "Step one message."),
STEP_TWO(2, "Step two message.");
// ... etc. with more steps
/**
* Number of steps for this process.
*/
private static final int STEPS = 2;
private int index;
private String message;
ProgressImplementationOfProcessOne(int index, String message) {
this.index = index;
this.message = message;
}
@Override
public int getTotalStepNumber() { return STEPS; }
@Override
public int getIndex() { return this.index; }
@Override
public String getMessage() { return this.message; }
@Override
public String toString() { return this.message; }
}
But then I thought it would be nice to find the corresponding step from implementation as well, with the method valueOf()
of enumerations. So I added the following lines to my interface :
default Progress valueOf(String s) {
for (Progress progress : this.getValues()) {
if (progress.getMessage().equals(s)) {
return progress
}
}
return null;
}
default Progress valueOf(int i) {
for (Progress progress : this.getValues()) {
if (progress.getIndex() == this.getIndex()) {
return progress;
}
}
return null;
}
Since there is no getValues()
method in the interface Progress
I added the following method to it (thinking "the enum implementation will handle it natively").
default List<Progress> getValues() { return null; }
But I don't understand why I get this error for each ProgressImplementation
:
This static method cannot hide the instance method from Progress.
.
I know I could do it by creating an additional class ProgressStep
which replace an enum value, and replace enum by classes with attributes and so on, but since enum can handle multiple values I thought it could have been easier with it.
Upvotes: 0
Views: 280
Reputation: 6435
Simply rename the valueOf
to something like valueOfEnum
, as valueOf
is already specified by java.lang.Enum
.
public static <T extends Enum<T>> T valueOf(Class<T> enumType,
String name) {
T result = enumType.enumConstantDirectory().get(name);
if (result != null)
return result;
if (name == null)
throw new NullPointerException("Name is null");
throw new IllegalArgumentException(
"No enum constant " + enumType.getCanonicalName() + "." + name);
}
The comment on that method contains following section:
Note that for a particular enum type T, the implicitly declared public static T valueOf(String) method on that enum may be used instead of this method to mapfrom a name to the corresponding enum constant. All theconstants of an enum type can be obtained by calling the implicit public static T[] values() method of that type
Emphasis mine
As you can see, valueOf(String s)
is already declared on every enum class which in turn is the reason, you can't have it in an interface on any enum
Upvotes: 1