处理智能方式的条件

时间:2017-07-18 10:21:41

标签: java

if (lineStyle == 5 || lineStyle == 21 || lineStyle == 82 || lineStyle == 83 || lineStyle == 3) {
    lineStyleString = "DOUBLE";
} else if (lineStyle == 6 || lineStyle == 35 || lineStyle == 39 || lineStyle == 30) {
    lineStyleString = "DOTTED" ;
} else if (lineStyle == 26 || lineStyle == 27  || lineStyle == 28  || lineStyle == 29 || lineStyle == 1) {
    lineStyleString = "SOLID";
} else if(lineStyle == -1) {
    lineStyleString = "NONE";
}

我们如何在Java中以智能方式处理此代码?切换案例,枚举或密钥对值模式?

9 个答案:

答案 0 :(得分:6)

您的条件看起来更随机。

Switch在这里看起来不错

switch(lineStyle) {
    case 5:
    case 21:
    case 82:
    case 83:
    case 3: 
     lineStyleString = "DOUBLE";   
     break;
    .. // add more cases
}

或者我更喜欢创建实用工具方法

public static boolean contains(int expecxted, int... vals) {
        for (int i = 0; i < vals.length; i++) {
            if (expecxted == vals[i]) {
                return true;
            }
        }
        return false;
    }

你可以像

一样使用它
if (contains(lineStyle, 5,21,82,83,3)) {
    lineStyleString = "DOUBLE";
} else if(contains(lineStyle,6,35,39,30)){
   lineStyleString = "DOTTED";
}

答案 1 :(得分:3)

一个良好缩进的开关盒将采用 30 行(Netbeans建议自行转换它以便我可以计算)

所以我认为这种方式更好( 9 行):

if (Arrays.asList(5, 21, 82, 83, 3).contains(lineStyle)) {
     lineStyleString = "DOUBLE";
} else if (Arrays.asList(6, 35, 39, 30).contains(lineStyle)) {
     lineStyleString = "DOTTED";
} else if (Arrays.asList(26, 27, 28, 29, 1).contains(lineStyle)) {
     lineStyleString = "SOLID";
}else if (lineStyle == -1) {
     lineStyleString = "NONE";
}

答案 2 :(得分:2)

我更喜欢这样的枚举:

enum LineStyle {

  DOUBLE(3, 5, 21, 82, 83),
  DOTTED(6, 30, 35, 39),
  SOLID(1, 26, 27, 28, 29),
  NONE(-1);

  private final Set<Integer> types;

  private LineStyle(Integer... types) {
    this.types = Stream.of(types).collect(Collectors.toSet());
  }

  public static LineStyle of(int lineStyle) {
    return Stream.of(LineStyle.values())
      .filter(ls -> ls.types.contains(lineStyle))
      .findFirst().orElse(null);
  }
}

然后您只需致电:LineStyle ls = LineStyle.of(lineStyle);

答案 3 :(得分:2)

您可以将条件提取到方法中,使其更具可读性。

private boolean isDoubleStyle(int lineStyle) {
    return lineStyle == 5 || lineStyle == 21 || lineStyle == 82 || lineStyle == 83 || lineStyle == 3;
}

private boolean isDottedStyle(int lineStyle) {
    return lineStyle == 6 || lineStyle == 35 || lineStyle == 39 || lineStyle == 30;
}

private boolean isSolidStyle(int lineStyle) {
    return lineStyle == 26 || lineStyle == 27  || lineStyle == 28  || lineStyle == 29 || lineStyle == 1;
}

然后调用方法

if (isDoubleStyle(lineStyle)) {
    lineStyleString = "DOUBLE";
} else if (isDottedStyle(lineStyle)) {
    lineStyleString = "DOTTED" ;
} else if (isSolidStyle(lineStyle)) {
    lineStyleString = "SOLID";
} else {
    lineStyleString = "NONE";
}

我删除了linestyle == -1的最终检查,以确保lineStyleString始终具有值,无论如何。

答案 4 :(得分:2)

您可以预先填充Map<Integer, String>并将其保存在某处,然后使用它来确定您的值而无需进行条件检查。像,

Map<Integer, String> valueMap = new HashMap<>();
Stream.of(5, 21, 82, 83, 3).forEach(x -> valueMap.put(x, "DOUBLE"));
Stream.of(6, 35, 39, 30).forEach(x -> valueMap.put(x, "DOTTED"));
Stream.of(26, 27, 28, 29, 1).forEach(x -> valueMap.put(x, "SOLID"));
valueMap.put(-1, "NONE");

然后再

String lineStyleString = valueMap.get(lineStyle);

答案 5 :(得分:0)

你可以这样做:

switch (lineStyle){
    case 5:
    case 21:
    case 82:
    case 83:
    case 3:
        lineStyleString = "DOUBLE";
        break;
    ...

答案 6 :(得分:-1)

我认为最好使用switch语句。

int lcase = lineStyle;
String lineStyleString = null;
switch(lcase)
{
    case 3 :
    case 5 :
    case 21:
    case 82:
    case 83:
        lineStyleString = "DOUBLE";
        break;
    case 6 :
    case 30:
    case 35:
    case 39:
        lineStyleString = "DOTTED";
        break;
    case 1 :
    case 26:
    case 27:
    case 28:
    case 29:
        lineStyleString = "SOLID";
        break;
    case -1:
        lineStyleString = "NONE";
        break;         
}

答案 7 :(得分:-1)

不知道这是否是聪明的方式,但它更具可读性和简短性:

lineStyleString = (lineStyle == 5 || lineStyle == 21 || lineStyle == 82 || lineStyle == 83 || lineStyle == 3)? "DOUBLE"
                    :(lineStyle == 6 || lineStyle == 35 || lineStyle == 39 || lineStyle == 30)? "DOTTED"
                    :(lineStyle == 26 || lineStyle == 27  || lineStyle == 28  || lineStyle == 29 || lineStyle == 1)? "SOLID"
                    :"NONE";

或使用IntStream

   lineStyleString = (IntStream.of(5,21,82,83,3).anyMatch(x -> x == lineStyle)) ? "DOUBLE"
                    :(IntStream.of(6,35,39,30).anyMatch(x -> x == lineStyle))   ? "DOTTED"
                    :(IntStream.of(26,27,28,29,1).anyMatch(x -> x == lineStyle))? "SOLID"
                    :"NONE";

答案 8 :(得分:-2)

通过一些微不足道的重新格式化它很好:

int s = lineStyle;

if (s == 5 || s == 21 || s == 82 || s == 83 || s == 3) {
    lineStyleString = "DOUBLE";

} else if (s == 6 || s == 35 || s == 39 || s == 30) {
    lineStyleString = "DOTTED";

} else if (s == 26 || s == 27  || s == 28  || s == 29 || s == 1) {
    lineStyleString = "SOLID";

} else if (s == -1) {
    lineStyleString = "NONE";
}