Here we are writing a method that calls a test sub-object by passing three data generators (functions) to it.

Try 1


Wow, what a method. A blob of text.

protected void runTestsTry1()  {
	DynamicListTest test;
	Function<List<Integer>, DynamicList<Integer>> numsGenerator;
	Function<List<City>, DynamicList<City>> citiesGenerator;
	Function<List<String>, DynamicList<String>> stringsGenerator;
	numsGenerator = (javaList) -> {
		DynamicList<T> newList = LinkedListFactory.newLinkedList();
		for (T eachElem: javaList)
			newList.addLast(eachElem);
		return newList;
	};
	citiesGenerator = (javaList) -> {
		DynamicList<T> newList = LinkedListFactory.newLinkedList();
		for (T eachElem: javaList)
			newList.addLast(eachElem);
		return newList;
	};
	stringsGenerator = (javaList) -> {
		DynamicList<T> newList = LinkedListFactory.newLinkedList();
		for (T eachElem: javaList)
			newList.addLast(eachElem);
		return newList;
	};
	test = new DynamicListTest(numsGenerator, citiesGenerator, stringsGenerator);
}



Try 2 -- With Helper Method


Here, we've added a helper method. Our runTests method is now three simple lines of code.

And the helper method itself is not so complex, which gives us a fighting chance to understand the code.

protected void runTests()  {
	DynamicListTest test;
	//lg() is listGenerator
	test = new DynamicListTest(lg(), lg(), lg());
	test.run();
}

public static <T> Function<List<T>, DynamicList<T>> lg()  {
	//list generator
	return (javaList) -> {
		DynamicList<T> newList = LinkedListFactory.newLinkedList();
		for (T eachElem: javaList)
			newList.addLast(eachElem);
		return newList;
	};
}