Skip to content

Instantly share code, notes, and snippets.

@saurabh-sp-tripathi
Created August 14, 2018 20:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saurabh-sp-tripathi/e3e372b050b383e0956b308c0a46a197 to your computer and use it in GitHub Desktop.
Save saurabh-sp-tripathi/e3e372b050b383e0956b308c0a46a197 to your computer and use it in GitHub Desktop.
a temp gist, this code is used in testng to create combination of parameter in dataProvider
public static class ParamSetListBuilder {
private List<List> paramList = new ArrayList<>();
private Logger logger = LoggerFactory.getLogger(getClass());
public ParamSetListBuilder addParams(List paramListToCombine) {
addParamsAtIndex(paramListToCombine, Helper.calcParamIndex(paramList));
return this;
}
public ParamSetListBuilder addParamsAtIndex(List paramListToCombine, int index) {
logger.info(paramListToCombine.toString());
if(paramList.isEmpty()) {
paramList = Helper.makeListOfListOfSignleElement(paramListToCombine);
} else {
paramList = Helper.combineListOfList(paramList, paramListToCombine, index);
}
return this;
}
public List<List> get() {
return paramList;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("paramList", paramList)
.toString();
}
/**
* Helper to have pure function, which are easy to understand and test
*/
public static class Helper {
public static int calcParamIndex(List<List> listOfList) {
if(listOfList.isEmpty()) {
return 0;
} else {
return listOfList.get(0).size();
}
}
public static List clone(List list) {
return (List)((ArrayList)list).clone();
}
public static List<List> combine(List a, List b, int index) {
List<List> listOfList = new ArrayList<>(b.size());
for(Object o : b) {
List tempList = clone(a);
tempList.add(index, o);
listOfList.add(tempList);
}
return listOfList;
}
public static List<List> combineListOfList(List<List> listOfList, List listToCombine, int index) {
List<List> combinedListOfList = new ArrayList<>();
for(List l : listOfList) {
combinedListOfList.addAll(combine(l, listToCombine, index));
}
return combinedListOfList;
}
public static List<List> makeListOfListOfSignleElement(List list) {
List<List> listOfLists = new ArrayList<>(list.size());
for(Object o : list) {
List temp = new ArrayList();
temp.add(o);
listOfLists.add(temp);
}
return listOfLists;
}
}
}
@Test
public void test() {
List<String> xList = Stream
.of("x", "y", "z")
.collect(Collectors.toCollection(ArrayList::new));
ParamSetListBuilder paramSetListBuilder = new ParamSetListBuilder();
paramSetListBuilder.addParamsAtIndex(xList, 0);
logger.info(paramSetListBuilder.get().toString());
assertEquals(xList.size(), paramSetListBuilder.get().size());
List<String> aList = Stream
.of("a", "b", "c")
.collect(Collectors.toCollection(ArrayList::new));
paramSetListBuilder.addParams(aList);
logger.info(paramSetListBuilder.get().toString());
List<String> dList = Stream
.of("1", "2", "3", "4")
.collect(Collectors.toCollection(ArrayList::new));
paramSetListBuilder.addParams(dList);
logger.info(paramSetListBuilder.get().toString());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment