|
| 1 | +package com.baeldung.typeinference; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertFalse; |
| 4 | +import static org.junit.Assert.assertTrue; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 6 | + |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.Arrays; |
| 9 | +import java.util.Collections; |
| 10 | +import java.util.HashMap; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Map; |
| 13 | + |
| 14 | +import org.junit.Test; |
| 15 | + |
| 16 | +public class TypeInferenceUnitTest { |
| 17 | + |
| 18 | + @Test |
| 19 | + public void givenNoTypeInference_whenInvokingGenericMethodsWithTypeParameters_ObjectsAreCreated() { |
| 20 | + // Without type inference. code is verbose. |
| 21 | + Map<String, Map<String, String>> mapOfMaps = new HashMap<String, Map<String, String>>(); |
| 22 | + List<String> strList = Collections.<String> emptyList(); |
| 23 | + List<Integer> intList = Collections.<Integer> emptyList(); |
| 24 | + |
| 25 | + assertTrue(mapOfMaps.isEmpty()); |
| 26 | + assertTrue(strList.isEmpty()); |
| 27 | + assertTrue(intList.isEmpty()); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + public void givenTypeInference_whenInvokingGenericMethodsWithoutTypeParameters_ObjectsAreCreated() { |
| 32 | + // With type inference. code is concise. |
| 33 | + List<String> strListInferred = Collections.emptyList(); |
| 34 | + List<Integer> intListInferred = Collections.emptyList(); |
| 35 | + |
| 36 | + assertTrue(strListInferred.isEmpty()); |
| 37 | + assertTrue(intListInferred.isEmpty()); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void givenJava7_whenInvokingCostructorWithoutTypeParameters_ObjectsAreCreated() { |
| 42 | + // Type Inference for constructor using diamond operator. |
| 43 | + Map<String, Map<String, String>> mapOfMapsInferred = new HashMap<>(); |
| 44 | + |
| 45 | + assertTrue(mapOfMapsInferred.isEmpty()); |
| 46 | + assertEquals("public class java.util.HashMap<K,V>", mapOfMapsInferred.getClass() |
| 47 | + .toGenericString()); |
| 48 | + } |
| 49 | + |
| 50 | + static <T> List<T> add(List<T> list, T a, T b) { |
| 51 | + list.add(a); |
| 52 | + list.add(b); |
| 53 | + return list; |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void givenGenericMethod_WhenInvokedWithoutExplicitTypes_TypesAreInferred() { |
| 58 | + // Generalized target-type inference |
| 59 | + List<String> strListGeneralized = add(new ArrayList<>(), "abc", "def"); |
| 60 | + List<Integer> intListGeneralized = add(new ArrayList<>(), 1, 2); |
| 61 | + List<Number> numListGeneralized = add(new ArrayList<>(), 1, 2.0); |
| 62 | + |
| 63 | + assertEquals("public class java.util.ArrayList<E>", strListGeneralized.getClass() |
| 64 | + .toGenericString()); |
| 65 | + assertFalse(intListGeneralized.isEmpty()); |
| 66 | + assertEquals(2, numListGeneralized.size()); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void givenLambdaExpressions_whenParameterTypesNotSpecified_ParameterTypesAreInferred() { |
| 71 | + // Type Inference and Lambda Expressions. |
| 72 | + List<Integer> intList = Arrays.asList(5, 3, 4, 2, 1); |
| 73 | + Collections.sort(intList, (a, b) -> { |
| 74 | + assertEquals("java.lang.Integer", a.getClass().getName()); |
| 75 | + return a.compareTo(b); |
| 76 | + }); |
| 77 | + assertEquals("[1, 2, 3, 4, 5]", Arrays.toString(intList.toArray())); |
| 78 | + |
| 79 | + List<String> strList = Arrays.asList("Red", "Blue", "Green"); |
| 80 | + Collections.sort(strList, (a, b) -> { |
| 81 | + assertEquals("java.lang.String", a.getClass().getName()); |
| 82 | + return a.compareTo(b); |
| 83 | + }); |
| 84 | + assertEquals("[Blue, Green, Red]", Arrays.toString(strList.toArray())); |
| 85 | + } |
| 86 | + |
| 87 | +} |
0 commit comments