我可以让每个bean成员的代码更漂亮吗?

时间:2016-12-22 08:18:28

标签: java

我有一个类:数据,一个calss:报警。  我必须统计数据然后输出excel使用npoi.Alarm可能为null。 现在是我的代码:

 while (it.hasNext()){       
             Data t =  it.next(); 
             row = sheet.createRow(index++);
             Alarm a=alarms.get(t.getDeviceid().trim());
              cell = row.createCell(0);
             if(a==null||inRange(t.getNoise(),a.getNoiselow(),a.getNoiseup()))
                     cell.setCellStyle(styleNormal);
             else
                 cell.setCellStyle(styleError);
             cell.setCellValue((double)t.getNoise());
             cell = row.createCell(1);
             if(a==null||inRange(t.getPm(),a.getPmlow(),a.getPmup()))
                     cell.setCellStyle(styleNormal);
             else
                 cell.setCellStyle(styleError);
             cell.setCellValue((double)t.getPm());
             cell = row.createCell(2);
             if(a==null||inRange(t.getPressure(),a.getPressurelow(),a.getPressureup()))
                     cell.setCellStyle(styleNormal);
             else
                 cell.setCellStyle(styleError);
             cell.setCellValue((double)t.getPressure());
        ....

我重复每个bean属性....我可以改进这样的代码吗?

2 个答案:

答案 0 :(得分:1)

最简单的启动方式:查找重复的代码并将其放入方法中。

  cell = row.createCell(0);
  if(a==null||inRange(t.getNoise(),a.getNoiselow(),a.getNoiseup()))
      cell.setCellStyle(styleNormal);
  else
      cell.setCellStyle(styleError);
  cell.setCellValue((double)t.getNoise());

您可以将上面的代码放在方法中并从while循环中调用:

 private void setCell(Row row, Data data, int cellIndex)
 {
      cell = row.createCell(cellIndex);
      Alarm a=alarms.get(data.getDeviceid().trim());
      if(a==null||inRange(data, a, cellIndex))
          cell.setCellStyle(styleNormal);
      else
          cell.setCellStyle(styleError);

      // You can also do as below if you can read it easily!!
      // cell.setCellStyle(a==null||inRange(data, a, cellIndex)?styleNormal:styleError);

      // This switch-case should also be moved to separate method.
      // Leaving that to you. 
      switch(cellIndex)
      {
          case 0:  cell.setCellValue((double)data.getNoise()); break;
          case 1:  cell.setCellValue((double)t.getPm()); break;
          ...
          ...
          default: /*set default value and break or throw InvalidCellIndex exception*/
      }
 }

 private boolean inRange(Date data, Alarm a, int cellIndex)
 {
      switch(cellIndex)
      {
          case 0:  return inRange(data.getNoise(),a.getNoiselow(),a.getNoiseup();
          case 1:  return inRange(data.getPm(),a.getPmlow(),a.getPmup();
          ...
          ...
          default: /* throw InvalidCellIndex exception*/
      }
 }

答案 1 :(得分:0)

我同意@Azodious重复代码应该在方法中抽象,但我会稍微区别对待:

while (it.hasNext()){       
    Data t =  it.next(); 
    row = sheet.createRow(index++);
    Alarm a=alarms.get(t.getDeviceid().trim());
    setNoise(row, 0, t, a);
    setPm(row, 1, t, a);
    ...
}

private void setCellValue(Row row, int cellIndex, double value, boolean error) {
    Cell cell = row.createCell(cellIndex);
    if (error) {
        cell.setCellStyle(styleError);
    } else {
        cell.setCellStyle(styleNormal);
    }
    cell.setCellValue(value);
}

private void setNoise(Row row, int cellIndex, Data t, Alarm a) {
    setCellValue(row, cellIndex, data.getNoise(),
        a == null || inRange(t.getNoise(),a.getNoiselow(),a.getNoiseup()));
}

...