Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using enums or static string variable in java

Tags:

java

got a question around which one is better to use. Java5 Enums or static string.

I always get the data in form of Strings.

So for example,

private static final String LOAD_DRUGS = "load drugs";

or use Java5 enums

public enum LoadType
{
LOAD_DRUGS("load drugs");
}

In my code, I always receive "load drugs" kind of strings. I use if else statements to see what to do next based on it. But I am tending to use java5 enums and use switch case, but I always have to get the enum based of the string value I get.

So what are the pro's and con's of both ways??

Thanks!!

like image 485
Rishi Avatar asked Jun 07 '26 10:06

Rishi


2 Answers

This answer is probably overkill. Maybe there's a badge for that. Anyway, it could be useful in a situation in which you have a lot of enumeration values and have to deal with a Strings as being the means by which another system sends information to you. That is exactly what I have (something north of 50), so I used this construct so that I could generate a mapping just once of the Strings reported by the db and the enums I used internally, and then not think about it after -- toString and fromString do all the work:

package com.stevej;

import com.google.common.collect.HashBiMap;

public enum TestEnum {
  ALPHA("Alpha"), BETA("Beta"), GAMMA("Gamma");

  private static HashBiMap<TestEnum, String> stringMapping = HashBiMap
      .create(TestEnum.values().length);

  private String stringValue = null;

  TestEnum(String stringValue) {
    this.stringValue = stringValue;
  }

  String getStringValue() {
    return this.stringValue;
  }

  @Override
  public String toString() {
    return stringMapping.get(this);
  }

  public static TestEnum fromString(String string) {
    return stringMapping.inverse().get(string);
  }

  static {
    for (TestEnum e : TestEnum.values()) {
      stringMapping.put(e, e.getStringValue());
    }
  }
}

Here's a quick test to show the data switching back and forth:

package com.stevej;

public class StackOverflowMain {

  public static void main(String[] args) {
    System.out.println(">> " + TestEnum.ALPHA);
    System.out.println(">> " + TestEnum.BETA);
    System.out.println(">> " + TestEnum.GAMMA);

    TestEnum a = TestEnum.fromString("Alpha");
    TestEnum b = TestEnum.fromString("Beta");
    TestEnum c = TestEnum.fromString("Gamma");

    System.out.println(">> " + a);
    System.out.println(">> " + b);
    System.out.println(">> " + c);
  }

}

The output shows the use of the mixed case values instead of the uppercase, showing my strings are being used:

>> Alpha
>> Beta
>> Gamma
>> Alpha
>> Beta
>> Gamma

Note that I am using the Google Guava library so I can take advantage of the BiMap.

like image 146
Steve J Avatar answered Jun 08 '26 23:06

Steve J


you can try a simple substitution for turn the string into an enum

switch(MyEnum.valueOf(text.replace(' ', '_')) {
    case load_strings:

You can use toUpperCase() if you want it in upper case.

You should do what you think is the simplest and clearest.

like image 41
Peter Lawrey Avatar answered Jun 08 '26 22:06

Peter Lawrey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!