BAEL-376 - changing method signatures

This commit is contained in:
slavisa-baeldung 2016-12-04 12:13:12 +01:00
parent 7ca7c69098
commit 9d3b8dfb72
4 changed files with 46 additions and 25 deletions

View File

@ -7,6 +7,6 @@ public class Building {
private static final Logger LOGGER = LoggerFactory.getLogger(Building.class); private static final Logger LOGGER = LoggerFactory.getLogger(Building.class);
public void paint() { public void paint() {
LOGGER.info("Building"); LOGGER.info("Painting Building");
} }
} }

View File

@ -1,8 +1,8 @@
package com.baeldung.generics; package com.baeldung.generics;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class Generics { public class Generics {
@ -12,6 +12,14 @@ public class Generics {
return Arrays.stream(a).collect(Collectors.toList()); return Arrays.stream(a).collect(Collectors.toList());
} }
// definition of a generic method
public static <T, G> List<G> fromArrayToList(T[] a, List<G> list, Function<T, G> mapperFunction) {
List<T> listWithTypeT = Arrays.stream(a).collect(Collectors.toList());
return listWithTypeT.stream().map(mapperFunction)
.collect(Collectors.toList());
}
// 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) {
return Arrays.stream(a).collect(Collectors.toList()); return Arrays.stream(a).collect(Collectors.toList());
@ -19,9 +27,8 @@ public class Generics {
// example of a generic method with a wild card, this method can be used // example of a generic method with a wild card, this method can be used
// with a list of any subtype of Building // with a list of any subtype of Building
public static boolean paintAllBuildings(List<? extends Building> buildings) { public static void paintAllBuildings(List<? extends Building> buildings) {
buildings.forEach(Building::paint); buildings.forEach(Building::paint);
return true;
} }
} }

View File

@ -7,6 +7,6 @@ public class House extends Building {
private static final Logger LOGGER = LoggerFactory.getLogger(House.class); private static final Logger LOGGER = LoggerFactory.getLogger(House.class);
public void paint() { public void paint() {
LOGGER.info("House"); LOGGER.info("Painting House");
} }
} }

View File

@ -7,7 +7,7 @@ import java.util.List;
import static org.hamcrest.CoreMatchers.hasItems; import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail;
public class GenericsTest { public class GenericsTest {
@ -20,6 +20,15 @@ public class GenericsTest {
assertThat(list, hasItems(intArray)); assertThat(list, hasItems(intArray));
} }
// testing the generic method with Integer and String type
@Test
public void givenArrayOfIntegers_thanListOfStringReturnedOK() {
Integer[] intArray = {1, 2, 3, 4, 5};
List<String> stringList = new ArrayList<>();
stringList = Generics.fromArrayToList(intArray, stringList, Object::toString);
assertThat(stringList, hasItems("1", "2", "3", "4", "5"));
}
// testing the generic method with String // testing the generic method with String
@Test @Test
public void givenArrayOfStrings_thanListOfStringsReturnedOK() { public void givenArrayOfStrings_thanListOfStringsReturnedOK() {
@ -43,14 +52,19 @@ public class GenericsTest {
// testing paintAllBuildings method with a subtype of Building, the method // testing paintAllBuildings method with a subtype of Building, the method
// will work with all subtypes of Building // will work with all subtypes of Building
@Test @Test
public void givenSubTypeOfWildCardBoundedGenericMethod_thanOK() { public void givenSubTypeOfWildCardBoundedGenericType_thanPaintingOK() {
try {
List<Building> subBuildingsList = new ArrayList<>(); List<Building> subBuildingsList = new ArrayList<>();
subBuildingsList.add(new Building()); subBuildingsList.add(new Building());
subBuildingsList.add(new House()); subBuildingsList.add(new House());
boolean result = Generics.paintAllBuildings(subBuildingsList); // prints
assertTrue(result); // Painting Building
// Painting House
Generics.paintAllBuildings(subBuildingsList);
} catch (Exception e) {
fail();
}
} }
} }