11

I have a number of setter methods which take an enum. These are based on incoming objects attribute. Rather than write a bunch of these is there a way around having to hard code say 10 different case statements. Would there be a way to create a reusable method?

//Side class declared as
public final enum Side

//How I initialise side
static Side side = Side.SELL;//default

//method to set object
Obj.setSide(sideEnum(zasAlloc.getM_buySellCode()));

//How I am implementing it
    public static Side sideEnum(String buysell)
    {

        if(buysell.equalsIgnoreCase("S"))
        {
            side = Side.SELL; //default 
        }
        else if(buysell.equalsIgnoreCase("B"))
        {
            side = Side.BUY; 
        }

        return side;

    }
0

4 Answers 4

26

You can implement that functionality in your Enum.

public enum Side {

    BUY("B"), SELL("S"), ...

    private String letter;
    private Side(String letter) {
        this.letter = letter;
    }

    public static Side fromLetter(String letter) {
        for (side s : values() ){
            if (s.letter.equals(letter)) return s;
        }
        return null;
    }

}

You could also do this as a helper static method if you can't edit Side.

public static Side fromString(String from) {
    for (Side s: Side.values()) {
        if (s.toString().startsWith(from)) {
            return s;
        }
    }

    throw new IllegalArgumentException( from );
}

The above method assumes your strings correspond to the names of you enums.

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

2 Comments

Minor comment, if you have a lot of enums and the fromLetter() method is called often, it may make sense to create a static HashMap to make lookup/retrieval faster. But your solution makes perfect sense!
@Peter, correct. This is just the quick and dirty way. But the hashmap is definitely worth noting.
6

Enums have valueOf() method that can be used to convert from String. Is it what you are looking for?

4 Comments

ENUM1("one","1","I") Create your own method similar to value of such as getEnum(String s). This would allow you to return ENUM1 for any of the attribute values. valueOf works fine for many cases though.
Comment it's more the kind that if the string that comes in is "d" it maps to static enum side.DOG
@Will, this is why you need to provide some code, otherwise we're all left shooting in the dark.
I realise this but Unfortunately. I working with two Objects I do not have access to the source.
2

I ended up using a simple object map:

private static HashMap<String, Side> sideMap = new HashMap<String, Side>(7);
static{
    sideMap.put("B", Side.BUY);
    sideMap.put("S", Side.SELL);
}

and simply using

Obj.setSide(sideMap.get(zasAlloc.getM_buySellCode()));

Comments

0

I think you need smth like:

Obj.setSide(Side.valueOf(zasAlloc.getM_buySellCode()));

Comments

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.