-
Notifications
You must be signed in to change notification settings - Fork 0
fix(OFJAVA-013): 3 review findings in CityService.java #73
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 |
|---|---|---|
|
|
@@ -6,10 +6,14 @@ | |
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.Stream; | ||
|
|
||
| import org.apache.commons.csv.CSVFormat; | ||
| import org.apache.commons.csv.CSVParser; | ||
| import org.apache.commons.csv.CSVRecord; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.context.annotation.Lazy; | ||
| import org.springframework.core.io.ClassPathResource; | ||
|
|
@@ -42,23 +46,19 @@ public void init() { | |
| private void loadCities() { | ||
| cities = new ArrayList<>(); | ||
| try (BufferedReader reader = new BufferedReader( | ||
| new InputStreamReader(new ClassPathResource("data/cities.csv").getInputStream()))) { | ||
|
|
||
| // Skip header | ||
| reader.readLine(); | ||
|
|
||
| String line; | ||
| while ((line = reader.readLine()) != null) { | ||
| String[] parts = line.split(","); | ||
| String id = parts[0]; | ||
| String name = parts[1]; | ||
| String stateId = parts[2]; | ||
| int population = Integer.parseInt(parts[3]); | ||
| double latitude = Double.parseDouble(parts[4]); | ||
| double longitude = Double.parseDouble(parts[5]); | ||
| Set<String> regionIds = Arrays.stream(parts[6].split("\\|")) | ||
| new InputStreamReader(new ClassPathResource("data/cities.csv").getInputStream())); | ||
| CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT.withFirstRecordAsHeader())) { | ||
|
|
||
| for (CSVRecord record : csvParser) { | ||
| String id = record.get(0); | ||
| String name = record.get(1); | ||
| String stateId = record.get(2); | ||
| int population = Integer.parseInt(record.get(3)); | ||
| double latitude = Double.parseDouble(record.get(4)); | ||
| double longitude = Double.parseDouble(record.get(5)); | ||
| Set<String> regionIds = Arrays.stream(record.get(6).split("\\|")) | ||
| .collect(Collectors.toSet()); | ||
|
|
||
| City city = City.builder() | ||
| .id(id) | ||
| .name(name) | ||
|
|
@@ -68,10 +68,10 @@ private void loadCities() { | |
| .longitude(longitude) | ||
| .regionIds(regionIds) | ||
| .build(); | ||
|
|
||
| // Set nearest team ID | ||
| city.setNearestTeamId(soccerTeamService.findNearestTeamId(city)); | ||
|
|
||
| cities.add(city); | ||
| } | ||
| } catch (IOException e) { | ||
|
|
@@ -128,13 +128,11 @@ public List<City> getCitiesByRegionId(String regionId) { | |
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| public City getCityById(String id) { | ||
| City city = cities.stream() | ||
| public Optional<City> getCityById(String id) { | ||
| return cities.stream() | ||
| .filter(c -> c.getId().equals(id)) | ||
| .findFirst() | ||
| .orElse(null); | ||
|
|
||
| return city != null ? populateState(city) : null; | ||
|
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. 𦩠π CityService.getCitiesByNearestTeamId() recomputes nearest team for every city on every call In π€ Prompt for AI agentsfix confidence: π’ 95 high β react π/π to teach the reviewer |
||
| .map(this::populateState); | ||
| } | ||
|
|
||
| public List<City> getAllCities() { | ||
|
Comment on lines
128
to
138
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. 𦩠π΄ CityService.getCityById() returns null instead of throwing or returning Optional In π€ Prompt for AI agentsfix confidence: π‘ 82 medium β react π/π to teach the reviewer |
||
|
|
@@ -159,12 +157,9 @@ public List<City> getCitiesByNearestTeamId(String teamId) { | |
| return new ArrayList<>(); | ||
| } | ||
| return cities.stream() | ||
| .filter(city -> { | ||
| String nearestTeamId = soccerTeamService.findNearestTeamId(city); | ||
| return teamId.equals(nearestTeamId); | ||
| }) | ||
| .filter(city -> teamId.equals(city.getNearestTeamId())) | ||
| .map(this::populateState) | ||
| .sorted((a, b) -> Integer.compare(b.getPopulation(), a.getPopulation())) | ||
| .collect(Collectors.toList()); | ||
| } | ||
| } | ||
| } | ||
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.
𦩠π CityService CSV parser splits on comma without handling quoted fields, breaking city names with commas
In
loadCities(), replaced theBufferedReader+line.split(",")approach with Apache Commons CSV (CSVParser/CSVRecord) to correctly handle quoted fields containing commas. TheCSVFormat.DEFAULT.withFirstRecordAsHeader()replaces the manualreader.readLine()header skip, and column values are accessed by index viarecord.get(N). Risk: Apache Commons CSV (commons-csv) must be present as a dependency in the project'spom.xml/build.gradleβ if it is not already declared, the build will fail and the dependency must be added. The column index mapping (0β6) is assumed to match the originalparts[0]βparts[6]ordering exactly. A reviewer should verify the CSV header order and confirm the dependency is available.π€ Prompt for AI agents
fix confidence: π΄ 55 low β review closely β react π/π to teach the reviewer