@@ -201,87 +201,121 @@ protected Map<String, Object> transform(final AsnResponse response) {
201
201
}
202
202
}
203
203
204
- static class City extends AbstractBase <CityResponse , CityResponse > {
204
+ record CacheableCityResponse (
205
+ Boolean isInEuropeanUnion ,
206
+ String countryIsoCode ,
207
+ String countryName ,
208
+ String continentCode ,
209
+ String continentName ,
210
+ String regionIsoCode ,
211
+ String regionName ,
212
+ String cityName ,
213
+ String timezone ,
214
+ Double latitude ,
215
+ Double longitude ,
216
+ Integer accuracyRadius ,
217
+ String postalCode ,
218
+ Boolean registeredCountryIsInEuropeanUnion ,
219
+ String registeredCountryIsoCode ,
220
+ String registeredCountryName
221
+ ) {}
222
+
223
+ static class City extends AbstractBase <CityResponse , Result <CacheableCityResponse >> {
205
224
City (final Set <Database .Property > properties ) {
206
225
super (properties , CityResponse .class , CityResponse ::new );
207
226
}
208
227
209
228
@ Override
210
- protected CityResponse cacheableRecord (CityResponse response ) {
211
- return response ;
229
+ protected Result <CacheableCityResponse > cacheableRecord (CityResponse response ) {
230
+ final com .maxmind .geoip2 .record .Country country = response .getCountry ();
231
+ final Continent continent = response .getContinent ();
232
+ final Subdivision subdivision = response .getMostSpecificSubdivision ();
233
+ final com .maxmind .geoip2 .record .City city = response .getCity ();
234
+ final Location location = response .getLocation ();
235
+ final Postal postal = response .getPostal ();
236
+ final com .maxmind .geoip2 .record .Country registeredCountry = response .getRegisteredCountry ();
237
+ final Traits traits = response .getTraits ();
238
+
239
+ return new Result <>(
240
+ new CacheableCityResponse (
241
+ isInEuropeanUnion (country ),
242
+ country .getIsoCode (),
243
+ country .getName (),
244
+ continent .getCode (),
245
+ continent .getName (),
246
+ regionIsoCode (country , subdivision ),
247
+ subdivision .getName (),
248
+ city .getName (),
249
+ location .getTimeZone (),
250
+ location .getLatitude (),
251
+ location .getLongitude (),
252
+ location .getAccuracyRadius (),
253
+ postal .getCode (),
254
+ isInEuropeanUnion (registeredCountry ),
255
+ registeredCountry .getIsoCode (),
256
+ registeredCountry .getName ()
257
+ ),
258
+ traits .getIpAddress (),
259
+ traits .getNetwork ().toString ()
260
+ );
212
261
}
213
262
214
263
@ Override
215
- protected Map <String , Object > transform (final CityResponse response ) {
216
- com .maxmind .geoip2 .record .Country country = response .getCountry ();
217
- com .maxmind .geoip2 .record .Country registeredCountry = response .getRegisteredCountry ();
218
- com .maxmind .geoip2 .record .City city = response .getCity ();
219
- Location location = response .getLocation ();
220
- Continent continent = response .getContinent ();
221
- Subdivision subdivision = response .getMostSpecificSubdivision ();
222
- Postal postal = response .getPostal ();
264
+ protected Map <String , Object > transform (final Result <CacheableCityResponse > result ) {
265
+ CacheableCityResponse response = result .result ();
223
266
224
267
Map <String , Object > data = new HashMap <>();
225
268
for (Database .Property property : this .properties ) {
226
269
switch (property ) {
227
- case IP -> data .put ("ip" , response . getTraits (). getIpAddress ());
270
+ case IP -> data .put ("ip" , result . ip ());
228
271
case COUNTRY_IN_EUROPEAN_UNION -> {
229
- Boolean isInEuropeanUnion = isInEuropeanUnion (country );
230
- if (isInEuropeanUnion != null ) {
231
- data .put ("country_in_european_union" , isInEuropeanUnion );
272
+ if (response .isInEuropeanUnion != null ) {
273
+ data .put ("country_in_european_union" , response .isInEuropeanUnion );
232
274
}
233
275
}
234
276
case COUNTRY_ISO_CODE -> {
235
- String countryIsoCode = country .getIsoCode ();
236
- if (countryIsoCode != null ) {
237
- data .put ("country_iso_code" , countryIsoCode );
277
+ if (response .countryIsoCode != null ) {
278
+ data .put ("country_iso_code" , response .countryIsoCode );
238
279
}
239
280
}
240
281
case COUNTRY_NAME -> {
241
- String countryName = country .getName ();
242
- if (countryName != null ) {
243
- data .put ("country_name" , countryName );
282
+ if (response .countryName != null ) {
283
+ data .put ("country_name" , response .countryName );
244
284
}
245
285
}
246
286
case CONTINENT_CODE -> {
247
- String continentCode = continent .getCode ();
248
- if (continentCode != null ) {
249
- data .put ("continent_code" , continentCode );
287
+ if (response .continentCode != null ) {
288
+ data .put ("continent_code" , response .continentCode );
250
289
}
251
290
}
252
291
case CONTINENT_NAME -> {
253
- String continentName = continent .getName ();
254
- if (continentName != null ) {
255
- data .put ("continent_name" , continentName );
292
+ if (response .continentName != null ) {
293
+ data .put ("continent_name" , response .continentName );
256
294
}
257
295
}
258
296
case REGION_ISO_CODE -> {
259
- String regionIsoCode = regionIsoCode (country , subdivision );
260
- if (regionIsoCode != null ) {
261
- data .put ("region_iso_code" , regionIsoCode );
297
+ if (response .regionIsoCode != null ) {
298
+ data .put ("region_iso_code" , response .regionIsoCode );
262
299
}
263
300
}
264
301
case REGION_NAME -> {
265
- String subdivisionName = subdivision .getName ();
266
- if (subdivisionName != null ) {
267
- data .put ("region_name" , subdivisionName );
302
+ if (response .regionName != null ) {
303
+ data .put ("region_name" , response .regionName );
268
304
}
269
305
}
270
306
case CITY_NAME -> {
271
- String cityName = city .getName ();
272
- if (cityName != null ) {
273
- data .put ("city_name" , cityName );
307
+ if (response .cityName != null ) {
308
+ data .put ("city_name" , response .cityName );
274
309
}
275
310
}
276
311
case TIMEZONE -> {
277
- String locationTimeZone = location .getTimeZone ();
278
- if (locationTimeZone != null ) {
279
- data .put ("timezone" , locationTimeZone );
312
+ if (response .timezone != null ) {
313
+ data .put ("timezone" , response .timezone );
280
314
}
281
315
}
282
316
case LOCATION -> {
283
- Double latitude = location . getLatitude () ;
284
- Double longitude = location . getLongitude () ;
317
+ Double latitude = response . latitude ;
318
+ Double longitude = response . longitude ;
285
319
if (latitude != null && longitude != null ) {
286
320
Map <String , Object > locationObject = new HashMap <>();
287
321
locationObject .put ("lat" , latitude );
@@ -290,30 +324,28 @@ protected Map<String, Object> transform(final CityResponse response) {
290
324
}
291
325
}
292
326
case ACCURACY_RADIUS -> {
293
- Integer accuracyRadius = location .getAccuracyRadius ();
294
- if (accuracyRadius != null ) {
295
- data .put ("accuracy_radius" , accuracyRadius );
327
+ if (response .accuracyRadius != null ) {
328
+ data .put ("accuracy_radius" , response .accuracyRadius );
296
329
}
297
330
}
298
331
case POSTAL_CODE -> {
299
- if (postal . getCode () != null ) {
300
- data .put ("postal_code" , postal . getCode () );
332
+ if (response . postalCode != null ) {
333
+ data .put ("postal_code" , response . postalCode );
301
334
}
302
335
}
303
336
case REGISTERED_COUNTRY_IN_EUROPEAN_UNION -> {
304
- Boolean isInEuropeanUnion = isInEuropeanUnion (registeredCountry );
305
- if (isInEuropeanUnion != null ) {
306
- data .put ("registered_country_in_european_union" , isInEuropeanUnion );
337
+ if (response .registeredCountryIsInEuropeanUnion != null ) {
338
+ data .put ("registered_country_in_european_union" , response .registeredCountryIsInEuropeanUnion );
307
339
}
308
340
}
309
341
case REGISTERED_COUNTRY_ISO_CODE -> {
310
- if (registeredCountry . getIsoCode () != null ) {
311
- data .put ("registered_country_iso_code" , registeredCountry . getIsoCode () );
342
+ if (response . registeredCountryIsoCode != null ) {
343
+ data .put ("registered_country_iso_code" , response . registeredCountryIsoCode );
312
344
}
313
345
}
314
346
case REGISTERED_COUNTRY_NAME -> {
315
- if (registeredCountry . getName () != null ) {
316
- data .put ("registered_country_name" , registeredCountry . getName () );
347
+ if (response . registeredCountryName != null ) {
348
+ data .put ("registered_country_name" , response . registeredCountryName );
317
349
}
318
350
}
319
351
}
0 commit comments