From 4ef6b9039f4e77075b6b199a5df9106da22f20ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bogdan=20Cardo=C5=9F?= <106325528+sodrac@users.noreply.github.com> Date: Mon, 6 Nov 2023 22:47:45 +0200 Subject: [PATCH] BAEL-6874 update test code for article (#15155) * BAEL-6819 convert from int to Long in Java * BAEL-6819 update package name * BAEL-6874 test code for article * BAEL-6874 rename test class * BAEL-6874 update test code for article --- .../GenericNumbersComparatorUnitTest.java | 40 ++----------------- 1 file changed, 4 insertions(+), 36 deletions(-) diff --git a/core-java-modules/core-java-numbers-6/src/test/java/com/baeldung/genericnumberscomparator/GenericNumbersComparatorUnitTest.java b/core-java-modules/core-java-numbers-6/src/test/java/com/baeldung/genericnumberscomparator/GenericNumbersComparatorUnitTest.java index 37b971b5c5..0fcc0f3453 100644 --- a/core-java-modules/core-java-numbers-6/src/test/java/com/baeldung/genericnumberscomparator/GenericNumbersComparatorUnitTest.java +++ b/core-java-modules/core-java-numbers-6/src/test/java/com/baeldung/genericnumberscomparator/GenericNumbersComparatorUnitTest.java @@ -1,13 +1,12 @@ package com.baeldung.genericnumberscomparator; +import static org.assertj.core.api.Assertions.entry; import static org.junit.jupiter.api.Assertions.assertEquals; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.util.Comparator; -import java.util.HashMap; import java.util.Map; -import java.util.Optional; import java.util.function.BiFunction; import java.util.function.BiPredicate; import java.util.function.Function; @@ -25,6 +24,7 @@ class GenericNumbersComparatorUnitTest { assertEquals(0, compareDouble(5, 5.0)); } + // we create a method that compares Integer, but this could also be done for other types e.g. Double, BigInteger public int compareTo(Integer int1, Integer int2) { return int1.compareTo(int2); } @@ -34,11 +34,10 @@ class GenericNumbersComparatorUnitTest { assertEquals(-1, compareTo(5, 7)); } - Map, BiFunction> comparisonMap = new HashMap<>(); + // for this example, we create a function that compares Integer, but this could also be done for other types e.g. Double, BigInteger + Map, BiFunction> comparisonMap = Map.ofEntries(entry(Integer.class, (num1, num2) -> ((Integer) num1).compareTo((Integer) num2))); public int compareUsingMap(Number num1, Number num2) { - comparisonMap.put(Integer.class, (a, b) -> ((Integer) num1).compareTo((Integer) num2)); - return comparisonMap.get(num1.getClass()) .apply(num1, num2); } @@ -70,24 +69,6 @@ class GenericNumbersComparatorUnitTest { assertEquals(-1, compareUsingReflection(5, 7)); } - interface NumberComparatorFactory { - Comparator getComparator(); - } - - class IntegerComparatorFactory implements NumberComparatorFactory { - @Override - public Comparator getComparator() { - return (num1, num2) -> ((Integer) num1).compareTo((Integer) num2); - } - } - - @Test - void givenNumbers_whenUseComparatorFactory_thenWillExecuteComparison() { - NumberComparatorFactory factory = new IntegerComparatorFactory(); - Comparator comparator = factory.getComparator(); - assertEquals(-1, comparator.compare(5, 7)); - } - Function toDouble = Number::doubleValue; BiPredicate isEqual = (num1, num2) -> toDouble.apply(num1) .equals(toDouble.apply(num2)); @@ -97,19 +78,6 @@ class GenericNumbersComparatorUnitTest { assertEquals(true, isEqual.test(5, 5.0)); } - private Number someNumber = 5; - private Number anotherNumber = 5.0; - - Optional optNum1 = Optional.ofNullable(someNumber); - Optional optNum2 = Optional.ofNullable(anotherNumber); - int comparisonResult = optNum1.flatMap(n1 -> optNum2.map(n2 -> Double.compare(n1.doubleValue(), n2.doubleValue()))) - .orElse(0); - - @Test - void givenNumbers_whenUseComparisonResult_thenWillExecuteComparison() { - assertEquals(0, comparisonResult); - } - private boolean someCondition = true; Function dynamicFunction = someCondition ? Number::doubleValue : Number::intValue; Comparator dynamicComparator = (num1, num2) -> ((Comparable) dynamicFunction.apply(num1)).compareTo(dynamicFunction.apply(num2));