BAEL-376 - changing method signatures
This commit is contained in:
parent
7ca7c69098
commit
9d3b8dfb72
|
@ -7,6 +7,6 @@ public class Building {
|
|||
private static final Logger LOGGER = LoggerFactory.getLogger(Building.class);
|
||||
|
||||
public void paint() {
|
||||
LOGGER.info("Building");
|
||||
LOGGER.info("Painting Building");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,27 +1,34 @@
|
|||
package com.baeldung.generics;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Generics {
|
||||
|
||||
// definition of a generic method
|
||||
public static <T> List<T> fromArrayToList(T[] a) {
|
||||
return Arrays.stream(a).collect(Collectors.toList());
|
||||
}
|
||||
// definition of a generic method
|
||||
public static <T> List<T> fromArrayToList(T[] a) {
|
||||
return Arrays.stream(a).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
// example of a generic method that has Number as an upper bound for T
|
||||
public static <T extends Number> List<T> fromArrayToListWithUpperBound(T[] a) {
|
||||
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 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) {
|
||||
buildings.forEach(Building::paint);
|
||||
return true;
|
||||
}
|
||||
|
||||
// example of a generic method that has Number as an upper bound for T
|
||||
public static <T extends Number> List<T> fromArrayToListWithUpperBound(T[] a) {
|
||||
return Arrays.stream(a).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
// example of a generic method with a wild card, this method can be used
|
||||
// with a list of any subtype of Building
|
||||
public static void paintAllBuildings(List<? extends Building> buildings) {
|
||||
buildings.forEach(Building::paint);
|
||||
}
|
||||
|
||||
}
|
|
@ -7,6 +7,6 @@ public class House extends Building {
|
|||
private static final Logger LOGGER = LoggerFactory.getLogger(House.class);
|
||||
|
||||
public void paint() {
|
||||
LOGGER.info("House");
|
||||
LOGGER.info("Painting House");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ import java.util.List;
|
|||
|
||||
import static org.hamcrest.CoreMatchers.hasItems;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
public class GenericsTest {
|
||||
|
||||
|
@ -20,6 +20,15 @@ public class GenericsTest {
|
|||
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
|
||||
@Test
|
||||
public void givenArrayOfStrings_thanListOfStringsReturnedOK() {
|
||||
|
@ -43,14 +52,19 @@ public class GenericsTest {
|
|||
// testing paintAllBuildings method with a subtype of Building, the method
|
||||
// will work with all subtypes of Building
|
||||
@Test
|
||||
public void givenSubTypeOfWildCardBoundedGenericMethod_thanOK() {
|
||||
public void givenSubTypeOfWildCardBoundedGenericType_thanPaintingOK() {
|
||||
try {
|
||||
List<Building> subBuildingsList = new ArrayList<>();
|
||||
subBuildingsList.add(new Building());
|
||||
subBuildingsList.add(new House());
|
||||
|
||||
List<Building> subBuildingsList = new ArrayList<>();
|
||||
subBuildingsList.add(new Building());
|
||||
subBuildingsList.add(new House());
|
||||
|
||||
boolean result = Generics.paintAllBuildings(subBuildingsList);
|
||||
assertTrue(result);
|
||||
// prints
|
||||
// Painting Building
|
||||
// Painting House
|
||||
Generics.paintAllBuildings(subBuildingsList);
|
||||
} catch (Exception e) {
|
||||
fail();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue