commit
c7a5ba91af
|
@ -15,6 +15,11 @@
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-lang3</artifactId>
|
||||||
|
<version>3.9</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>commons-beanutils</groupId>
|
<groupId>commons-beanutils</groupId>
|
||||||
<artifactId>commons-beanutils</artifactId>
|
<artifactId>commons-beanutils</artifactId>
|
||||||
|
|
|
@ -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<Coordinates, Double> getMostDistantPoint(
|
||||||
|
List<Coordinates> coordinatesList,
|
||||||
|
Coordinates target) {
|
||||||
|
return coordinatesList.stream()
|
||||||
|
.map(coordinates -> ImmutablePair.of(coordinates, coordinates.calculateDistance(target)))
|
||||||
|
.max(Comparator.comparingDouble(Pair::getRight))
|
||||||
|
.get();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -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<Double, Double, Double> getMinAvgMaxTriple(
|
||||||
|
List<Coordinates> coordinatesList,
|
||||||
|
Coordinates target) {
|
||||||
|
|
||||||
|
List<Double> 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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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<Coordinates> 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<Coordinates, Double> 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());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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<Coordinates> 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<Double, Double, Double> 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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -1 +1,4 @@
|
||||||
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
|
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
|
||||||
|
spring.application.name = spring-boot-bootstrap
|
||||||
|
spring.datasource.username = ${SPRING_DATASOURCE_USER}
|
||||||
|
spring.datasource.password = ${SPRING_DATASOURCE_PASSWORD}
|
Loading…
Reference in New Issue