-
Notifications
You must be signed in to change notification settings - Fork 0
fix(MAJORLEA-013): 6 review findings across 3 files #74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,35 +5,39 @@ | |
| import java.io.InputStreamReader; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.Stream; | ||
|
|
||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.core.io.ClassPathResource; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import cx.flamingo.analysis.model.City; | ||
| import cx.flamingo.analysis.model.Region; | ||
| import jakarta.annotation.PostConstruct; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
|
|
||
| @Slf4j | ||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class RegionService { | ||
| private List<Region> regions; | ||
| private Map<String, Integer> regionPopulationCache; | ||
|
|
||
| @Autowired | ||
| private StateService stateService; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 𦩠π RegionService uses @Autowired field injection instead of @requiredargsconstructor Removed both π€ Prompt for AI agentsfix confidence: π’ 95 high β react π/π to teach the reviewer |
||
| private final StateService stateService; | ||
|
|
||
| @Autowired | ||
| private CityService cityService; | ||
| private final CityService cityService; | ||
|
|
||
| @PostConstruct | ||
| public void init() { | ||
| loadRegions(); | ||
| log.info("Loaded {} regions", regions.size()); | ||
| buildPopulationCache(); | ||
| } | ||
|
|
||
| private void loadRegions() { | ||
|
|
@@ -71,6 +75,15 @@ private void loadRegions() { | |
| } | ||
| } | ||
|
|
||
| private void buildPopulationCache() { | ||
| regionPopulationCache = new HashMap<>(); | ||
| for (City city : cityService.getAllCities()) { | ||
| for (String regionId : city.getRegionIds()) { | ||
| regionPopulationCache.merge(regionId, city.getPopulation(), Integer::sum); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public void updateRegion(Region updatedRegion) { | ||
| int index = -1; | ||
| for (int i = 0; i < regions.size(); i++) { | ||
|
Comment on lines
75
to
89
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 𦩠π RegionService.getRegionTotalPopulation() performs a full scan of all cities for every region during autocomplete sorting β O(n*m) complexity Added a π€ Prompt for AI agentsfix confidence: π‘ 88 medium β react π/π to teach the reviewer |
||
|
|
@@ -88,10 +101,7 @@ public void updateRegion(Region updatedRegion) { | |
| } | ||
|
|
||
| private int getRegionTotalPopulation(Region region) { | ||
| return cityService.getAllCities().stream() | ||
| .filter(city -> city.getRegionIds().contains(region.getId())) | ||
| .mapToInt(City::getPopulation) | ||
| .sum(); | ||
| return regionPopulationCache.getOrDefault(region.getId(), 0); | ||
| } | ||
|
|
||
| public List<Region> autocompleteRegions(String query, String stateId, List<String> cityIds, int maxResults) { | ||
|
|
@@ -122,21 +132,19 @@ public List<Region> autocompleteRegions(String query, String stateId, List<Strin | |
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| public Region getRegionById(String id) { | ||
| public Optional<Region> getRegionById(String id) { | ||
| return regions.stream() | ||
| .filter(r -> r.getId().equals(id)) | ||
| .findFirst() | ||
| .orElse(null); | ||
| .findFirst(); | ||
| } | ||
|
|
||
| public Region getRegionByName(String name) { | ||
| public Optional<Region> getRegionByName(String name) { | ||
| return regions.stream() | ||
| .filter(r -> r.getName().equalsIgnoreCase(name)) | ||
| .findFirst() | ||
| .orElse(null); | ||
| .findFirst(); | ||
| } | ||
|
|
||
| public List<Region> getAllRegions() { | ||
| return new ArrayList<>(regions); | ||
| } | ||
| } | ||
| } | ||
|
Comment on lines
132
to
+150
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 𦩠π΄ RegionService.getRegionById() and getRegionByName() return null instead of Optional or throwing Changed π€ Prompt for AI agentsfix confidence: π‘ 72 medium β react π/π to teach the reviewer |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,8 +4,11 @@ | |
| import java.io.IOException; | ||
| import java.io.InputStreamReader; | ||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.Stream; | ||
|
|
@@ -24,6 +27,7 @@ | |
| @Service | ||
| public class StateService { | ||
| private List<State> states; | ||
| private Map<String, Integer> statePopulationCache; | ||
|
|
||
| private final CityService cityService; | ||
|
|
||
|
|
@@ -36,6 +40,7 @@ public StateService(@Lazy CityService cityService) { | |
| public void init() { | ||
| loadStates(); | ||
| log.info("Loaded {} states", states.size()); | ||
| buildPopulationCache(); | ||
| } | ||
|
|
||
| private void loadStates() { | ||
|
|
@@ -70,11 +75,15 @@ private void loadStates() { | |
| } | ||
| } | ||
|
|
||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 𦩠π StateService.getStateTotalPopulation() performs a full scan of all cities for every state during autocomplete sorting β O(n*m) complexity Added π€ Prompt for AI agentsfix confidence: π’ 90 high β react π/π to teach the reviewer |
||
| private void buildPopulationCache() { | ||
| statePopulationCache = new HashMap<>(); | ||
| for (City city : cityService.getAllCities()) { | ||
| statePopulationCache.merge(city.getStateId(), city.getPopulation(), Integer::sum); | ||
| } | ||
| } | ||
|
|
||
| private int getStateTotalPopulation(State state) { | ||
| return cityService.getAllCities().stream() | ||
| .filter(city -> city.getStateId().equals(state.getId())) | ||
| .mapToInt(City::getPopulation) | ||
| .sum(); | ||
| return statePopulationCache.getOrDefault(state.getId(), 0); | ||
| } | ||
|
|
||
| public List<State> autocompleteStates(String query, String regionId, List<String> cityIds, int maxResults) { | ||
|
|
@@ -105,21 +114,19 @@ public List<State> autocompleteStates(String query, String regionId, List<String | |
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 𦩠π΄ StateService.getStateById() and getStateByCode() return null instead of Optional or throwing Changed π€ Prompt for AI agentsfix confidence: π‘ 72 medium β react π/π to teach the reviewer |
||
| public State getStateById(String id) { | ||
| public Optional<State> getStateById(String id) { | ||
| return states.stream() | ||
| .filter(s -> s.getId().equals(id)) | ||
| .findFirst() | ||
| .orElse(null); | ||
| .findFirst(); | ||
| } | ||
|
|
||
| public State getStateByCode(String code) { | ||
| public Optional<State> getStateByCode(String code) { | ||
| return states.stream() | ||
| .filter(s -> s.getCode().equalsIgnoreCase(code)) | ||
| .findFirst() | ||
| .orElse(null); | ||
| .findFirst(); | ||
| } | ||
|
|
||
| public List<State> getAllStates() { | ||
| return new ArrayList<>(states); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
𦩠π΄ LanguageService.getLanguageById() returns null instead of Optional or throwing
Changed
getLanguageById(String id)return type fromLanguagetoOptional<Language>and replaced.orElse(null)with.findFirst()directly (line 62-65). Addedimport java.util.Optional;at line 10. The method itself is now null-safe. RISK: Any callers ofgetLanguageById()in other files (controllers, services, etc.) that previously used the returnedLanguagedirectly will now receive anOptional<Language>and will fail to compile until updated to call.get(),.orElseThrow(), or similar. Those callers are not visible in this file and must be updated separately. A reviewer should search the codebase for all usages ofgetLanguageByIdbefore merging.π€ Prompt for AI agents
fix confidence: π‘ 72 medium β react π/π to teach the reviewer