Skip to content

Commit b18e801

Browse files
committed
added more example
1 parent 6835625 commit b18e801

4 files changed

Lines changed: 51 additions & 27 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
public class Building {
3+
public void paint() {
4+
}
5+
}
Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
11
import java.util.ArrayList;
2+
import java.util.Arrays;
23
import java.util.List;
34

45
public class Generics {
56

67
// definition of a generic method
78
public static <T> List<T> fromArrayToList(T[] a) {
89
List<T> list = new ArrayList<>();
9-
for (T t : a) {
10-
list.add(t);
11-
}
10+
Arrays.stream(a).forEach(list::add);
1211
return list;
1312
}
1413

1514
// example of a generic method that has Number as an upper bound for T
1615
public static <T extends Number> List<T> fromArrayToListWithUpperBound(T[] a) {
1716
List<T> list = new ArrayList<>();
18-
for (T t : a) {
19-
list.add(t);
20-
}
17+
Arrays.stream(a).forEach(list::add);
2118
return list;
2219
}
2320

24-
}
21+
// example of a generic method with a wild card, this method can be used
22+
// with a list of any subtype of Building
23+
public static boolean paintAllBuildings(List<? extends Building> buildings) {
24+
for (Building building : buildings) {
25+
building.paint();
26+
}
27+
return true;
28+
}
29+
30+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
public class SubBuilding extends Building {
3+
4+
}
Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import java.util.Iterator;
1+
import static org.hamcrest.CoreMatchers.hasItems;
2+
import static org.hamcrest.MatcherAssert.assertThat;
3+
import static org.junit.Assert.assertTrue;
4+
5+
import java.util.ArrayList;
26
import java.util.List;
37

48
import org.junit.Test;
@@ -7,40 +11,45 @@ public class GenericsTest {
711

812
// testing the generic method with Integer
913
@Test
10-
public void fromArrayToListIntTest() {
14+
public void givenArrayOfIntegers_thanListOfIntegersReturnedOK() {
1115
Integer[] intArray = { 1, 2, 3, 4, 5 };
1216
List<Integer> list = Generics.fromArrayToList(intArray);
13-
Iterator<Integer> iterator;
14-
iterator = list.iterator();
15-
while (iterator.hasNext()) {
16-
System.out.println(iterator.next());
17-
}
17+
18+
assertThat(list, hasItems(intArray));
1819
}
1920

2021
// testing the generic method with String
2122
@Test
22-
public void fromArrayToListStringTest() {
23+
public void givenArrayOfStrings_thanListOfStringsReturnedOK() {
2324
String[] stringArray = { "hello1", "hello2", "hello3", "hello4", "hello5" };
2425
List<String> list = Generics.fromArrayToList(stringArray);
25-
Iterator<String> iterator;
26-
iterator = list.iterator();
27-
while (iterator.hasNext()) {
28-
System.out.println(iterator.next());
29-
}
26+
27+
assertThat(list, hasItems(stringArray));
3028
}
3129

3230
// testing the generic method with Number as upper bound with Integer
3331
// if we test fromArrayToListWithUpperBound with any type that doesn't
3432
// extend Number it will fail to compile
3533
@Test
36-
public void fromArrayToListUpperboundIntTest() {
34+
public void givenArrayOfIntegersAndNumberUpperBound_thanListOfIntegersReturnedOK() {
3735
Integer[] intArray = { 1, 2, 3, 4, 5 };
3836
List<Integer> list = Generics.fromArrayToListWithUpperBound(intArray);
39-
Iterator<Integer> iterator;
40-
iterator = list.iterator();
41-
while (iterator.hasNext()) {
42-
System.out.println(iterator.next());
43-
}
37+
38+
assertThat(list, hasItems(intArray));
39+
}
40+
41+
// testing paintAllBuildings method with a subtype of Building, the method
42+
// will work with all subtypes of Building
43+
@Test
44+
public void givenSubTypeOfAwildCardBoundedGenericMethod() {
45+
46+
List<SubBuilding> subBuildingsList = new ArrayList<>();
47+
subBuildingsList.add(new SubBuilding());
48+
subBuildingsList.add(new SubBuilding());
49+
50+
boolean result = Generics.paintAllBuildings(subBuildingsList);
51+
assertTrue(result);
52+
4453
}
4554

46-
}
55+
}

0 commit comments

Comments
 (0)