From 50066f0aefdc6fc90fc4ea17cf44c50a47c52680 Mon Sep 17 00:00:00 2001 From: Kai Yuan Date: Sun, 26 Jan 2020 01:22:39 +0100 Subject: [PATCH] improvement of the article "How to Return Multiple Values From a Java Method" --- core-java-modules/core-java-lang-2/pom.xml | 5 +++ ...pleReturnValuesUsingApacheCommonsPair.java | 20 +++++++++++ ...eReturnValuesUsingApacheCommonsTriple.java | 23 ++++++++++++ ...nValuesUsingApacheCommonsPairUnitTest.java | 34 ++++++++++++++++++ ...aluesUsingApacheCommonsTripleUnitTest.java | 35 +++++++++++++++++++ 5 files changed, 117 insertions(+) create mode 100644 core-java-modules/core-java-lang-2/src/main/java/com/baeldung/methodmultiplereturnvalues/MultipleReturnValuesUsingApacheCommonsPair.java create mode 100644 core-java-modules/core-java-lang-2/src/main/java/com/baeldung/methodmultiplereturnvalues/MultipleReturnValuesUsingApacheCommonsTriple.java create mode 100644 core-java-modules/core-java-lang-2/src/test/java/com/baeldung/methodmultiplereturnvalues/MultipleReturnValuesUsingApacheCommonsPairUnitTest.java create mode 100644 core-java-modules/core-java-lang-2/src/test/java/com/baeldung/methodmultiplereturnvalues/MultipleReturnValuesUsingApacheCommonsTripleUnitTest.java diff --git a/core-java-modules/core-java-lang-2/pom.xml b/core-java-modules/core-java-lang-2/pom.xml index 4702b7350b..a5fb5ca859 100644 --- a/core-java-modules/core-java-lang-2/pom.xml +++ b/core-java-modules/core-java-lang-2/pom.xml @@ -15,6 +15,11 @@ + + org.apache.commons + commons-lang3 + 3.9 + commons-beanutils commons-beanutils diff --git a/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/methodmultiplereturnvalues/MultipleReturnValuesUsingApacheCommonsPair.java b/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/methodmultiplereturnvalues/MultipleReturnValuesUsingApacheCommonsPair.java new file mode 100644 index 0000000000..2f505443c2 --- /dev/null +++ b/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/methodmultiplereturnvalues/MultipleReturnValuesUsingApacheCommonsPair.java @@ -0,0 +1,20 @@ +package com.baeldung.methodmultiplereturnvalues; + +import org.apache.commons.lang3.tuple.ImmutablePair; +import org.apache.commons.lang3.tuple.Pair; + +import java.util.Comparator; +import java.util.List; + +class MultipleReturnValuesUsingApacheCommonsPair { + + static ImmutablePair getMostDistantPoint( + List coordinatesList, + Coordinates target) { + return coordinatesList.stream() + .map(coordinates -> ImmutablePair.of(coordinates, coordinates.calculateDistance(target))) + .max(Comparator.comparingDouble(Pair::getRight)) + .get(); + + } +} diff --git a/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/methodmultiplereturnvalues/MultipleReturnValuesUsingApacheCommonsTriple.java b/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/methodmultiplereturnvalues/MultipleReturnValuesUsingApacheCommonsTriple.java new file mode 100644 index 0000000000..4da297a51f --- /dev/null +++ b/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/methodmultiplereturnvalues/MultipleReturnValuesUsingApacheCommonsTriple.java @@ -0,0 +1,23 @@ +package com.baeldung.methodmultiplereturnvalues; + +import org.apache.commons.lang3.tuple.ImmutableTriple; + +import java.util.List; +import java.util.stream.Collectors; + +class MultipleReturnValuesUsingApacheCommonsTriple { + + static ImmutableTriple getMinAvgMaxTriple( + List coordinatesList, + Coordinates target) { + + List distanceList = coordinatesList.stream() + .map(coordinates -> coordinates.calculateDistance(target)) + .collect(Collectors.toList()); + Double minDistance = distanceList.stream().mapToDouble(Double::doubleValue).min().getAsDouble(); + Double avgDistance = distanceList.stream().mapToDouble(Double::doubleValue).average().orElse(0.0D); + Double maxDistance = distanceList.stream().mapToDouble(Double::doubleValue).max().getAsDouble(); + + return ImmutableTriple.of(minDistance, avgDistance, maxDistance); + } +} diff --git a/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/methodmultiplereturnvalues/MultipleReturnValuesUsingApacheCommonsPairUnitTest.java b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/methodmultiplereturnvalues/MultipleReturnValuesUsingApacheCommonsPairUnitTest.java new file mode 100644 index 0000000000..ca5736fe50 --- /dev/null +++ b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/methodmultiplereturnvalues/MultipleReturnValuesUsingApacheCommonsPairUnitTest.java @@ -0,0 +1,34 @@ +package com.baeldung.methodmultiplereturnvalues; + +import org.apache.commons.lang3.tuple.ImmutablePair; +import org.junit.jupiter.api.Test; + +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class MultipleReturnValuesUsingApacheCommonsPairUnitTest { + + @Test + void whenUsingPair_thenMultipleFieldsAreReturned() { + + List coordinatesList = new ArrayList<>(); + coordinatesList.add(new Coordinates(1, 1, "home")); + coordinatesList.add(new Coordinates(2, 2, "school")); + coordinatesList.add(new Coordinates(3, 3, "hotel")); + + Coordinates target = new Coordinates(5, 5, "gym"); + + ImmutablePair mostDistantPoint = MultipleReturnValuesUsingApacheCommonsPair.getMostDistantPoint(coordinatesList, target); + + assertEquals(1, mostDistantPoint.getLeft().getLongitude()); + assertEquals(1, mostDistantPoint.getLeft().getLatitude()); + assertEquals("home", mostDistantPoint.getLeft().getPlaceName()); + assertEquals(5.66, BigDecimal.valueOf(mostDistantPoint.getRight()).setScale(2, RoundingMode.HALF_UP).doubleValue()); + + } + +} diff --git a/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/methodmultiplereturnvalues/MultipleReturnValuesUsingApacheCommonsTripleUnitTest.java b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/methodmultiplereturnvalues/MultipleReturnValuesUsingApacheCommonsTripleUnitTest.java new file mode 100644 index 0000000000..d23036f5e6 --- /dev/null +++ b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/methodmultiplereturnvalues/MultipleReturnValuesUsingApacheCommonsTripleUnitTest.java @@ -0,0 +1,35 @@ +package com.baeldung.methodmultiplereturnvalues; + +import org.apache.commons.lang3.tuple.ImmutableTriple; +import org.junit.jupiter.api.Test; + +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class MultipleReturnValuesUsingApacheCommonsTripleUnitTest { + + @Test + void whenUsingTriple_thenMultipleFieldsAreReturned() { + + List coordinatesList = new ArrayList<>(); + coordinatesList.add(new Coordinates(1, 1, "home")); + coordinatesList.add(new Coordinates(2, 2, "school")); + coordinatesList.add(new Coordinates(3, 3, "hotel")); + + Coordinates target = new Coordinates(5, 5, "gym"); + + ImmutableTriple minAvgMax = MultipleReturnValuesUsingApacheCommonsTriple.getMinAvgMaxTriple(coordinatesList, target); + + assertEquals(2.83, scaleDouble(minAvgMax.left)); //min + assertEquals(4.24, scaleDouble(minAvgMax.middle)); //avg + assertEquals(5.66, scaleDouble(minAvgMax.right)); //max + } + + private double scaleDouble(Double d) { + return BigDecimal.valueOf(d).setScale(2, RoundingMode.HALF_UP).doubleValue(); + } +}