Java: enum toString()
Solution 1
I see where you're going... I think this is what you want (tested, and it works):
public String toString(Object... o) {
return String.format(text, o);
}
For a design point of view, I would not publish the text (ie have the getter) unless you really need to - the fact that text
is used as a format string is an implementation choice. I would simply do this:
public static enum MY_ENUM {
VALUE_A("aaa %s bbb %s"),
VALUE_B("bbb %s");
private final String text;
MY_ENUM(String text) {
this.text = text;
}
public String toString(Object... o) {
return String.format(text, o);
}
}
As an aside, I really like the idea of the class. Haven't seen it before.
Solution 2
You can't override toString()
if you need to pass more parameters (toString()
doesn't receive any). Simply define a new method in the enum, no need to override:
public String getAsFormattedText(Object... o) {
return String.format(text, o);
}
You shouldn't name this method toString()
, it'd be confusing because you're not returning the string representation of the current object, instead you're returning a formatted string of the objects passed as parameters. Also, the text()
method should be called getText()
, that's the Java convention.
Better use a name that clearly indicates that the returned string is not any string - it's a formatted string that expects the text to be formatted as a parameter - getAsFormattedText()
clearly expresses this.
Popokoko
Updated on November 27, 2020Comments
-
Popokoko over 3 years
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 thetoString()
method. Basically, I wanted to support thetoString()
method but sadly didn't manage to do that so I created this method. As you can see it's namedtext(..)
and nottoString()
.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); } }; } }
-
Popokoko about 12 yearsGreat solution, works perfectly, erm, i was really close to solve it myself, thanks a lot!
-
mP. almost 11 yearsYes very confusing when someone doesnt want to pass any params but gets the parameterless toString.
-
thecoshman over 9 yearsI like this idea too, though I would favour using a function more like
anEnumValue.format(Object o)
. to me, 'toString' should return a string representation of the enum value itself, soVALUE_A
would be like "aaa <object toString> bbb <objecttoString>". -
Bohemian over 9 years@thecoshman that is true of
toString()
without a parameter ( ie overriding Object's impl), buttoString()
with parameter(s) can do what it likes. There are lots of examples from the JDK that use this pattern, for exampleInteger.toString(int)
. -
thecoshman over 9 years@Bohemian fair point, though we are of course arguing semantics here now. I'd still favour
.format(...)
over.toString(...)
in this context.