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.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
public class Generics { public class Generics {
@ -6,19 +7,24 @@ public class Generics {
// definition of a generic method // definition of a generic method
public static <T> List<T> fromArrayToList(T[] a) { public static <T> List<T> fromArrayToList(T[] a) {
List<T> list = new ArrayList<>(); List<T> list = new ArrayList<>();
for (T t : a) { Arrays.stream(a).forEach(list::add);
list.add(t);
}
return list; return list;
} }
// example of a generic method that has Number as an upper bound for T // example of a generic method that has Number as an upper bound for T
public static <T extends Number> List<T> fromArrayToListWithUpperBound(T[] a) { public static <T extends Number> List<T> fromArrayToListWithUpperBound(T[] a) {
List<T> list = new ArrayList<>(); List<T> list = new ArrayList<>();
for (T t : a) { Arrays.stream(a).forEach(list::add);
list.add(t);
}
return list; 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 java.util.List;
import org.junit.Test; import org.junit.Test;
@ -7,40 +11,45 @@ public class GenericsTest {
// testing the generic method with Integer // testing the generic method with Integer
@Test @Test
public void fromArrayToListIntTest() { public void givenArrayOfIntegers_thanListOfIntegersReturnedOK() {
Integer[] intArray = { 1, 2, 3, 4, 5 }; Integer[] intArray = { 1, 2, 3, 4, 5 };
List<Integer> list = Generics.fromArrayToList(intArray); List<Integer> list = Generics.fromArrayToList(intArray);
Iterator<Integer> iterator;
iterator = list.iterator(); assertThat(list, hasItems(intArray));
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
} }
// testing the generic method with String // testing the generic method with String
@Test @Test
public void fromArrayToListStringTest() { public void givenArrayOfStrings_thanListOfStringsReturnedOK() {
String[] stringArray = { "hello1", "hello2", "hello3", "hello4", "hello5" }; String[] stringArray = { "hello1", "hello2", "hello3", "hello4", "hello5" };
List<String> list = Generics.fromArrayToList(stringArray); List<String> list = Generics.fromArrayToList(stringArray);
Iterator<String> iterator;
iterator = list.iterator(); assertThat(list, hasItems(stringArray));
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
} }
// testing the generic method with Number as upper bound with Integer // testing the generic method with Number as upper bound with Integer
// if we test fromArrayToListWithUpperBound with any type that doesn't // if we test fromArrayToListWithUpperBound with any type that doesn't
// extend Number it will fail to compile // extend Number it will fail to compile
@Test @Test
public void fromArrayToListUpperboundIntTest() { public void givenArrayOfIntegersAndNumberUpperBound_thanListOfIntegersReturnedOK() {
Integer[] intArray = { 1, 2, 3, 4, 5 }; Integer[] intArray = { 1, 2, 3, 4, 5 };
List<Integer> list = Generics.fromArrayToListWithUpperBound(intArray); List<Integer> list = Generics.fromArrayToListWithUpperBound(intArray);
Iterator<Integer> iterator;
iterator = list.iterator(); assertThat(list, hasItems(intArray));
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
} }
} // 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);
}
}