Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: enum toString()

Tags:

java

string

enums

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);
            }
        };
    }
}
like image 572
Popokoko Avatar asked Apr 18 '12 18:04

Popokoko


People also ask

Does Java enum have toString?

The Java Enum has two methods that retrieve that value of an enum constant, name() and toString(). The toString() method calls the name() method, which returns the string representation of the enum constant.

What does enum toString do?

toString() method returns the name of this enum constant, as contained in the declaration.

Can we override toString method for enum in Java?

The toString() method of Enum class returns the name of this enum constant, as the declaration contains. The toString() method can be overridden, although it's not essential.

How do I override strings in enum?

ToString() and only provide switch case statements for the enum values that you want to override. In your example, I get that they're all different but in actual use cases, I suspect that most of the single-word enum values will suffice and you'll only be providing overrides for multi-word enum values.


2 Answers

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.

like image 176
Bohemian Avatar answered Oct 17 '22 04:10

Bohemian


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.

like image 4
Óscar López Avatar answered Oct 17 '22 04:10

Óscar López