定義一個callBack接口:用於實現回調過程
public interface callBack {
public void execute(List<NodeObj> nodeList, Collection<String> curList, List<NodeObj> tempNodes, int count);
}
控制流程實現callBack接口:
public class processController implements callBack {
File file = null;
int count = 0;
Collection<String> curList = null;
List<NodeObj> nodeList = null;
List<NodeObj> tempNodes = null;
callBack callBack ;
public processController() {
super();
}
public processController(File file){
this.file = file;
}
public void init(){
nodeList = new ArrayList<NodeObj>();
nodeList = XmlUtils.getNodes(file);
curList = new ArrayList<String>();
curList.add(nodeList.get(0).getAlg_id());
nodeList.remove(0);
count = nodeList.size()-1;
tempNodes = new ArrayList<NodeObj>();
callBack = new processController();
}
public void algProcess(){
if( count >= 0 ){
if(tempNodes!=null && nodeList!=null){
nodeList.removeAll(tempNodes);
tempNodes = new ArrayList<NodeObj>();
}
algExecute alg = new algExecute();
alg.setCallback(callBack);
alg.algExe(nodeList, curList, tempNodes, count);
}
}
@Override
public void execute(List<NodeObj> nodeList,Collection<String> curList, List<NodeObj> tempNodes, int count) {
this.nodeList = nodeList;
this.count = count;
this.curList = curList;
this.tempNodes = tempNodes;
algProcess();
}
}
主遍歷流程:
public class algExecute {
callBack callback = new processController();
public void setCallback(callBack callback){
this.callback = callback;
}
public void algExe(List<NodeObj> nodeList, Collection<String> curList, List<NodeObj> tempNodes, int count) {
for (NodeObj node : nodeList) {
String[] nodeArr = node.getPrior_node().split(",");
Collection<String> priorCol = Arrays.asList(nodeArr);
if (curList.containsAll(priorCol)) {
algExe.execute(node.getAlg_id() + "||" + node.getAlg_name());
curList.add(node.getAlg_id());
System.out.println("\t\t當前curList中的元素:" + curList.toString());
tempNodes.add(node);
count--;
}
}
callback.execute(nodeList,curList,tempNodes,count);
}
}
測試類:
public class TestDemo {
public static void main(String[] args) {
processController process = new processController(new File("./src/AlgProcess.xml"));
process.init();
process.algProcess();
}
}
測試樣例:
輸出結果: