From c321a49f27c06ee81aa8b3bdd2a1f357646b3041 Mon Sep 17 00:00:00 2001 From: tomaytotomato <971697+tomaytotomato@users.noreply.github.com> Date: Sun, 11 Aug 2024 11:01:40 +0100 Subject: [PATCH 1/3] Sonar fixing warnings and code tidy --- .../tomaytotomato/JsonToBinaryConverter.java | 24 +- .../tomaytotomato/SearchLocationService.java | 36 +- .../aliases/DefaultLocationAliases.java | 11 +- .../DefaultCountriesDataLoaderImpl.java | 4 +- .../java/com/tomaytotomato/model/City.java | 165 ++++++--- .../java/com/tomaytotomato/model/Country.java | 335 +++++++++++------- .../com/tomaytotomato/model/Location.java | 50 +-- .../java/com/tomaytotomato/model/State.java | 143 +++++--- .../com/tomaytotomato/model/TimeZone.java | 73 ++-- 9 files changed, 531 insertions(+), 310 deletions(-) diff --git a/buildtools/src/main/java/com/tomaytotomato/JsonToBinaryConverter.java b/buildtools/src/main/java/com/tomaytotomato/JsonToBinaryConverter.java index 4271999..414b8b4 100644 --- a/buildtools/src/main/java/com/tomaytotomato/JsonToBinaryConverter.java +++ b/buildtools/src/main/java/com/tomaytotomato/JsonToBinaryConverter.java @@ -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()); @@ -47,7 +47,8 @@ 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)) { @@ -55,19 +56,22 @@ public static void main(String[] args) { 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; } + } diff --git a/location4j/src/main/java/com/tomaytotomato/SearchLocationService.java b/location4j/src/main/java/com/tomaytotomato/SearchLocationService.java index ef132cb..9c80f8b 100644 --- a/location4j/src/main/java/com/tomaytotomato/SearchLocationService.java +++ b/location4j/src/main/java/com/tomaytotomato/SearchLocationService.java @@ -36,7 +36,6 @@ public class SearchLocationService implements SearchLocation { */ private final Map countryIdToCountryMap = new HashMap<>(); private final Map countryNameToCountryMap = new HashMap<>(); - private final Map countryNativeNameToCountry = new HashMap<>(); private final Map iso2CodeToCountryMap = new HashMap<>(); private final Map iso3CodeToCountryMap = new HashMap<>(); private final Map stateIdToStateMap = new HashMap<>(); @@ -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(); } @@ -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); } diff --git a/location4j/src/main/java/com/tomaytotomato/aliases/DefaultLocationAliases.java b/location4j/src/main/java/com/tomaytotomato/aliases/DefaultLocationAliases.java index 78fbd4a..3e6a3ca 100644 --- a/location4j/src/main/java/com/tomaytotomato/aliases/DefaultLocationAliases.java +++ b/location4j/src/main/java/com/tomaytotomato/aliases/DefaultLocationAliases.java @@ -24,12 +24,13 @@ public Map getCountryIso3Aliases() { @Override public Map 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 ); } diff --git a/location4j/src/main/java/com/tomaytotomato/loader/DefaultCountriesDataLoaderImpl.java b/location4j/src/main/java/com/tomaytotomato/loader/DefaultCountriesDataLoaderImpl.java index 1443c75..4468ee5 100644 --- a/location4j/src/main/java/com/tomaytotomato/loader/DefaultCountriesDataLoaderImpl.java +++ b/location4j/src/main/java/com/tomaytotomato/loader/DefaultCountriesDataLoaderImpl.java @@ -30,7 +30,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); } } @@ -41,7 +41,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); } } diff --git a/location4j/src/main/java/com/tomaytotomato/model/City.java b/location4j/src/main/java/com/tomaytotomato/model/City.java index 71d5e15..8a0c65f 100644 --- a/location4j/src/main/java/com/tomaytotomato/model/City.java +++ b/location4j/src/main/java/com/tomaytotomato/model/City.java @@ -3,6 +3,7 @@ import java.io.Serializable; import java.math.BigDecimal; import java.util.Objects; +import javax.annotation.processing.Generated; /** * Represents a city with various attributes such as name, state, and country. @@ -33,94 +34,177 @@ public class City implements Serializable { City() { } - public Integer getId() { - return id; + private City(Integer id, Integer countryId, String countryName, String countryIso2Code, + String countryIso3Code, Integer stateId, String stateName, String stateCode, String name, + BigDecimal latitude, BigDecimal longitude) { + this.id = id; + this.countryId = countryId; + this.countryName = countryName; + this.countryIso2Code = countryIso2Code; + this.countryIso3Code = countryIso3Code; + this.stateId = stateId; + this.stateName = stateName; + this.stateCode = stateCode; + this.name = name; + this.latitude = latitude; + this.longitude = longitude; } - public void setId(Integer id) { - this.id = id; + public Integer getId() { + return id; } public Integer getCountryId() { return countryId; } - public void setCountryId(Integer countryId) { - this.countryId = countryId; - } - public String getCountryName() { return countryName; } - public void setCountryName(String countryName) { - this.countryName = countryName; - } - public String getCountryIso2Code() { return countryIso2Code; } - public void setCountryIso2Code(String countryIso2Code) { - this.countryIso2Code = countryIso2Code; - } - public String getCountryIso3Code() { return countryIso3Code; } - public void setCountryIso3Code(String countryIso3Code) { - this.countryIso3Code = countryIso3Code; - } - public Integer getStateId() { return stateId; } - public void setStateId(Integer stateId) { - this.stateId = stateId; + public String getStateName() { + return stateName; } public String getStateCode() { return stateCode; } - public void setStateCode(String stateCode) { - this.stateCode = stateCode; + public String getName() { + return name; } - public String getStateName() { - return stateName; + public BigDecimal getLatitude() { + return latitude; } - public void setStateName(String stateName) { - this.stateName = stateName; + public BigDecimal getLongitude() { + return longitude; } - public String getName() { - return name; + public void setCountryId(Integer countryId) { + this.countryId = countryId; } - public void setName(String name) { - this.name = name; + public void setCountryName(String countryName) { + this.countryName = countryName; } - public BigDecimal getLatitude() { - return latitude; + public void setCountryIso2Code(String countryIso2Code) { + this.countryIso2Code = countryIso2Code; } - public void setLatitude(BigDecimal latitude) { - this.latitude = latitude; + public void setCountryIso3Code(String countryIso3Code) { + this.countryIso3Code = countryIso3Code; } - public BigDecimal getLongitude() { - return longitude; + public void setStateId(Integer stateId) { + this.stateId = stateId; } - public void setLongitude(BigDecimal longitude) { - this.longitude = longitude; + public void setStateName(String stateName) { + this.stateName = stateName; + } + + public void setStateCode(String stateCode) { + this.stateCode = stateCode; + } + + public static final class Builder { + + private Integer id; + private Integer countryId; + private String countryName; + private String countryIso2Code; + private String countryIso3Code; + private Integer stateId; + private String stateName; + private String stateCode; + private String name; + private BigDecimal latitude; + private BigDecimal longitude; + + private Builder() { + } + + public static Builder aCity() { + return new Builder(); + } + + public Builder id(Integer id) { + this.id = id; + return this; + } + + public Builder countryId(Integer countryId) { + this.countryId = countryId; + return this; + } + + public Builder countryName(String countryName) { + this.countryName = countryName; + return this; + } + + public Builder countryIso2Code(String countryIso2Code) { + this.countryIso2Code = countryIso2Code; + return this; + } + + public Builder countryIso3Code(String countryIso3Code) { + this.countryIso3Code = countryIso3Code; + return this; + } + + public Builder stateId(Integer stateId) { + this.stateId = stateId; + return this; + } + + public Builder stateName(String stateName) { + this.stateName = stateName; + return this; + } + + public Builder stateCode(String stateCode) { + this.stateCode = stateCode; + return this; + } + + public Builder name(String name) { + this.name = name; + return this; + } + + public Builder latitude(BigDecimal latitude) { + this.latitude = latitude; + return this; + } + + public Builder longitude(BigDecimal longitude) { + this.longitude = longitude; + return this; + } + + public City build() { + return new City(id, countryId, countryName, countryIso2Code, countryIso3Code, stateId, + stateName, stateCode, name, latitude, longitude); + } } + @Generated("IntelliJ") @Override public boolean equals(Object o) { if (this == o) { @@ -141,6 +225,7 @@ public boolean equals(Object o) { getLongitude(), city.getLongitude()); } + @Generated("IntelliJ") @Override public int hashCode() { return Objects.hash(getId(), getCountryId(), getCountryName(), getCountryIso2Code(), diff --git a/location4j/src/main/java/com/tomaytotomato/model/Country.java b/location4j/src/main/java/com/tomaytotomato/model/Country.java index bc3b9d6..52f3e20 100644 --- a/location4j/src/main/java/com/tomaytotomato/model/Country.java +++ b/location4j/src/main/java/com/tomaytotomato/model/Country.java @@ -5,6 +5,7 @@ import java.util.List; import java.util.Map; import java.util.Objects; +import javax.annotation.processing.Generated; /** * Represents a country with various attributes such as name, codes, and geographic information. @@ -45,206 +46,299 @@ public class Country implements Serializable { Country() { } - public int getId() { + public Integer getId() { return id; } - public void setId(int id) { - this.id = id; - } - public String getName() { return name; } - public void setName(String name) { - this.name = name; - } - public String getIso3() { return iso3; } - public void setIso3(String iso3) { - this.iso3 = iso3; - } - public String getIso2() { return iso2; } - public void setIso2(String iso2) { - this.iso2 = iso2; - } - public String getZoneName() { return zoneName; } - public void setZoneName(String zoneName) { - this.zoneName = zoneName; - } - public String getPhoneCode() { return phoneCode; } - public void setPhoneCode(String phoneCode) { - this.phoneCode = phoneCode; - } - public String getNumericCode() { return numericCode; } - public void setNumericCode(String numericCode) { - this.numericCode = numericCode; - } - public String getCapital() { return capital; } - public void setCapital(String capital) { - this.capital = capital; - } - public String getCurrency() { return currency; } - public void setCurrency(String currency) { - this.currency = currency; - } - public String getCurrencyName() { return currencyName; } - public void setCurrencyName(String currencyName) { - this.currencyName = currencyName; - } - public String getCurrencySymbol() { return currencySymbol; } - public void setCurrencySymbol(String currencySymbol) { - this.currencySymbol = currencySymbol; - } - public String getTld() { return tld; } - public void setTld(String tld) { - this.tld = tld; - } - public String getNativeName() { return nativeName; } - public void setNativeName(String nativeName) { - this.nativeName = nativeName; - } - public String getRegion() { return region; } - public void setRegion(String region) { - this.region = region; - } - public String getRegionId() { return regionId; } - public void setRegionId(String regionId) { - this.regionId = regionId; - } - public String getSubregion() { return subregion; } - public void setSubregion(String subregion) { - this.subregion = subregion; - } - public String getSubregionId() { return subregionId; } - public void setSubregionId(String subregionId) { - this.subregionId = subregionId; - } - public List getStates() { return states; } - public void setStates(List states) { - this.states = states; - } - public String getNationality() { return nationality; } - public void setNationality(String nationality) { - this.nationality = nationality; - } - public List getTimezones() { return timezones; } - public void setTimezones(List timezones) { - this.timezones = timezones; - } - public Map getTranslations() { return translations; } - public void setTranslations(Map translations) { - this.translations = translations; - } - public BigDecimal getLatitude() { return latitude; } - public void setLatitude(BigDecimal latitude) { - this.latitude = latitude; - } - public BigDecimal getLongitude() { return longitude; } - public void setLongitude(BigDecimal longitude) { - this.longitude = longitude; - } - public String getEmoji() { return emoji; } - public void setEmoji(String emoji) { - this.emoji = emoji; - } - public String getEmojiU() { return emojiU; } - public void setEmojiU(String emojiU) { - this.emojiU = emojiU; + + public static final class Builder { + + private Integer id; + private String name; + private String iso3; + private String iso2; + private String zoneName; + private String phoneCode; + private String numericCode; + private String capital; + private String currency; + private String currencyName; + private String currencySymbol; + private String tld; + private String nativeName; + private String region; + private String regionId; + private String subregion; + private String subregionId; + private List states; + private String nationality; + private List timezones; + private Map translations; + private BigDecimal latitude; + private BigDecimal longitude; + private String emoji; + private String emojiU; + + private Builder() { + } + + public static Builder aCountry() { + return new Builder(); + } + + public Builder id(Integer id) { + this.id = id; + return this; + } + + public Builder name(String name) { + this.name = name; + return this; + } + + public Builder iso3(String iso3) { + this.iso3 = iso3; + return this; + } + + public Builder iso2(String iso2) { + this.iso2 = iso2; + return this; + } + + public Builder zoneName(String zoneName) { + this.zoneName = zoneName; + return this; + } + + public Builder phoneCode(String phoneCode) { + this.phoneCode = phoneCode; + return this; + } + + public Builder numericCode(String numericCode) { + this.numericCode = numericCode; + return this; + } + + public Builder capital(String capital) { + this.capital = capital; + return this; + } + + public Builder currency(String currency) { + this.currency = currency; + return this; + } + + public Builder currencyName(String currencyName) { + this.currencyName = currencyName; + return this; + } + + public Builder currencySymbol(String currencySymbol) { + this.currencySymbol = currencySymbol; + return this; + } + + public Builder tld(String tld) { + this.tld = tld; + return this; + } + + public Builder nativeName(String nativeName) { + this.nativeName = nativeName; + return this; + } + + public Builder region(String region) { + this.region = region; + return this; + } + + public Builder regionId(String regionId) { + this.regionId = regionId; + return this; + } + + public Builder subregion(String subregion) { + this.subregion = subregion; + return this; + } + + public Builder subregionId(String subregionId) { + this.subregionId = subregionId; + return this; + } + + public Builder states(List states) { + this.states = states; + return this; + } + + public Builder nationality(String nationality) { + this.nationality = nationality; + return this; + } + + public Builder timezones(List timezones) { + this.timezones = timezones; + return this; + } + + public Builder translations(Map translations) { + this.translations = translations; + return this; + } + + public Builder latitude(BigDecimal latitude) { + this.latitude = latitude; + return this; + } + + public Builder longitude(BigDecimal longitude) { + this.longitude = longitude; + return this; + } + + public Builder emoji(String emoji) { + this.emoji = emoji; + return this; + } + + public Builder emojiU(String emojiU) { + this.emojiU = emojiU; + return this; + } + + public Country build() { + Country country = new Country(); + country.name = this.name; + country.phoneCode = this.phoneCode; + country.iso3 = this.iso3; + country.longitude = this.longitude; + country.currencyName = this.currencyName; + country.numericCode = this.numericCode; + country.subregionId = this.subregionId; + country.capital = this.capital; + country.region = this.region; + country.translations = this.translations; + country.iso2 = this.iso2; + country.currencySymbol = this.currencySymbol; + country.timezones = this.timezones; + country.tld = this.tld; + country.currency = this.currency; + country.nativeName = this.nativeName; + country.emoji = this.emoji; + country.subregion = this.subregion; + country.zoneName = this.zoneName; + country.regionId = this.regionId; + country.latitude = this.latitude; + country.id = this.id; + country.states = this.states; + country.nationality = this.nationality; + country.emojiU = this.emojiU; + return country; + } } + @Generated("IntelliJ") @Override public boolean equals(Object o) { if (this == o) { @@ -254,26 +348,29 @@ public boolean equals(Object o) { return false; } Country country = (Country) o; - return getId() == country.getId() && Objects.equals(getName(), country.getName()) - && Objects.equals(getIso3(), country.getIso3()) && Objects.equals(getIso2(), - country.getIso2()) && Objects.equals(getZoneName(), country.getZoneName()) - && Objects.equals(getPhoneCode(), country.getPhoneCode()) && Objects.equals( - getNumericCode(), country.getNumericCode()) && Objects.equals(getCapital(), - country.getCapital()) && Objects.equals(getCurrency(), country.getCurrency()) - && Objects.equals(getCurrencyName(), country.getCurrencyName()) && Objects.equals( - getCurrencySymbol(), country.getCurrencySymbol()) && Objects.equals(getTld(), - country.getTld()) && Objects.equals(getNativeName(), country.getNativeName()) - && Objects.equals(getRegion(), country.getRegion()) && Objects.equals(getRegionId(), - country.getRegionId()) && Objects.equals(getSubregion(), country.getSubregion()) - && Objects.equals(getSubregionId(), country.getSubregionId()) && Objects.equals(getStates(), - country.getStates()) && Objects.equals(getNationality(), country.getNationality()) - && Objects.equals(getTimezones(), country.getTimezones()) && Objects.equals( - getTranslations(), country.getTranslations()) && Objects.equals(getLatitude(), - country.getLatitude()) && Objects.equals(getLongitude(), country.getLongitude()) - && Objects.equals(getEmoji(), country.getEmoji()) && Objects.equals(getEmojiU(), - country.getEmojiU()); - } - + return Objects.equals(getId(), country.getId()) && Objects.equals(getName(), + country.getName()) && Objects.equals(getIso3(), country.getIso3()) + && Objects.equals(getIso2(), country.getIso2()) && Objects.equals( + getZoneName(), country.getZoneName()) && Objects.equals(getPhoneCode(), + country.getPhoneCode()) && Objects.equals(getNumericCode(), + country.getNumericCode()) && Objects.equals(getCapital(), country.getCapital()) + && Objects.equals(getCurrency(), country.getCurrency()) && Objects.equals( + getCurrencyName(), country.getCurrencyName()) && Objects.equals(getCurrencySymbol(), + country.getCurrencySymbol()) && Objects.equals(getTld(), country.getTld()) + && Objects.equals(getNativeName(), country.getNativeName()) + && Objects.equals(getRegion(), country.getRegion()) && Objects.equals( + getRegionId(), country.getRegionId()) && Objects.equals(getSubregion(), + country.getSubregion()) && Objects.equals(getSubregionId(), + country.getSubregionId()) && Objects.equals(getStates(), country.getStates()) + && Objects.equals(getNationality(), country.getNationality()) + && Objects.equals(getTimezones(), country.getTimezones()) + && Objects.equals(getTranslations(), country.getTranslations()) + && Objects.equals(getLatitude(), country.getLatitude()) && Objects.equals( + getLongitude(), country.getLongitude()) && Objects.equals(getEmoji(), + country.getEmoji()) && Objects.equals(getEmojiU(), country.getEmojiU()); + } + + @Generated("IntelliJ") @Override public int hashCode() { return Objects.hash(getId(), getName(), getIso3(), getIso2(), getZoneName(), getPhoneCode(), diff --git a/location4j/src/main/java/com/tomaytotomato/model/Location.java b/location4j/src/main/java/com/tomaytotomato/model/Location.java index 92800d5..7561105 100644 --- a/location4j/src/main/java/com/tomaytotomato/model/Location.java +++ b/location4j/src/main/java/com/tomaytotomato/model/Location.java @@ -46,7 +46,7 @@ public Location() { * @param latitude the latitude of the location * @param longitude the longitude of the location */ - public Location(String countryName, Integer countryId, String countryIso2Code, + private Location(String countryName, Integer countryId, String countryIso2Code, String countryIso3Code, String state, Integer stateId, String stateCode, String stateName, String city, Integer cityId, BigDecimal latitude, BigDecimal longitude) { @@ -72,98 +72,50 @@ public String getCountryName() { return countryName; } - public void setCountryName(String countryName) { - this.countryName = countryName; - } - public Integer getCountryId() { return countryId; } - public void setCountryId(Integer countryId) { - this.countryId = countryId; - } - public String getCountryIso2Code() { return countryIso2Code; } - public void setCountryIso2Code(String countryIso2Code) { - this.countryIso2Code = countryIso2Code; - } - public String getCountryIso3Code() { return countryIso3Code; } - public void setCountryIso3Code(String countryIso3Code) { - this.countryIso3Code = countryIso3Code; - } - public String getState() { return state; } - public void setState(String state) { - this.state = state; - } - public Integer getStateId() { return stateId; } - public void setStateId(Integer stateId) { - this.stateId = stateId; - } - public String getStateCode() { return stateCode; } - public void setStateCode(String stateCode) { - this.stateCode = stateCode; - } - public String getStateName() { return stateName; } - public void setStateName(String stateName) { - this.stateName = stateName; - } - public String getCity() { return city; } - public void setCity(String city) { - this.city = city; - } - public Integer getCityId() { return cityId; } - public void setCityId(Integer cityId) { - this.cityId = cityId; - } - public BigDecimal getLatitude() { return latitude; } - public void setLatitude(BigDecimal latitude) { - this.latitude = latitude; - } - public BigDecimal getLongitude() { return longitude; } - public void setLongitude(BigDecimal longitude) { - this.longitude = longitude; - } - public static class Builder { private String countryName; diff --git a/location4j/src/main/java/com/tomaytotomato/model/State.java b/location4j/src/main/java/com/tomaytotomato/model/State.java index bf98baa..c41d6f8 100644 --- a/location4j/src/main/java/com/tomaytotomato/model/State.java +++ b/location4j/src/main/java/com/tomaytotomato/model/State.java @@ -4,6 +4,7 @@ import java.math.BigDecimal; import java.util.List; import java.util.Objects; +import javax.annotation.processing.Generated; /** * Represents a state data class. @@ -49,7 +50,7 @@ public class State implements Serializable { * @param latitude the latitude of the state's geographic center * @param longitude the longitude of the state's geographic center */ - public State(Integer id, Integer countryId, String countryName, String countryIso2Code, + private State(Integer id, Integer countryId, String countryName, String countryIso2Code, String countryIso3Code, String name, String type, String stateCode, List cities, BigDecimal latitude, BigDecimal longitude) { this.id = id; @@ -69,90 +70,145 @@ public Integer getId() { return id; } - public void setId(Integer id) { - this.id = id; - } - public Integer getCountryId() { return countryId; } - public void setCountryId(Integer countryId) { - this.countryId = countryId; - } - public String getCountryName() { return countryName; } - public void setCountryName(String countryName) { - this.countryName = countryName; - } - public String getCountryIso2Code() { return countryIso2Code; } - public void setCountryIso2Code(String countryIso2Code) { - this.countryIso2Code = countryIso2Code; - } - public String getCountryIso3Code() { return countryIso3Code; } - public void setCountryIso3Code(String countryIso3Code) { - this.countryIso3Code = countryIso3Code; - } - public String getName() { return name; } - public void setName(String name) { - this.name = name; - } - public String getType() { return type; } - public void setType(String type) { - this.type = type; - } - public String getStateCode() { return stateCode; } - public void setStateCode(String stateCode) { - this.stateCode = stateCode; - } - public List getCities() { return cities; } - public void setCities(List cities) { - this.cities = cities; - } - public BigDecimal getLatitude() { return latitude; } - public void setLatitude(BigDecimal latitude) { - this.latitude = latitude; - } - public BigDecimal getLongitude() { return longitude; } - public void setLongitude(BigDecimal longitude) { - this.longitude = longitude; + public void setCountryId(Integer id) { + this.countryId = id; + } + + public void setCountryName(String countryName) { + this.countryName = countryName; + } + + public void setCountryIso2Code(String countryIso2Code) { + this.countryIso2Code = countryIso2Code; + } + + public void setCountryIso3Code(String countryIso3Code) { + this.countryIso3Code = countryIso3Code; + } + + public static final class Builder { + + private Integer id; + private Integer countryId; + private String countryName; + private String countryIso2Code; + private String countryIso3Code; + private String name; + private String type; + private String stateCode; + private List cities; + private BigDecimal latitude; + private BigDecimal longitude; + + private Builder() { + } + + public static Builder aState() { + return new Builder(); + } + + public Builder id(Integer id) { + this.id = id; + return this; + } + + public Builder countryId(Integer countryId) { + this.countryId = countryId; + return this; + } + + public Builder countryName(String countryName) { + this.countryName = countryName; + return this; + } + + public Builder countryIso2Code(String countryIso2Code) { + this.countryIso2Code = countryIso2Code; + return this; + } + + public Builder countryIso3Code(String countryIso3Code) { + this.countryIso3Code = countryIso3Code; + return this; + } + + public Builder name(String name) { + this.name = name; + return this; + } + + public Builder type(String type) { + this.type = type; + return this; + } + + public Builder stateCode(String stateCode) { + this.stateCode = stateCode; + return this; + } + + public Builder cities(List cities) { + this.cities = cities; + return this; + } + + public Builder latitude(BigDecimal latitude) { + this.latitude = latitude; + return this; + } + + public Builder longitude(BigDecimal longitude) { + this.longitude = longitude; + return this; + } + + public State build() { + return new State(id, countryId, countryName, countryIso2Code, countryIso3Code, name, type, + stateCode, cities, latitude, longitude); + } } + @Generated("IntelliJ") @Override public boolean equals(Object o) { if (this == o) { @@ -173,6 +229,7 @@ public boolean equals(Object o) { && Objects.equals(getLongitude(), state.getLongitude()); } + @Generated("IntelliJ") @Override public int hashCode() { return Objects.hash(getId(), getCountryId(), getCountryName(), getCountryIso2Code(), diff --git a/location4j/src/main/java/com/tomaytotomato/model/TimeZone.java b/location4j/src/main/java/com/tomaytotomato/model/TimeZone.java index 065dbea..729c6ba 100644 --- a/location4j/src/main/java/com/tomaytotomato/model/TimeZone.java +++ b/location4j/src/main/java/com/tomaytotomato/model/TimeZone.java @@ -2,6 +2,7 @@ import java.io.Serializable; import java.util.Objects; +import javax.annotation.processing.Generated; /** * Represents a timezone data class. @@ -48,42 +49,68 @@ public String getZoneName() { return zoneName; } - public void setZoneName(String zoneName) { - this.zoneName = zoneName; - } - public String getAbbreviation() { return abbreviation; } - public void setAbbreviation(String abbreviation) { - this.abbreviation = abbreviation; - } - public String getTzName() { return tzName; } - public void setTzName(String tzName) { - this.tzName = tzName; - } - public Integer getGmtOffset() { return gmtOffset; } - public void setGmtOffset(Integer gmtOffset) { - this.gmtOffset = gmtOffset; - } - public String getGmtOffsetName() { return gmtOffsetName; } - public void setGmtOffsetName(String gmtOffsetName) { - this.gmtOffsetName = gmtOffsetName; + public static final class Builder { + + private String zoneName; + private String abbreviation; + private String tzName; + private Integer gmtOffset; + private String gmtOffsetName; + + private Builder() { + } + + public static Builder aTimeZone() { + return new Builder(); + } + + public Builder zoneName(String zoneName) { + this.zoneName = zoneName; + return this; + } + + public Builder abbreviation(String abbreviation) { + this.abbreviation = abbreviation; + return this; + } + + public Builder tzName(String tzName) { + this.tzName = tzName; + return this; + } + + public Builder gmtOffset(Integer gmtOffset) { + this.gmtOffset = gmtOffset; + return this; + } + + public Builder gmtOffsetName(String gmtOffsetName) { + this.gmtOffsetName = gmtOffsetName; + return this; + } + + public TimeZone build() { + return new TimeZone(zoneName, abbreviation, tzName, gmtOffset, gmtOffsetName); + } } + @Generated("IntelliJ") @Override public boolean equals(Object o) { if (this == o) { @@ -93,12 +120,14 @@ public boolean equals(Object o) { return false; } TimeZone timeZone = (TimeZone) o; - return Objects.equals(getZoneName(), timeZone.getZoneName()) && Objects.equals( - getAbbreviation(), timeZone.getAbbreviation()) && Objects.equals(getTzName(), - timeZone.getTzName()) && Objects.equals(getGmtOffset(), timeZone.getGmtOffset()) - && Objects.equals(getGmtOffsetName(), timeZone.getGmtOffsetName()); + return Objects.equals(getZoneName(), timeZone.getZoneName()) + && Objects.equals(getAbbreviation(), timeZone.getAbbreviation()) + && Objects.equals(getTzName(), timeZone.getTzName()) && Objects.equals( + getGmtOffset(), timeZone.getGmtOffset()) && Objects.equals(getGmtOffsetName(), + timeZone.getGmtOffsetName()); } + @Generated("IntelliJ") @Override public int hashCode() { return Objects.hash(getZoneName(), getAbbreviation(), getTzName(), getGmtOffset(), From 0ce17a76f72dcc257cc1e7d63ce032a03ec055d1 Mon Sep 17 00:00:00 2001 From: tomaytotomato <971697+tomaytotomato@users.noreply.github.com> Date: Sun, 11 Aug 2024 11:05:55 +0100 Subject: [PATCH 2/3] Sonar fixing warnings and code tidy --- .github/workflows/build.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index b5c9b23..fd01a27 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -5,7 +5,7 @@ on: branches: - master paths: - - 'library/src/**' + - 'location4j/src/**' - 'pom.xml' - '.github/**' @@ -13,7 +13,7 @@ on: branches: - master paths: - - 'library/src/**' + - 'location4j/src/**' - 'pom.xml' - '.github/**' From 3030878b4fd1e86d3b244a8947db28147855bda0 Mon Sep 17 00:00:00 2001 From: tomaytotomato <971697+tomaytotomato@users.noreply.github.com> Date: Sun, 11 Aug 2024 12:13:04 +0100 Subject: [PATCH 3/3] Merge conflicts --- .../java/com/tomaytotomato/model/City.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/location4j/src/main/java/com/tomaytotomato/model/City.java b/location4j/src/main/java/com/tomaytotomato/model/City.java index 8a0c65f..d6daf67 100644 --- a/location4j/src/main/java/com/tomaytotomato/model/City.java +++ b/location4j/src/main/java/com/tomaytotomato/model/City.java @@ -27,6 +27,8 @@ public class City implements Serializable { private String name; private BigDecimal latitude; private BigDecimal longitude; + private double latitudeDouble = 0.0; + private double longitudeDouble = 0.0; /** * Default constructor for City. @@ -86,6 +88,24 @@ public String getName() { return name; } + public void setName(String name) { + this.name = name; + } + + public double getLatitudeDouble() { + if (latitudeDouble == 0.0) { + latitudeDouble = latitude.doubleValue(); + } + return latitudeDouble; + } + + public double getLongitudeDouble() { + if (longitudeDouble == 0.0) { + longitudeDouble = longitude.doubleValue(); + } + return longitudeDouble; + } + public BigDecimal getLatitude() { return latitude; }