Home > Mobile >  alternative for switch case constants in java
alternative for switch case constants in java

Time:01-13

I have this switch case and I am wondering if there is any way to replace the constant values with enums. I tried to define an enum file and used it instead of the constant value but it's not recognized.

switch (item.getToString()) {
              case "XS":
                estimatedTimes.add(3.5);
                break;
              case "S":
                estimatedTimes.add(10.5);
                break;
              case "M":
                estimatedTimes.add(17.5);
                break;
              case "L":
                estimatedTimes.add(28.0);
                break;
              case "XL":
                estimatedTimes.add(45.5);
                break;
            }

Here is my enum, I used TShirtSize.XS.getValue() instead of "XS". But it doesn't work.

public enum TShirtSize {

  XS("XS"), S("S"), M("M"), L("L"), XL("XL");

  private final String value;

  JiraTShirtSize(String value) {
    this.value = value;
  }

  public String getValue() {
    return value;
  }

}

EDIT: the Item class is:

  @Getter
  @Setter
  public static class Item {

    private String fromString;
    private String toString;
  }

Also changed code after adding enum to case:

  switch (item.getToString()) {
                  case TShirtSize.XS.getValue():
                    estimatedTimes.add(3.5);
                    break;
                  //////.... 
                }

CodePudding user response:

You can use enum values as case labels:

enum TShirtSize {
    XS, S, M, L, XL;
}
public class Test2 {
    public static void main(String[] args) {
        TShirtSize size = TShirtSize.valueOf("S");

        double factor = switch (size) {
            case XS -> 3.5;
            case S -> 10.5;
            case L -> 28.0;
            case XL -> 45.5;
            case M -> 17.5;
        };
        // estimatedTimes.add(factor);
    }
}

But why not add the number as an attribute of the enum and remove the switch alltogether?

enum TShirtSize {
    XS(3.5), S(10.5), M(17.5), L(28.0), XL(45.5);

    private final double factor;

    TShirtSize(double factor) {
        this.factor = factor;
    }

    public double getFactor() {
        return factor;
    }
}
public class Test2 {
    public static void main(String[] args) {
        TShirtSize size = TShirtSize.valueOf("S");
        double factor = size.getFactor();

        // estimatedTimes.add(factor);
    }
}

In the context of your code:

TShirtSize size = TShirtSize.valueOf(item.getToString());
estimatedTimes.add(size.getFactor());

CodePudding user response:

You can use Enum in the switch operator. No need to convert it to the string value.

@lombok.Getter
@lombok.RequiredArgsConstructor(access = AccessLevel.PACKAGE)
public enum TShirtSize {

  XS("XS"),
  S("S"),
  M("M"),
  L("L"),
  XL("XL");

  private final String value;

}

public void foo(TShirtSize tshirtSize) {
    switch (tshirtSize) {
        case TShirtSize.XS: estimatedTimes.add(3.5);    break;
        case TShirtSize.S:  estimatedTimes.add(10.5);   break;
        case TShirtSize.M:  estimatedTimes.add(17.5);   break;
        case TShirtSize.L:  estimatedTimes.add(28.5);   break;
        case TShirtSize.XL: estimatedTimes.add(45.5);   break;
        default: break;
    }
}

As alternative, you can encapsulat logic directly to the Enum and you do not need to write switch operator at all

@lombok.Getter
@lombok.RequiredArgsConstructor(access = AccessLevel.PACKAGE)
public enum TShirtSize {

  XS("XS", 3.5),
  S("S", 10.5),
  M("M", 17.5),
  L("L", 28.5),
  XL("XL", 45.5);

  private final String value;
  private final double estimatedTimes;

}

public void foo(TShirtSize tshirtSize) {
    estimatedTimes.add(tshirtSize.getEstimatedTImes());
}

P.S. Do not forget that you can convert string value to the Enum instance.

String value = "XS";
TShirtSize tshirtSize = TShirtSize.valueOf(value);
  • Related