Bael 3510-method multiple return values (#8348)
* init * first draft * first review round changes * second review round * changing arg name
This commit is contained in:
parent
888539b06d
commit
e0f885a467
@ -0,0 +1,49 @@
|
||||
package com.baeldung.methodmultiplereturnvalues;
|
||||
|
||||
public class Coordinates {
|
||||
|
||||
private double longitude;
|
||||
private double latitude;
|
||||
private String placeName;
|
||||
|
||||
public Coordinates() {}
|
||||
|
||||
public Coordinates(double longitude, double latitude, String placeName) {
|
||||
this.longitude = longitude;
|
||||
this.latitude = latitude;
|
||||
this.placeName = placeName;
|
||||
}
|
||||
|
||||
public double calculateDistance(Coordinates c) {
|
||||
|
||||
double s1 = Math.abs(this.longitude - c.longitude);
|
||||
double s2 = Math.abs(this.latitude - c.latitude);
|
||||
|
||||
return Math.hypot(s1, s2);
|
||||
}
|
||||
|
||||
public double getLongitude() {
|
||||
return longitude;
|
||||
}
|
||||
|
||||
public void setLongitude(double longitude) {
|
||||
this.longitude = longitude;
|
||||
}
|
||||
|
||||
public double getLatitude() {
|
||||
return latitude;
|
||||
}
|
||||
|
||||
public void setLatitude(double latitude) {
|
||||
this.latitude = latitude;
|
||||
}
|
||||
|
||||
public String getPlaceName() {
|
||||
return placeName;
|
||||
}
|
||||
|
||||
public void setPlaceName(String placeName) {
|
||||
this.placeName = placeName;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package com.baeldung.methodmultiplereturnvalues;
|
||||
|
||||
class MultipleReturnValuesUsingArrays {
|
||||
|
||||
|
||||
static double[] getCoordinatesDoubleArray() {
|
||||
|
||||
double[] coordinates = new double[2];
|
||||
|
||||
coordinates[0] = 10;
|
||||
coordinates[1] = 12.5;
|
||||
|
||||
return coordinates;
|
||||
}
|
||||
|
||||
|
||||
static Number[] getCoordinatesNumberArray() {
|
||||
|
||||
Number[] coordinates = new Number[2];
|
||||
|
||||
coordinates[0] = 10; //Integer
|
||||
coordinates[1] = 12.5; //Double
|
||||
|
||||
return coordinates;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package com.baeldung.methodmultiplereturnvalues;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
class MultipleReturnValuesUsingCollections {
|
||||
|
||||
static List<Number> getCoordinatesList() {
|
||||
|
||||
List<Number> coordinates = new ArrayList<>();
|
||||
|
||||
coordinates.add(10);
|
||||
coordinates.add(12.5);
|
||||
|
||||
return coordinates;
|
||||
}
|
||||
|
||||
static Map<String, Number> getCoordinatesMap() {
|
||||
|
||||
Map<String, Number> coordinates = new HashMap<>();
|
||||
|
||||
coordinates.put("longitude", 10);
|
||||
coordinates.put("latitude", 12.5);
|
||||
|
||||
return coordinates;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package com.baeldung.methodmultiplereturnvalues;
|
||||
|
||||
class MultipleReturnValuesUsingContainer {
|
||||
|
||||
static Coordinates getCoordinates() {
|
||||
|
||||
double longitude = 10;
|
||||
double latitude = 12.5;
|
||||
String placeName = "home";
|
||||
|
||||
return new Coordinates(longitude, latitude, placeName);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package com.baeldung.methodmultiplereturnvalues;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
class MultipleReturnValuesUsingTuples {
|
||||
|
||||
static Tuple2<Coordinates, Double> getMostDistantPoint(List<Coordinates> coordinatesList,
|
||||
Coordinates target) {
|
||||
|
||||
return coordinatesList.stream()
|
||||
.map(coor -> new Tuple2<>(coor, coor.calculateDistance(target)))
|
||||
.max((d1, d2) -> Double.compare(d1.getSecond(), d2.getSecond()))
|
||||
.get();
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package com.baeldung.methodmultiplereturnvalues;
|
||||
|
||||
public class Tuple2 <K, V> {
|
||||
|
||||
private K first;
|
||||
private V second;
|
||||
|
||||
public Tuple2() {}
|
||||
|
||||
public Tuple2(K first, V second) {
|
||||
this.first = first;
|
||||
this.second = second;
|
||||
}
|
||||
|
||||
public K getFirst() {
|
||||
return first;
|
||||
}
|
||||
|
||||
public V getSecond() {
|
||||
return second;
|
||||
}
|
||||
|
||||
public void setFirst(K first) {
|
||||
this.first = first;
|
||||
}
|
||||
|
||||
public void setSecond(V second) {
|
||||
this.second = second;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.baeldung.methodmultiplereturnvalues;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class MultipleReturnValuesUsingArraysUnitTest {
|
||||
|
||||
@Test
|
||||
void whenUsingArrayOfDoubles_thenMultipleDoubleFieldsAreReturned() {
|
||||
|
||||
double[] coordinates = MultipleReturnValuesUsingArrays.getCoordinatesDoubleArray();
|
||||
assertEquals(10, coordinates[0]);
|
||||
assertEquals(12.5, coordinates[1]);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenUsingArrayOfNumbers_thenMultipleNumberFieldsAreReturned() {
|
||||
|
||||
Number[] coordinates = MultipleReturnValuesUsingArrays.getCoordinatesNumberArray();
|
||||
assertEquals(10, coordinates[0]);
|
||||
assertEquals(12.5, coordinates[1]);
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.baeldung.methodmultiplereturnvalues;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class MultipleReturnValuesUsingCollectionsUnitTest {
|
||||
|
||||
@Test
|
||||
void whenUsingList_thenMultipleFieldsAreReturned() {
|
||||
|
||||
List<Number> coordinates = MultipleReturnValuesUsingCollections.getCoordinatesList();
|
||||
assertEquals(Integer.valueOf(10), coordinates.get(0));
|
||||
assertEquals(Double.valueOf(12.5), coordinates.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenUsingMap_thenMultipleFieldsAreReturned() {
|
||||
|
||||
Map<String, Number> coordinates = MultipleReturnValuesUsingCollections.getCoordinatesMap();
|
||||
assertEquals(Integer.valueOf(10), coordinates.get("longitude"));
|
||||
assertEquals(Double.valueOf(12.5), coordinates.get("latitude"));
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package com.baeldung.methodmultiplereturnvalues;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class MultipleReturnValuesUsingContainerUnitTest {
|
||||
|
||||
@Test
|
||||
void whenUsingContainerClass_thenMultipleFieldsAreReturned() {
|
||||
|
||||
Coordinates coordinates = MultipleReturnValuesUsingContainer.getCoordinates();
|
||||
|
||||
assertEquals(10, coordinates.getLongitude());
|
||||
assertEquals(12.5, coordinates.getLatitude());
|
||||
assertEquals("home", coordinates.getPlaceName());
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package com.baeldung.methodmultiplereturnvalues;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class MultipleReturnValuesUsingTuplesUnitTest {
|
||||
|
||||
@Test
|
||||
void whenUsingTuple_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");
|
||||
|
||||
Tuple2<Coordinates, Double> mostDistantPoint = MultipleReturnValuesUsingTuples.getMostDistantPoint(coordinatesList, target);
|
||||
|
||||
assertEquals(1, mostDistantPoint.getFirst().getLongitude());
|
||||
assertEquals(1, mostDistantPoint.getFirst().getLatitude());
|
||||
assertEquals("home", mostDistantPoint.getFirst().getPlaceName());
|
||||
assertEquals(5.66, BigDecimal.valueOf(mostDistantPoint.getSecond()).setScale(2, RoundingMode.HALF_UP).doubleValue());
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user