added more example

This commit is contained in:
Yasser Afifi 2016-12-02 23:31:10 +00:00
parent 6835625602
commit b18e801370
4 changed files with 51 additions and 27 deletions

View File

@ -0,0 +1,5 @@
public class Building {
public void paint() {
}
}

View File

@ -1,4 +1,5 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Generics {
@ -6,19 +7,24 @@ public class Generics {
// definition of a generic method
public static <T> List<T> fromArrayToList(T[] a) {
List<T> list = new ArrayList<>();
for (T t : a) {
list.add(t);
}
Arrays.stream(a).forEach(list::add);
return list;
}
// example of a generic method that has Number as an upper bound for T
public static <T extends Number> List<T> fromArrayToListWithUpperBound(T[] a) {
List<T> list = new ArrayList<>();
for (T t : a) {
list.add(t);
}
Arrays.stream(a).forEach(list::add);
return list;
}
// example of a generic method with a wild card, this method can be used
// with a list of any subtype of Building
public static boolean paintAllBuildings(List<? extends Building> buildings) {
for (Building building : buildings) {
building.paint();
}
return true;
}
}

View File

@ -0,0 +1,4 @@
public class SubBuilding extends Building {
}

View File

@ -1,4 +1,8 @@
import java.util.Iterator;
import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
@ -7,40 +11,45 @@ public class GenericsTest {
// testing the generic method with Integer
@Test
public void fromArrayToListIntTest() {
public void givenArrayOfIntegers_thanListOfIntegersReturnedOK() {
Integer[] intArray = { 1, 2, 3, 4, 5 };
List<Integer> list = Generics.fromArrayToList(intArray);
Iterator<Integer> iterator;
iterator = list.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
assertThat(list, hasItems(intArray));
}
// testing the generic method with String
@Test
public void fromArrayToListStringTest() {
public void givenArrayOfStrings_thanListOfStringsReturnedOK() {
String[] stringArray = { "hello1", "hello2", "hello3", "hello4", "hello5" };
List<String> list = Generics.fromArrayToList(stringArray);
Iterator<String> iterator;
iterator = list.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
assertThat(list, hasItems(stringArray));
}
// testing the generic method with Number as upper bound with Integer
// if we test fromArrayToListWithUpperBound with any type that doesn't
// extend Number it will fail to compile
@Test
public void fromArrayToListUpperboundIntTest() {
public void givenArrayOfIntegersAndNumberUpperBound_thanListOfIntegersReturnedOK() {
Integer[] intArray = { 1, 2, 3, 4, 5 };
List<Integer> list = Generics.fromArrayToListWithUpperBound(intArray);
Iterator<Integer> iterator;
iterator = list.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
assertThat(list, hasItems(intArray));
}
// testing paintAllBuildings method with a subtype of Building, the method
// will work with all subtypes of Building
@Test
public void givenSubTypeOfAwildCardBoundedGenericMethod() {
List<SubBuilding> subBuildingsList = new ArrayList<>();
subBuildingsList.add(new SubBuilding());
subBuildingsList.add(new SubBuilding());
boolean result = Generics.paintAllBuildings(subBuildingsList);
assertTrue(result);
}
}