I have created an enum and I'm trying to allow my enum to support a String.format operation that gets unlimited number of parameters are return a string.
I only managed to return an object and after using this method I have to do a toString()/casting. I am guessing there's a more "clean" way to do it, or maybe to override better the toString() method. Basically, I wanted to support the toString() method but sadly didn't manage to do that so I created this method. As you can see it's named text(..) and not toString().
How can I do this better? The ideal solution I wanted was something like toString(..) which returns a String.
public enum MY_ENUM {
VALUE_A("aaa %s"), VALUE_B("bbb %s");
private String text;
MY_ENUM(String text) {
this.text = text;
}
public String text() {
return this.text;
}
public Object text(final Object... o) {
return new Object() {
@Override
public String toString() {
return String.format(text(), o);
}
};
}
}