0

Hi I'm having trouble trying to generalize a function I've written for an specific enum:

public static enum InstrumentType {
    SPOT {
        public String toString() {
            return "MKP";
        }
    },
    VOLATILITY {
        public String toString() {
            return "VOL";
        }
    };

    public static InstrumentType parseXML(String value) {
        InstrumentType ret = InstrumentType.SPOT;

        for(InstrumentType instrumentType : values()) {
            if(instrumentType.toString().equalsIgnoreCase(value)) {
                ret = instrumentType;
                break;
            }
        }

        return ret;
    }
} 

I wish to add a new parameter to the function that will represent any enum. I know I'm supposed to use templates but I can't use the function "values()" then inside the function code. Basicly what I want is a valueOf function that uses the toString() value I've defined.

Thanks in advance.

Esko
  • 29,022
  • 11
  • 55
  • 82
eliocs
  • 18,511
  • 7
  • 40
  • 52
  • wouldn't it be easier to just name your enums MKP and VOL, that way you get the string parsing for free? – falstro Nov 30 '09 at 09:23
  • possible duplicate? http://stackoverflow.com/questions/604424/java-enum-converting-string-to-enum – Rubens Farias Nov 30 '09 at 09:25
  • I agree, but I also want it to choose a default value and I dont want the name of the ENUM be dependant on the string it prints. – eliocs Nov 30 '09 at 09:25

2 Answers2

16

Try a much cleaner way of writing your enum:

public static enum InstrumentType {

    SPOT("MKP"),
    VOLATILITY("VOL");

    private final String name;

    InstrumentType(String name)
    {
        this.name = name;
    }

    public String toString()
    {
        return this.name;
    }

    public static InstrumentType getValue(String s)
    {
        for (InstrumentType t : InstrumentType.values())
        {
            if (t.toString().equals(s))
                return t;
        }
        return SOME_DEFAULT_VALUE;
    }
}

This also solves your problem of String -> Enum. It might be cleaner to just use the three-letter acronyms as the enum name, but in the case you will need to make the getValue() decision according to other parameters, this is the right way to go.

Prashant Kumar
  • 20,069
  • 14
  • 47
  • 63
Yuval Adam
  • 161,610
  • 92
  • 305
  • 395
  • That is a neater way to overwrite the toString method, thanks. – eliocs Nov 30 '09 at 09:27
  • 1
    +1 We're naturally allergic to linear searches, so we tend not to realize that in small and medium enums it's never really going to cost that much. – Kevin Bourrillion Dec 01 '09 at 21:44
  • Just wanted to call attention (since I missed it at first) to the fact that getValue is intentionally named differently from the static valueOf you get for free. valueOf cannot be overridden. – Joshua Goldberg Mar 28 '11 at 15:37
  • FWLIW, I'd use a name a little more different from "toValue" - there's no way to "turn off" toValue, and one's too likely to use the wrong one without noticing. Even just "getValueOf" would be enough. – Ed Staub Jan 26 '12 at 14:55
0

I believe that Enums can implement interfaces, so you could define one with a values() method and then use that as your parameter type.

But as the commenter said, it would probably be easier if you named your enums MKP, VOL etc