Skip to content

Commit

Permalink
Merge pull request #22 from tomaytotomato/Sonar-fix-warnings-tidy
Browse files Browse the repository at this point in the history
Sonar fixing warnings and code tidy
  • Loading branch information
tomaytotomato authored Aug 11, 2024
2 parents c6da844 + 3030878 commit f58cb21
Show file tree
Hide file tree
Showing 10 changed files with 604 additions and 390 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ on:
branches:
- master
paths:
- 'library/src/**'
- 'location4j/src/**'
- 'pom.xml'
- '.github/**'

pull_request:
branches:
- master
paths:
- 'library/src/**'
- 'location4j/src/**'
- 'pom.xml'
- '.github/**'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
public class JsonToBinaryConverter {

private static final String JSON_FILE = "/location4j-countries.json";
private static final String OUTPUT_FILE = "library/src/main/resources/location4j.bin";
private static final String OUTPUT_FILE = "location4j/src/main/resources/location4j.bin";

private static final Logger logger = Logger.getLogger(JsonToBinaryConverter.class.getName());

Expand All @@ -47,27 +47,31 @@ public static void main(String[] args) {
});

Path outputFile = Paths.get(OUTPUT_FILE).toAbsolutePath();
logger.info("Serializing data to binary file at: " + outputFile);
logger.log(Level.INFO,
() -> String.format("Serializing data to binary file at: %s", outputFile));

try (var fileOutputStream = new FileOutputStream(outputFile.toFile());
var objectOutputStream = new ObjectOutputStream(fileOutputStream)) {
objectOutputStream.writeObject(countries);
logger.info("Data successfully serialized to binary format.");
}
} catch (IOException e) {
logger.log(Level.SEVERE, "IO Exception occurred during serialization: " + e.getMessage(), e);
logger.log(Level.SEVERE,
String.format("IO Exception occurred during serialization: %s", e.getMessage()), e);
} catch (IllegalArgumentException e) {
logger.log(Level.SEVERE, "Argument exception: " + e.getMessage(), e);
logger.log(Level.SEVERE, String.format("Argument exception: %s", e.getMessage()), e);
}
}

private static String fixJSONPropertyNames(String jsonString) {
var modifiedJson = jsonString.replaceAll("\"native\"", "\"native_name\"");
modifiedJson = modifiedJson.replaceAll("\"zoneName\"", "\"zone_name\"");
modifiedJson = modifiedJson.replaceAll("\"gmtOffset\"", "\"gmt_offset\"");
modifiedJson = modifiedJson.replaceAll("\"gmtOffsetName\"", "\"gmt_offset_name\"");
modifiedJson = modifiedJson.replaceAll("\"tzName\"", "\"tz_name\"");
modifiedJson = modifiedJson.replaceAll("\"emojiU\"", "\"emoji_u\"");
// Use String::replace for simple string replacements
var modifiedJson = jsonString.replace("\"native\"", "\"native_name\"");
modifiedJson = modifiedJson.replace("\"zoneName\"", "\"zone_name\"");
modifiedJson = modifiedJson.replace("\"gmtOffset\"", "\"gmt_offset\"");
modifiedJson = modifiedJson.replace("\"gmtOffsetName\"", "\"gmt_offset_name\"");
modifiedJson = modifiedJson.replace("\"tzName\"", "\"tz_name\"");
modifiedJson = modifiedJson.replace("\"emojiU\"", "\"emoji_u\"");
return modifiedJson;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public class SearchLocationService implements SearchLocation {
*/
private final Map<Integer, Country> countryIdToCountryMap = new HashMap<>();
private final Map<String, Country> countryNameToCountryMap = new HashMap<>();
private final Map<String, Country> countryNativeNameToCountry = new HashMap<>();
private final Map<String, Country> iso2CodeToCountryMap = new HashMap<>();
private final Map<String, Country> iso3CodeToCountryMap = new HashMap<>();
private final Map<Integer, State> stateIdToStateMap = new HashMap<>();
Expand Down Expand Up @@ -74,28 +73,28 @@ public SearchLocationService(TextTokeniser textTokeniser, TextNormaliser textNor
}

private static Location buildLocationResult(Country topCountry, State topState, City topCity) {
var locationBuilder = Location.builder();
var locationBuilder = Location.builder()

locationBuilder.countryName(topCountry.getName());
locationBuilder.countryId(topCountry.getId());
locationBuilder.countryIso2Code(topCountry.getIso2());
locationBuilder.countryIso3Code(topCountry.getIso3());
locationBuilder.latitude(topCountry.getLatitude());
locationBuilder.longitude(topCountry.getLongitude());
.countryName(topCountry.getName())
.countryId(topCountry.getId())
.countryIso2Code(topCountry.getIso2())
.countryIso3Code(topCountry.getIso3())
.latitude(topCountry.getLatitude())
.longitude(topCountry.getLongitude());

if (!Objects.isNull(topState)) {
locationBuilder.stateName(topState.getName());
locationBuilder.stateCode(topState.getStateCode());
locationBuilder.stateId(topState.getId());
locationBuilder.latitude(topState.getLatitude());
locationBuilder.longitude(topState.getLongitude());
locationBuilder.stateName(topState.getName())
.stateCode(topState.getStateCode())
.stateId(topState.getId())
.latitude(topState.getLatitude())
.longitude(topState.getLongitude());
}

if (!Objects.isNull(topCity)) {
locationBuilder.city(topCity.getName());
locationBuilder.cityId(topCity.getId());
locationBuilder.latitude(topCity.getLatitude());
locationBuilder.longitude(topCity.getLongitude());
locationBuilder.city(topCity.getName())
.cityId(topCity.getId())
.latitude(topCity.getLatitude())
.longitude(topCity.getLongitude());
}
return locationBuilder.build();
}
Expand Down Expand Up @@ -149,9 +148,6 @@ private void addAliases() {
private void buildCountryLookups(Country country) {
countryNameToCountryMap.put(keyMaker(country.getName()), country);
countryIdToCountryMap.put(country.getId(), country);
if (!Objects.isNull(country.getNativeName()) && !country.getNativeName().isEmpty()) {
countryNativeNameToCountry.put(keyMaker(country.getNativeName()), country);
}
iso2CodeToCountryMap.put(keyMaker(country.getIso2()), country);
iso3CodeToCountryMap.put(keyMaker(country.getIso3()), country);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ public Map<String, String> getCountryIso3Aliases() {

@Override
public Map<String, String> getCountryNameAliases() {
var unitedKingdom = "United Kingdom";
return Map.of(
"Scotland", "United Kingdom",
"England", "United Kingdom",
"Northern Ireland", "United Kingdom",
"Wales", "United Kingdom",
"Cymru", "United Kingdom"
"Scotland", unitedKingdom,
"England", unitedKingdom,
"Northern Ireland", unitedKingdom,
"Wales", unitedKingdom,
"Cymru", unitedKingdom
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public DefaultCountriesDataLoaderImpl() {

loadLocationsFromBinary(inputStream, logger);
} catch (IOException | ClassNotFoundException e) {
logger.log(Level.SEVERE, "Failed to load countries file: " + e.getMessage(), e);
logger.log(Level.SEVERE, String.format("Failed to load countries file: %s", e.getMessage()), e);
}
}

Expand All @@ -51,7 +51,7 @@ private void loadLocationsFromBinary(InputStream inputStream, Logger logger)
this.countries.addAll(loadedCountries);
logger.info("Successfully loaded countries binary file");
} catch (IOException | ClassNotFoundException e) {
logger.log(Level.SEVERE, "Failed to parse countries file: " + e.getMessage(), e);
logger.log(Level.SEVERE, String.format("Failed to parse countries file: %s", e.getMessage()), e);
}
}

Expand Down
Loading

0 comments on commit f58cb21

Please sign in to comment.