Skip to content

Commit fc59422

Browse files
committed
Move model and record classes to records
This will eventually reduce boilerplate and provide a more consistent experience for users. I am leaving the old getter methods as deprecated for one major version to give users the ability to upgrade before having to migrate to the updated accessors. There are other breaking changes, but these will likely not affect most users.
1 parent bf1dda7 commit fc59422

36 files changed

+2448
-1530
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@ CHANGELOG
44
5.0.0
55
------------------
66

7+
* **BREAKING:** All model and record classes have been converted to Java records.
8+
This provides a more modern, immutable data model with automatic implementations
9+
of `equals()`, `hashCode()`, and `toString()`. The abstract classes
10+
`AbstractRecord`, `AbstractNamedRecord`, `AbstractResponse`,
11+
`AbstractCountryResponse`, `AbstractCityResponse`, and `IpBaseResponse` have
12+
been removed. Record components can be accessed using the new accessor methods
13+
(e.g., `city()`, `country()`, `location()`). The traditional getter methods
14+
(e.g., `getCity()`, `getCountry()`, `getLocation()`) are still available but
15+
have been deprecated and will be removed in version 6.0.0.
16+
* **BREAKING:** `RepresentedCountry` is now a separate record type instead of
17+
extending `Country`. It shares the same fields as `Country` but adds a `type`
18+
field.
719
* The deprecation notices for IP Risk database support have been removed.
820
IP Risk database support will continue to be maintained.
921
* **BREAKING:** The deprecated `WebServiceClient.Builder` methods

src/main/java/com/maxmind/geoip2/record/AbstractRecord.java renamed to src/main/java/com/maxmind/geoip2/JsonSerializable.java

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,31 @@
1-
package com.maxmind.geoip2.record;
1+
package com.maxmind.geoip2;
22

33
import com.fasterxml.jackson.annotation.JsonInclude;
44
import com.fasterxml.jackson.databind.MapperFeature;
55
import com.fasterxml.jackson.databind.json.JsonMapper;
6+
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
67
import java.io.IOException;
78

89
/**
9-
* Abstract class for GeoIP2.
10+
* Interface for classes that can be serialized to JSON.
11+
* Provides default implementation for toJson() method.
1012
*/
11-
public abstract class AbstractRecord {
13+
public interface JsonSerializable {
1214

1315
/**
1416
* @return JSON representation of this object. The structure is the same as
1517
* the JSON provided by the GeoIP2 web service.
1618
* @throws IOException if there is an error serializing the object to JSON.
1719
*/
18-
public String toJson() throws IOException {
20+
default String toJson() throws IOException {
1921
JsonMapper mapper = JsonMapper.builder()
2022
.disable(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS)
23+
.addModule(new JavaTimeModule())
2124
.serializationInclusion(JsonInclude.Include.NON_NULL)
2225
.serializationInclusion(JsonInclude.Include.NON_EMPTY)
2326
.build();
2427

2528
return mapper.writeValueAsString(this);
2629
}
2730

28-
@Override
29-
public String toString() {
30-
// This exception should never happen. If it does happen, we did
31-
// something wrong.
32-
try {
33-
return getClass().getName() + " [ " + toJson() + " ]";
34-
} catch (IOException e) {
35-
throw new RuntimeException(e);
36-
}
37-
}
3831
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.maxmind.geoip2;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnore;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
/**
9+
* Interface for record classes that have localized names and GeoName IDs.
10+
* Provides a default implementation for the name() method that returns the name
11+
* in the first available locale.
12+
*/
13+
public interface NamedRecord extends JsonSerializable {
14+
15+
/**
16+
* @return The GeoName ID for this location.
17+
*/
18+
@JsonProperty("geoname_id")
19+
Long geonameId();
20+
21+
/**
22+
* @return A {@link Map} from locale codes to the name in that locale.
23+
*/
24+
@JsonProperty("names")
25+
Map<String, String> names();
26+
27+
/**
28+
* @return The list of locales to use for name lookups.
29+
*/
30+
@JsonIgnore
31+
List<String> locales();
32+
33+
/**
34+
* @return The name based on the locales list. Returns the name in the first
35+
* locale for which a name is available. If no name is available in any of the
36+
* specified locales, returns null.
37+
*/
38+
@JsonIgnore
39+
default String name() {
40+
for (String lang : locales()) {
41+
if (names().containsKey(lang)) {
42+
return names().get(lang);
43+
}
44+
}
45+
return null;
46+
}
47+
}

src/main/java/com/maxmind/geoip2/model/AbstractCityResponse.java

Lines changed: 0 additions & 131 deletions
This file was deleted.

src/main/java/com/maxmind/geoip2/model/AbstractCountryResponse.java

Lines changed: 0 additions & 108 deletions
This file was deleted.

src/main/java/com/maxmind/geoip2/model/AbstractResponse.java

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)