-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
45bd5c9
commit f8db27e
Showing
5 changed files
with
160 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
backend/src/main/java/dev/tripdraw/domain/trip/RouteLength.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package dev.tripdraw.domain.trip; | ||
|
||
import dev.tripdraw.exception.trip.TripException; | ||
|
||
import java.util.List; | ||
import java.util.stream.IntStream; | ||
|
||
import static dev.tripdraw.exception.trip.TripExceptionType.ONE_OR_NO_POINT; | ||
import static java.lang.Math.*; | ||
|
||
public class RouteLength { | ||
|
||
public static final double AVERAGE_DISTANCE_FOR_ONE_DEGREE_DIFFERENCE_IN_LATITUDE_ON_KOREA_REGION = 111.1; | ||
|
||
private final Double length; | ||
|
||
private RouteLength(List<Point> points) { | ||
this.length = calculateLength(points); | ||
} | ||
|
||
public static RouteLength from(List<Point> points) { | ||
return new RouteLength(points); | ||
} | ||
|
||
private Double calculateLength(List<Point> points) { | ||
if (points.isEmpty() || points.size() == 1) { | ||
throw new TripException(ONE_OR_NO_POINT); | ||
} | ||
|
||
return IntStream.range(0, points.size() - 1) | ||
.mapToDouble(i -> distanceBetween(points.get(i), points.get(i + 1))) | ||
.sum(); | ||
} | ||
|
||
private Double distanceBetween(Point startPoint, Point endPoint) { | ||
double theta = startPoint.longitude() - endPoint.longitude(); | ||
Double latitude1 = startPoint.latitude(); | ||
Double latitude2 = endPoint.latitude(); | ||
|
||
double unitDistance = sin(toRadians(latitude1)) * sin(toRadians(latitude2)) | ||
+ cos(toRadians(latitude1)) * cos(toRadians(latitude2)) * cos(toRadians(theta)); | ||
|
||
return toDegrees(acos(unitDistance)) * AVERAGE_DISTANCE_FOR_ONE_DEGREE_DIFFERENCE_IN_LATITUDE_ON_KOREA_REGION; | ||
} | ||
|
||
public String lengthInKm() { | ||
return String.format("%.2f" + "km", length); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
backend/src/test/java/dev/tripdraw/domain/trip/RouteLengthTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package dev.tripdraw.domain.trip; | ||
|
||
import dev.tripdraw.exception.trip.TripException; | ||
import dev.tripdraw.exception.trip.TripExceptionType; | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.DisplayNameGeneration; | ||
import org.junit.jupiter.api.DisplayNameGenerator; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Stream; | ||
|
||
@SuppressWarnings("NonAsciiCharacters") | ||
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) | ||
class RouteLengthTest { | ||
|
||
@Test | ||
void 경로의_길이를_계산한다() { | ||
// given | ||
List<Point> points = List.of(new Point(1.1, 1.1, LocalDateTime.now()), new Point(2.1, 1.1, LocalDateTime.now()), new Point(2.1, 2.1, LocalDateTime.now())); | ||
|
||
// when | ||
RouteLength length = RouteLength.from(points); | ||
|
||
// then | ||
Assertions.assertThat(length.lengthInKm()).isEqualTo("222.13km"); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("generateData") | ||
void 경로에_위치정보가_하나이거나_없는_경우_예외를_발생시킨다(List<Point> points) { | ||
// expect | ||
Assertions.assertThatThrownBy(() -> RouteLength.from(points)) | ||
.isInstanceOf(TripException.class) | ||
.hasMessage(TripExceptionType.ONE_OR_NO_POINT.message()); | ||
} | ||
|
||
static Stream<List<Point>> generateData() { | ||
return Stream.of( | ||
new ArrayList<>(), | ||
List.of(new Point(1.1, 1.1, LocalDateTime.now())) | ||
); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters