-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- path 요청 POST방식에서 GET방식으로 수정 - 시간 관련 데이터 TimeEntity로 통합 - 가독성을 위해 스트림 사이 공백 추가 - 불필요한 생성자 파라미터 삭제 - PathType, GraphResponse˜ 클래스 패키지 이동 - GraphTest 추가 - Algorithm 확장성을 위한 수정 - Algorithm 테스트 추가
- Loading branch information
Showing
21 changed files
with
294 additions
and
157 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
This file was deleted.
Oops, something went wrong.
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
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
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,6 @@ | ||
package wooteco.subway.admin.domain; | ||
|
||
public interface PathAlgorithm { | ||
|
||
PathResult findPath(Long sourceId, Long targetId, Graph graph); | ||
} |
50 changes: 50 additions & 0 deletions
50
src/main/java/wooteco/subway/admin/domain/PathAlgorithmByDijkstra.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 wooteco.subway.admin.domain; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import org.jgrapht.alg.shortestpath.DijkstraShortestPath; | ||
import org.springframework.stereotype.Component; | ||
|
||
import wooteco.subway.admin.exception.IllegalStationNameException; | ||
import wooteco.subway.admin.exception.NotFoundPathException; | ||
|
||
@Component | ||
public class PathAlgorithmByDijkstra implements PathAlgorithm { | ||
|
||
public PathResult findPath(Long sourceId, Long targetId, Graph graph) { | ||
DijkstraShortestPath<Long, LineStationEdge> dijkstraShortestPath = | ||
new DijkstraShortestPath<>(graph.getGraph()); | ||
validate(sourceId, targetId); | ||
|
||
if (Objects.isNull(dijkstraShortestPath.getPath(sourceId, targetId))) { | ||
throw new NotFoundPathException(sourceId, targetId); | ||
} | ||
|
||
return mapToPahtResponse(sourceId, targetId, dijkstraShortestPath); | ||
} | ||
|
||
private void validate(Long sourceId, Long targetId) { | ||
if (Objects.equals(sourceId, targetId)) { | ||
throw new IllegalStationNameException(sourceId, targetId); | ||
} | ||
} | ||
|
||
private PathResult mapToPahtResponse(Long source, Long target, | ||
DijkstraShortestPath<Long, LineStationEdge> dijkstraShortestPath) { | ||
List<Long> path = dijkstraShortestPath.getPath(source, target).getVertexList(); | ||
|
||
int totalDistance = dijkstraShortestPath.getPath(source, target) | ||
.getEdgeList() | ||
.stream() | ||
.mapToInt(LineStationEdge::getDistance) | ||
.sum(); | ||
|
||
int totalDuration = dijkstraShortestPath.getPath(source, target) | ||
.getEdgeList() | ||
.stream() | ||
.mapToInt(LineStationEdge::getDuration) | ||
.sum(); | ||
return new PathResult(path, totalDistance, totalDuration); | ||
} | ||
} |
6 changes: 3 additions & 3 deletions
6
...oteco/subway/admin/dto/GraphResponse.java → ...oteco/subway/admin/domain/PathResult.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
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
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,39 @@ | ||
package wooteco.subway.admin.domain; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
import org.springframework.data.annotation.CreatedDate; | ||
import org.springframework.data.annotation.LastModifiedDate; | ||
|
||
public abstract class TimeEntity { | ||
|
||
@CreatedDate | ||
private LocalDateTime createdAt; | ||
|
||
@LastModifiedDate | ||
private LocalDateTime updatedAt; | ||
|
||
public TimeEntity() { | ||
} | ||
|
||
public TimeEntity(LocalDateTime createdAt, LocalDateTime updatedAt) { | ||
this.createdAt = createdAt; | ||
this.updatedAt = updatedAt; | ||
} | ||
|
||
public void create(LocalDateTime createdAt) { | ||
this.createdAt = createdAt; | ||
} | ||
|
||
public void update(LocalDateTime updatedAt) { | ||
this.updatedAt = updatedAt; | ||
} | ||
|
||
public LocalDateTime getUpdatedAt() { | ||
return updatedAt; | ||
} | ||
|
||
public LocalDateTime getCreatedAt() { | ||
return createdAt; | ||
} | ||
} |
Oops, something went wrong.