Skip to content

Commit 9d3b8df

Browse files
BAEL-376 - changing method signatures
1 parent 7ca7c69 commit 9d3b8df

4 files changed

Lines changed: 48 additions & 27 deletions

File tree

‎core-java/src/main/java/com/baeldung/generics/Building.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ public class Building {
77
private static final Logger LOGGER = LoggerFactory.getLogger(Building.class);
88

99
public void paint() {
10-
LOGGER.info("Building");
10+
LOGGER.info("Painting Building");
1111
}
1212
}
Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,34 @@
11
package com.baeldung.generics;
22

3-
import java.util.ArrayList;
43
import java.util.Arrays;
54
import java.util.List;
5+
import java.util.function.Function;
66
import java.util.stream.Collectors;
77

88
public class Generics {
99

10-
// definition of a generic method
11-
public static <T> List<T> fromArrayToList(T[] a) {
12-
return Arrays.stream(a).collect(Collectors.toList());
13-
}
14-
15-
// example of a generic method that has Number as an upper bound for T
16-
public static <T extends Number> List<T> fromArrayToListWithUpperBound(T[] a) {
17-
return Arrays.stream(a).collect(Collectors.toList());
18-
}
19-
20-
// example of a generic method with a wild card, this method can be used
21-
// with a list of any subtype of Building
22-
public static boolean paintAllBuildings(List<? extends Building> buildings) {
23-
buildings.forEach(Building::paint);
24-
return true;
25-
}
10+
// definition of a generic method
11+
public static <T> List<T> fromArrayToList(T[] a) {
12+
return Arrays.stream(a).collect(Collectors.toList());
13+
}
14+
15+
// definition of a generic method
16+
public static <T, G> List<G> fromArrayToList(T[] a, List<G> list, Function<T, G> mapperFunction) {
17+
List<T> listWithTypeT = Arrays.stream(a).collect(Collectors.toList());
18+
return listWithTypeT.stream().map(mapperFunction)
19+
.collect(Collectors.toList());
20+
}
21+
22+
23+
// example of a generic method that has Number as an upper bound for T
24+
public static <T extends Number> List<T> fromArrayToListWithUpperBound(T[] a) {
25+
return Arrays.stream(a).collect(Collectors.toList());
26+
}
27+
28+
// example of a generic method with a wild card, this method can be used
29+
// with a list of any subtype of Building
30+
public static void paintAllBuildings(List<? extends Building> buildings) {
31+
buildings.forEach(Building::paint);
32+
}
2633

2734
}

‎core-java/src/main/java/com/baeldung/generics/House.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ public class House extends Building {
77
private static final Logger LOGGER = LoggerFactory.getLogger(House.class);
88

99
public void paint() {
10-
LOGGER.info("House");
10+
LOGGER.info("Painting House");
1111
}
1212
}

‎core-java/src/test/java/com/baeldung/generics/GenericsTest.java‎

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import static org.hamcrest.CoreMatchers.hasItems;
99
import static org.hamcrest.MatcherAssert.assertThat;
10-
import static org.junit.Assert.assertTrue;
10+
import static org.junit.Assert.fail;
1111

1212
public class GenericsTest {
1313

@@ -20,6 +20,15 @@ public void givenArrayOfIntegers_thanListOfIntegersReturnedOK() {
2020
assertThat(list, hasItems(intArray));
2121
}
2222

23+
// testing the generic method with Integer and String type
24+
@Test
25+
public void givenArrayOfIntegers_thanListOfStringReturnedOK() {
26+
Integer[] intArray = {1, 2, 3, 4, 5};
27+
List<String> stringList = new ArrayList<>();
28+
stringList = Generics.fromArrayToList(intArray, stringList, Object::toString);
29+
assertThat(stringList, hasItems("1", "2", "3", "4", "5"));
30+
}
31+
2332
// testing the generic method with String
2433
@Test
2534
public void givenArrayOfStrings_thanListOfStringsReturnedOK() {
@@ -43,14 +52,19 @@ public void givenArrayOfIntegersAndNumberUpperBound_thanListOfIntegersReturnedOK
4352
// testing paintAllBuildings method with a subtype of Building, the method
4453
// will work with all subtypes of Building
4554
@Test
46-
public void givenSubTypeOfWildCardBoundedGenericMethod_thanOK() {
47-
48-
List<Building> subBuildingsList = new ArrayList<>();
49-
subBuildingsList.add(new Building());
50-
subBuildingsList.add(new House());
55+
public void givenSubTypeOfWildCardBoundedGenericType_thanPaintingOK() {
56+
try {
57+
List<Building> subBuildingsList = new ArrayList<>();
58+
subBuildingsList.add(new Building());
59+
subBuildingsList.add(new House());
5160

52-
boolean result = Generics.paintAllBuildings(subBuildingsList);
53-
assertTrue(result);
61+
// prints
62+
// Painting Building
63+
// Painting House
64+
Generics.paintAllBuildings(subBuildingsList);
65+
} catch (Exception e) {
66+
fail();
67+
}
5468
}
5569

5670
}

0 commit comments

Comments
 (0)