在JAVA Generic方法中合并多个for循环

时间:2018-03-26 08:34:10

标签: java generics generic-programming generic-list

我目前正在修改一个高级项目,但我仍然是JAVA的新手。我试图搜索有关泛型方法和应用的大量信息,但它们似乎是不正确的。请帮助我,看看并纠正我错在哪里。谢谢。 =)

下面是我尝试用于合并for循环的泛型方法的代码。

public String[] name = {
 "SDLManPower sdlManPower",
 "SDLScopeOfWork sdlSOW",
 "SDLMaterial sdlMat",
 "SDLUtility sdlUtil",
 "SDLTMEquip sdlTMEquip"
};

public String[] sname = {
 "sdlManPower",
 "sdlSOW",
 "sdlMat",
 "sdlUtil",
 "sdlTMEquip"
};

public String[] list = {
 "getAttendanceList()",
 "getSowList()",
 "SDLMaterial sdlMat",
 "getUtilityList()",
 "getEquipmentList()"
};

public String[] forlp = {
 "(SDLManPower manPower : sdlManPowerList",
 "SDLScopeOfWork sow : sdlSOWList",
 "SDLMaterial projectMat : sdlMaterialList",
 "SDLUtility utility : sdlUtilityList",
 "SDLTMEquip sdlEquipment : sdlEquipmentList"
};

public String[] id = {
 "getUserUID()",
 "getSubTreatmentID()",
 "getPurposeID()",
 "getUtilityID()",
 "getSubEquipmentID()"
};

public String[] fname = {
 "manPower",
 "sow",
 "projectMat",
 "utility",
 "sdlEquipment"
};


public static < T > void AllList(T[] name, T[] sname, T[] list, T[] forlp, T[] id, T[] fname) {

 for (T x: name) {
  for (name[x]: sdlReportObj.list[x]) {
   Boolean alreadyExist = true;
   for (forlp[x]) {
    if (sname[x].id[x] == fname[x].id[x] && sname[x].getProjectTeamUID().equals(fname[x].getProjectTeamUID())) {
     alreadyExist = true;
     break;
    } else {
     alreadyExist = false;
    }
   }
   if (!alreadyExist) {

    UserGroup user = null;
    Vendor v = null;
    ProjectArrangement[] arrangementList = projectInfo.getProjPlan().getArrangementList();
    for (ProjectArrangement projArrangement: arrangementList) {
     if (projArrangement.getuID().equals(sname[x].getProjectTeamUID())) {

      if (projArrangement.getUserGroupUID() != null) {
       user = userList.getUserGroupCollection().findUserGroup(projArrangement.getUserGroupUID());
      } else {
       v = (Vendor) maintData.getVendorCollection().findItem(projArrangement.getVendorID());
      }
     }
    }

    sname[x].setTeamName(user != null ? user.getRoleFullName() : v.getName());
    sname[x].setUserGroupID(user != null ? user.getRoleUID() : v.getId() + "");
    sname[x].setDeletable(true);
    sname[x].setValue(0.00);
    sname[x].setPreLoad(false);
    sdlEquipmentList.add(sdlTMEquip);
   }
  }
 } //for name
}

以下代码是高级工作。他使用许多for循环来做类似的动作。 所以我想使用通用方法通过将所有for循环合并为一个来减少系统加载时间。但我失败了。 =(

if (sdlReportObj != null && sdlReportObj.getProjectUID().equals(projectInfo.getProjectUID())) {
 for (SDLExpenses sdlExpenses: sdlReportObj.getExpensesList()) {
  Boolean alreadyExist = true;
  for (ProjectExpenses projExpenses: projectInfo.getProjPlan().getExpensesList()) {
   if (projExpenses.getExpensesItemID() == sdlExpenses.getSubExpensesItemID()) {
    alreadyExist = true;
    break;
   } else {
    alreadyExist = false;
   }
  }
  if (!alreadyExist || projectInfo.getProjPlan().getExpensesList().length == 0) {

   sdlExpenses.setDeletable(true);
   sdlExpenses.setValue(0.00);
   sdlExpenses.setPreLoad(false);
   sdlExpensesList.add(sdlExpenses);
  }
 }

 for (SDLManPower sdlManPower: sdlReportObj.getAttendanceList()) {
  Boolean alreadyExist = true;
  for (SDLManPower manPower: sdlManPowerList) {
   if (manPower.getUserUID().equals(sdlManPower.getUserUID()) && sdlManPower.getProjectTeamUID().equals(manPower.getProjectTeamUID())) {
    alreadyExist = true;
    break;
   } else {
    alreadyExist = false;
   }
  }
  if (!alreadyExist) {
   // tempManPower.setDeletable(false);
   UserGroup user = null;
   Vendor v = null;
   ProjectArrangement[] arrangementList = projectInfo.getProjPlan().getArrangementList();
   for (ProjectArrangement projArrangement: arrangementList) {
    if (projArrangement.getuID().equals(sdlManPower.getProjectTeamUID())) {

     if (projArrangement.getUserGroupUID() != null) {
      user = userList.getUserGroupCollection().findUserGroup(projArrangement.getUserGroupUID());
     } else {
      v = (Vendor) maintData.getVendorCollection().findItem(projArrangement.getVendorID());
     }
    }
   }

   sdlManPower.setTeamName(user != null ? user.getRoleFullName() : v.getName());
   sdlManPower.setUserGroupID(user != null ? user.getRoleUID() : v.getId() + "");
   sdlManPower.setStartTime(null);
   sdlManPower.setEndTime(null);
   sdlManPower.setPreLoad(false);
   sdlManPowerList.add(sdlManPower);
  }
 }


 ArrayList < SDLScopeOfWork > tempSOWList = new ArrayList < SDLScopeOfWork > ();
 for (SDLScopeOfWork sdlSOW: sdlReportObj.getSowList()) { //foreach sow in sdl
  Boolean alreadyExist = true;

  for (SDLScopeOfWork sow: sdlSOWList) {
   if (sdlSOW.getSubTreatmentID() == sow.getSubTreatmentID() && sdlSOW.getProjectTeamUID().equals(sow.getProjectTeamUID())) { //
    alreadyExist = true;
    break;
   } else {
    alreadyExist = false;
    tempSOWList.add(sow);
   }
  }

  if (!alreadyExist) {
   UserGroup user = null;
   Vendor v = null;
   ProjectArrangement[] arrangementList = projectInfo.getProjPlan().getArrangementList();
   for (ProjectArrangement projArrangement: arrangementList) {
    if (projArrangement.getuID().equals(sdlSOW.getProjectTeamUID())) {

     if (projArrangement.getUserGroupUID() != null) {
      user = userList.getUserGroupCollection().findUserGroup(projArrangement.getUserGroupUID());
     } else {
      v = (Vendor) maintData.getVendorCollection().findItem(projArrangement.getVendorID());
     }
    }
   }

   sdlSOW.setTeamName(user != null ? user.getRoleFullName() : v.getName());
   sdlSOW.setUserGroupID(user != null ? user.getRoleUID() : v.getId() + "");
   sdlSOW.setDeletable(true);
   sdlSOW.setFrom(0.00);
   sdlSOW.setTo(0.00);
   sdlSOW.setValue(0.00);
   sdlSOW.setPreLoad(false);

   sdlSOWList.add(sdlSOW);
  }
 }
 //sdlSOWList.addAll(tempSOWList);

 for (SDLMaterial sdlMat: sdlReportObj.SDLMaterial sdlMat) {
  Boolean alreadyExist = true;
  for (SDLMaterial projectMat: sdlMaterialList) {
   if (sdlMat.getPurposeID() == projectMat.getPurposeID() && sdlMat.getProjectTeamUID().equals(projectMat.getProjectTeamUID())) {
    alreadyExist = true;
    break;
   } else {
    alreadyExist = false;
   }
  }
  if (!alreadyExist) {
   UserGroup user = null;
   Vendor v = null;
   ProjectArrangement[] arrangementList = projectInfo.getProjPlan().getArrangementList();
   for (ProjectArrangement projArrangement: arrangementList) {
    if (projArrangement.getuID().equals(sdlMat.getProjectTeamUID())) {

     if (projArrangement.getUserGroupUID() != null) {
      user = userList.getUserGroupCollection().findUserGroup(projArrangement.getUserGroupUID());
     } else {
      v = (Vendor) maintData.getVendorCollection().findItem(projArrangement.getVendorID());
     }
    }
   }

   sdlMat.setTeamName(user != null ? user.getRoleFullName() : v.getName());
   sdlMat.setUserGroupID(user != null ? user.getRoleUID() : v.getId() + "");
   sdlMat.setDeletable(true);
   sdlMat.setValue(0.00);
   sdlMat.setPreLoad(false);

   sdlMaterialList.add(sdlMat);
  }
 }

 for (SDLUtility sdlUtil: sdlReportObj.getUtilityList()) { // a,b,d,c
  Boolean alreadyExist = true;

  for (SDLUtility utility: sdlUtilityList) { // a,b,c
   if (sdlUtil.getUtilityID() == utility.getUtilityID() && sdlUtil.getProjectTeamUID().equals(utility.getProjectTeamUID())) {
    alreadyExist = true;
    break;
   } else {
    alreadyExist = false;
   }
  }

  if (!alreadyExist) {
   UserGroup user = null;
   Vendor v = null;
   ProjectArrangement[] arrangementList = projectInfo.getProjPlan().getArrangementList();
   for (ProjectArrangement projArrangement: arrangementList) {
    if (projArrangement.getuID().equals(sdlUtil.getProjectTeamUID())) {

     if (projArrangement.getUserGroupUID() != null) {
      user = userList.getUserGroupCollection().findUserGroup(projArrangement.getUserGroupUID());
     } else {
      v = (Vendor) maintData.getVendorCollection().findItem(projArrangement.getVendorID());
     }
    }
   }

   sdlUtil.setTeamName(user != null ? user.getRoleFullName() : v.getName());
   sdlUtil.setUserGroupID(user != null ? user.getRoleUID() : v.getId() + "");
   sdlUtil.setDeletable(true);
   sdlUtil.setRegistrationNo(null);
   sdlUtil.setValue(0.00);
   sdlUtil.setPreLoad(false);
   sdlUtilityList.add(sdlUtil);
  }
 }

 // tm equip
 for (SDLTMEquip sdlTMEquip: sdlReportObj.getEquipmentList()) {
  Boolean alreadyExist = true;
  for (SDLTMEquip sdlEquipment: sdlEquipmentList) {
   if (sdlTMEquip.getSubEquipmentID() == sdlEquipment.getSubEquipmentID() && sdlTMEquip.getProjectTeamUID().equals(sdlEquipment.getProjectTeamUID())) {
    alreadyExist = true;
    break;
   } else {
    alreadyExist = false;
   }
  }
  if (!alreadyExist) {

   UserGroup user = null;
   Vendor v = null;
   ProjectArrangement[] arrangementList = projectInfo.getProjPlan().getArrangementList();
   for (ProjectArrangement projArrangement: arrangementList) {
    if (projArrangement.getuID().equals(sdlTMEquip.getProjectTeamUID())) {

     if (projArrangement.getUserGroupUID() != null) {
      user = userList.getUserGroupCollection().findUserGroup(projArrangement.getUserGroupUID());
     } else {
      v = (Vendor) maintData.getVendorCollection().findItem(projArrangement.getVendorID());
     }
    }
   }

   sdlTMEquip.setTeamName(user != null ? user.getRoleFullName() : v.getName());
   sdlTMEquip.setUserGroupID(user != null ? user.getRoleUID() : v.getId() + "");
   sdlTMEquip.setDeletable(true);
   sdlTMEquip.setValue(0.00);
   sdlTMEquip.setPreLoad(false);
   sdlEquipmentList.add(sdlTMEquip);
  }
 }

是否可以使用通用方法来减少加载时间和循环次数? 还有其他方法可以解决吗?谢谢。

1 个答案:

答案 0 :(得分:0)

好的,首先,看起来你的老人只是在ide中呕吐,并通过前一行的复制/粘贴做同样的事情。所以请不要复制他的作品,因为他称自己为大四学生。我说的是糟糕的经历。

所以是的,你可以像Generics那样重构一些思考。但这不是你问题的答案。答案是:只需将问题分成更小,更方便的问题。

SDLExpenses,SDLScopeOfWork,SDLMaterial,SDLUtility和SDLTMEquip应具有通用接口,将其命名为SDLIntf,您应该使用此接口实现5个新类。 使用抽象工厂(https://dzone.com/articles/design-patterns-abstract-factory)获取类的新实例,具体取决于您返回产品的类型。 I.E. sdlReportObj.SDLMaterial返回一个新的SDLMaterial对象。

每个类都有相同的方法,这些方法在SDLIntf中定义,我们称之为SortData(),这是你应该做的工作。如果你使用for循环,好吧。但是如果您使用Java 8,请尝试使用Lambda Expressions。有了它,每个for循环,如果可以用一行代码替换。但是如果你使用Java 5到7,坚持使用你的大四学生使用的foreach循环,它没有错。

解释完整的重构类会有太多的工作,但我的代码看起来像:

if (sdlReportObj != null && sdlReportObj.getProjectUID().equals(projectInfo.getProjectUID())) {
    SDLIntf mat = SDLFactory.GetProduct(sdlMat, projectInfo);
    SDLIntf sdlSOW = SDLFactory.GetProduct(sdlReportObj.getSowList(), projectInfo);

    mat.SortData();
    sdlSOW.SortData();
    ... etc......
}

所以最后你应该访问数据并用它做一些事情。但只有你知道它应该做什么。最好的是,你可以单元测试整个认为很容易。

是的,你可以在你的类中运行私有方法来处理更小的问题,比如在SDLScopeOfWork中,我看到至少有4种小方法可能会有更多可读代码。说实话,我甚至无法理解为什么他使用多个for循环。

快速思考我在Notepad ++中做过的事情(在这台机器上没有Java IDE;)) - &gt; https://pastebin.com/qkFHAb4J

也许这会给你正确的方向。