-
Notifications
You must be signed in to change notification settings - Fork 2
/
S2CellSearch.java
37 lines (30 loc) · 1.35 KB
/
S2CellSearch.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.amorgakco.backend.group.service.search;
import com.amorgakco.backend.global.exception.GroupSearchException;
import com.amorgakco.backend.group.domain.location.LocationCalculator;
import com.amorgakco.backend.group.dto.GroupSearchRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import java.util.List;
@RequiredArgsConstructor
@Component
public class S2CellSearch {
private final List<GroupSearchStrategy> searchStrategies;
public List<String> getCellTokens(final GroupSearchRequest request) {
final double diagonalDistance = getDiagonalDistance(request);
final GroupSearchStrategy groupSearchStrategy = getSearchStrategy(diagonalDistance);
return groupSearchStrategy.selectCellTokens(request);
}
private double getDiagonalDistance(final GroupSearchRequest request) {
return LocationCalculator.getDistance(
request.southWestLon(),
request.southWestLat(),
request.northEastLon(),
request.northEastLat());
}
private GroupSearchStrategy getSearchStrategy(final double diagonalDistance) {
return searchStrategies.stream()
.filter(s -> s.isValid(diagonalDistance))
.findFirst()
.orElseThrow(GroupSearchException::invalidDiagonalDistance);
}
}