From 9d3b8dfb72a80e520e33f0acc27ca18c30707e82 Mon Sep 17 00:00:00 2001 From: slavisa-baeldung Date: Sun, 4 Dec 2016 12:13:12 +0100 Subject: [PATCH] BAEL-376 - changing method signatures --- .../java/com/baeldung/generics/Building.java | 2 +- .../java/com/baeldung/generics/Generics.java | 37 +++++++++++-------- .../java/com/baeldung/generics/House.java | 2 +- .../com/baeldung/generics/GenericsTest.java | 30 +++++++++++---- 4 files changed, 46 insertions(+), 25 deletions(-) diff --git a/core-java/src/main/java/com/baeldung/generics/Building.java b/core-java/src/main/java/com/baeldung/generics/Building.java index 2af6ee0711..d162c07774 100644 --- a/core-java/src/main/java/com/baeldung/generics/Building.java +++ b/core-java/src/main/java/com/baeldung/generics/Building.java @@ -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"); } } diff --git a/core-java/src/main/java/com/baeldung/generics/Generics.java b/core-java/src/main/java/com/baeldung/generics/Generics.java index bea20eab1d..2b83778f64 100644 --- a/core-java/src/main/java/com/baeldung/generics/Generics.java +++ b/core-java/src/main/java/com/baeldung/generics/Generics.java @@ -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 List fromArrayToList(T[] a) { - return Arrays.stream(a).collect(Collectors.toList()); - } + // definition of a generic method + public static List 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 List fromArrayToListWithUpperBound(T[] a) { - return Arrays.stream(a).collect(Collectors.toList()); - } + // definition of a generic method + public static List fromArrayToList(T[] a, List list, Function mapperFunction) { + List 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 buildings) { - buildings.forEach(Building::paint); - return true; - } + + // example of a generic method that has Number as an upper bound for T + public static List 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 buildings) { + buildings.forEach(Building::paint); + } } \ No newline at end of file diff --git a/core-java/src/main/java/com/baeldung/generics/House.java b/core-java/src/main/java/com/baeldung/generics/House.java index e10922120f..88e7d2710a 100644 --- a/core-java/src/main/java/com/baeldung/generics/House.java +++ b/core-java/src/main/java/com/baeldung/generics/House.java @@ -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"); } } diff --git a/core-java/src/test/java/com/baeldung/generics/GenericsTest.java b/core-java/src/test/java/com/baeldung/generics/GenericsTest.java index 0488ae3d22..139633d924 100644 --- a/core-java/src/test/java/com/baeldung/generics/GenericsTest.java +++ b/core-java/src/test/java/com/baeldung/generics/GenericsTest.java @@ -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 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 subBuildingsList = new ArrayList<>(); + subBuildingsList.add(new Building()); + subBuildingsList.add(new House()); - List 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(); + } } } \ No newline at end of file