I created an enumeration, and I'm trying to allow my enumeration to support the String.format operation, which receives an unlimited number of parameters, returns a string. I managed to return the object, and after using this method I have to do toString()
/ casting. I assume there is a cleaner way to do this, or it might be better to use the toString()
method. Basically, I wanted to support the toString()
method, but, unfortunately, could not do this, so I created this method. As you can see, this is called text(..)
, not toString()
.
How can I do it 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); } }; } }
source share