How to prevent code repetition with enums?

  • Thread starter daniel_nordlund_1982
  • Start date
D

daniel_nordlund_1982

Hello. I was wondering if there is a better way to write the below
code. I have several enums, where each enum is for an option type and
each option has a short 1-3 letter string used when I need to identify
the option from a string. The code for each option type is identical
and I'd prefer to not repeat it over and over again.

static enum YesNoOption {
YES("yes"), NO("no");
private final String str;
private YesNoOption(String str) { this.str = str; }
public String getOptionString() { return str; }
public static YesNoOption getOption(String str) {
for(YesNoOption o: YesNoOption.values())
if(o.str.equals(str))
return o;
return null;
}
}

static enum ColorOption {
BLUE("b"), RED("r"), GREEN("g"), MAGENTA("m"), YELLOW("y");
private final String str;
private ColorOption(String str) { this.str = str; }
public String getOptionString() { return str; }
public static ColorOption getOption(String str) {
for(ColorOption o: ColorOption.values())
if(o.str.equals(str))
return o;
return null;
}
}

....

-------------

YesNoOption yesno = YesNoOption.getOption("no");
ColorOption color = ColorOption.getOption("g");

if(color == ColorOption.GREEN) {
...
}

Daniel
 
C

Chris Uppal

Hello. I was wondering if there is a better way to write the below
code. I have several enums, where each enum is for an option type and
each option has a short 1-3 letter string used when I need to identify
the option from a string. The code for each option type is identical
and I'd prefer to not repeat it over and over again.

As far as I know there is no way to remove the repetition. So, if it bothers
you particularly (and it would bother me ;-) I suggest that you generate the
enum classes automatically.

Exactly how you do that depends on what programming language your favour for
text manipulation, and on what restrictions (if any) your build environment
imposes.

-- chris
 
P

Piotr Kobzda

Chris said:
As far as I know there is no way to remove the repetition. So, if it bothers
you particularly (and it would bother me ;-) I suggest that you generate the
enum classes automatically.

Yes. That's the option.

But a little improvement is also possible here with a single
implementation of the lookup logic, like in the following example:


public class Options {

public interface Option {
String getOptionString();
}

public static enum YesNoOption implements Option {
YES("yes"), NO("no");
private final String str;
private YesNoOption(String str) { this.str = str; }
public String getOptionString() { return str; }
public static YesNoOption getOption(String str) {
return forString(YesNoOption.class, str);
}
}

// ... other options ...


public static <T extends Enum<T> & Option> T forString(
Class<T> optionType, String str) {
for(T o : optionType.getEnumConstants())
if(o.getOptionString().equals(str))
return o;
return null;
}

}


piotr
 
E

EJP

public static YesNoOption getOption(String str) {
for(YesNoOption o: YesNoOption.values())
if(o.str.equals(str))
return o;
return null;
} }

This method is redundant. Enum has a static valueOf() method with,
curiously enough, the same parameters and return type as the above. It
throws IllegalArgumentException instead of returning null.
 
?

=?gb2312?B?yMrV387etdA=?=

But a little improvement is also possible here with a single
implementation of the lookup logic, like in the following example:

public class Options {

public interface Option {
String getOptionString();
}

public static enum YesNoOption implements Option {
YES("yes"), NO("no");
private final String str;
private YesNoOption(String str) { this.str = str; }
public String getOptionString() { return str; }
public static YesNoOption getOption(String str) {
return forString(YesNoOption.class, str);
}
}

// ... other options ...

public static <T extends Enum<T> & Option> T forString(
Class<T> optionType, String str) {
for(T o : optionType.getEnumConstants())
if(o.getOptionString().equals(str))
return o;
return null;
Very strong template code......
hard to understand,could you explain what the template trys to
describe by the way?
 
D

daniel_nordlund_1982

EJP skrev:
for(YesNoOption o: YesNoOption.values())
if(o.str.equals(str))
return o;
return null;
} }

This method is redundant. Enum has a static valueOf() method with,
curiously enough, the same parameters and return type as the above. It
throws IllegalArgumentException instead of returning null.

The problem is that with valueOf I have to use the full enum
identifier:

ColorOption color = ColorOption.valueOf("GREEN");

But I need this method to look up an option given it's abbreviation:

ColorOption color = ColorOption.getOption("g");

Thank you Piotr for your improved code!

Daniel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top