6

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);
            }
        };
    }
}
3
  • 3
    I don't understand what you want to achieve. Why make this method an instance method of an enum, since it doesn't use anything from the enum instance? Commented Apr 18, 2012 at 18:38
  • 1
    How is this method related to your enum? What do you want it to return exactly? Commented Apr 18, 2012 at 18:38
  • I extended my code, im trying to support a toString() method with few given string params.. Commented Apr 18, 2012 at 18:45

2 Answers 2

9

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Great solution, works perfectly, erm, i was really close to solve it myself, thanks a lot!
I 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, so VALUE_A would be like "aaa <object toString> bbb <objecttoString>".
@thecoshman that is true of toString() without a parameter ( ie overriding Object's impl), but toString() with parameter(s) can do what it likes. There are lots of examples from the JDK that use this pattern, for example Integer.toString(int).
@Bohemian fair point, though we are of course arguing semantics here now. I'd still favour .format(...) over .toString(...) in this context.
4

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.

1 Comment

Yes very confusing when someone doesnt want to pass any params but gets the parameterless toString.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.