001package com.ganteater.ae.processor; 002 003import java.io.IOException; 004import java.util.ArrayList; 005import java.util.List; 006 007import org.apache.commons.lang.StringUtils; 008 009public class TestIterator { 010 private int fCurrentIteration; 011 012 private int fIterationCount; 013 private List<ParameterIterator> fParameterIteratorArray = new ArrayList<>(); 014 015 private String fType = ParameterIterator.SINGLE; 016 private List<String> fNamePropertiesIsSet = new ArrayList<>(); 017 018 public TestIterator(Processor processor, String rules) throws IOException { 019 fIterationCount = 0; 020 021 int multipleCount = 1; 022 ParameterIterator theCurrParameterIterator = null; 023 boolean singlePresent = false; 024 025 String[] lines = rules.split("\n"); 026 for (String theLine : lines) { 027 if (theLine.charAt(0) == '#') 028 continue; 029 030 int thePbrkEndName = StringUtils.indexOfAny(theLine, "$="); 031 032 if (thePbrkEndName < 0) { 033 theLine = theLine.trim(); 034 035 if (ParameterIterator.INDEPENDENT.equalsIgnoreCase(theLine) 036 || ParameterIterator.CONCURRENTLY.equalsIgnoreCase(theLine) 037 || ParameterIterator.MULTIPLE.equalsIgnoreCase(theLine) 038 || ParameterIterator.SINGLE.equalsIgnoreCase(theLine)) { 039 if (multipleCount > 1) 040 fIterationCount += multipleCount; 041 multipleCount = 1; 042 theCurrParameterIterator = null; 043 fType = theLine.toLowerCase(); 044 } else if (!theLine.isEmpty()) { 045 throw new IllegalArgumentException( 046 "Incorrect line in itteration rule file, is absent ':' in line: " + theLine); 047 } 048 049 } else { 050 051 String theName = theLine.substring(0, thePbrkEndName).trim(); 052 String theRules = theLine.substring(thePbrkEndName + 1).trim(); 053 054 ParameterIterator theParameterIterator = new ParameterIterator(processor, theName, fIterationCount); 055 056 int thePbrkEndRule = theRules.indexOf('='); 057 058 String theRuleName = theRules; 059 String theRuleParameters = null; 060 061 if (thePbrkEndRule >= 0) { 062 theRuleName = theRules.substring(0, thePbrkEndRule).trim(); 063 theRuleParameters = theRules.substring(thePbrkEndRule + 1); 064 theParameterIterator.addRule(theRuleName, theRuleParameters); 065 } else { 066 theParameterIterator.addRule("value", theRules); 067 } 068 069 if (ParameterIterator.SINGLE.equals(fType)) { 070 if (theParameterIterator.count() > 1) { 071 fIterationCount += theParameterIterator.count() - 1; 072 } 073 singlePresent = true; 074 theParameterIterator.setType(ParameterIterator.SINGLE); 075 } else if (ParameterIterator.MULTIPLE.equals(fType)) { 076 if (theCurrParameterIterator != null) { 077 theCurrParameterIterator.setChield(theParameterIterator); 078 } else { 079 theParameterIterator.setParent(true); 080 } 081 082 multipleCount *= theParameterIterator.count(); 083 theCurrParameterIterator = theParameterIterator; 084 theParameterIterator.setType(ParameterIterator.MULTIPLE); 085 } else if (ParameterIterator.CONCURRENTLY.equals(fType)) { 086 if (theCurrParameterIterator != null) { 087 theCurrParameterIterator.setChield(theParameterIterator); 088 } else { 089 theParameterIterator.setParent(true); 090 fIterationCount += theParameterIterator.count(); 091 } 092 093 theCurrParameterIterator = theParameterIterator; 094 theParameterIterator.setType(ParameterIterator.CONCURRENTLY); 095 } else if (ParameterIterator.INDEPENDENT.equals(fType)) { 096 theParameterIterator.setType(ParameterIterator.INDEPENDENT); 097 } 098 099 fParameterIteratorArray.add(theParameterIterator); 100 } 101 102 if (multipleCount > 1 103 || (ParameterIterator.MULTIPLE.equals(fType) && theCurrParameterIterator != null)) { 104 fIterationCount += multipleCount; 105 } 106 107 if (singlePresent) { 108 fIterationCount++; 109 } 110 } 111 } 112 113 public int count() { 114 return fIterationCount; 115 } 116 117 public void nextIteration(Processor taskProcessor) { 118 119 fNamePropertiesIsSet.clear(); 120 121 for (int i = 0; i < fParameterIteratorArray.size(); i++) { 122 ParameterIterator theParameterIterator = fParameterIteratorArray.get(i); 123 theParameterIterator.applyValue(fCurrentIteration, taskProcessor, fNamePropertiesIsSet); 124 } 125 126 fCurrentIteration++; 127 } 128 129 public String[] getPropertiesList() { 130 List<String> theArrayList = new ArrayList<>(); 131 132 for (int i = 0; i < fParameterIteratorArray.size(); i++) { 133 ParameterIterator theElem = (ParameterIterator) fParameterIteratorArray.get(i); 134 String name = theElem.getName(); 135 if (theArrayList.contains(name) == false) { 136 theArrayList.add(name); 137 } 138 } 139 140 String[] theReturn = new String[theArrayList.size()]; 141 142 for (int i = 0; i < theArrayList.size(); i++) { 143 theReturn[i] = (String) theArrayList.get(i); 144 } 145 146 return theReturn; 147 } 148}