Skip to content

Commit 33c09f2

Browse files
oschwaldclaude
andcommitted
Replace explicit types with var for local variables
This modernizes the codebase by using Java's var keyword for local variables where the type is obvious from the initializer. This includes: - Local variable declarations with initializers - Enhanced for-loop variables - Try-with-resources variables Some try-block variables were refactored into helper methods to enable var usage (parsePrefixLength in NetworkDeserializer and readBody in WebServiceClient). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 9d5369d commit 33c09f2

File tree

7 files changed

+68
-64
lines changed

7 files changed

+68
-64
lines changed

src/main/java/com/maxmind/geoip2/DatabaseReader.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ private DatabaseReader(Builder builder) throws IOException {
115115
}
116116

117117
private int getDatabaseType() {
118-
String databaseType = this.metadata().databaseType();
119-
int type = 0;
118+
var databaseType = this.metadata().databaseType();
119+
var type = 0;
120120
if (databaseType.contains("GeoIP2-Anonymous-IP")) {
121121
type |= DatabaseType.ANONYMOUS_IP.type;
122122
}
@@ -258,9 +258,9 @@ private <T> LookupResult<T> get(InetAddress ipAddress, Class<T> cls,
258258
+ " database using the " + caller + " method");
259259
}
260260

261-
DatabaseRecord<T> record = reader.getRecord(ipAddress, cls);
261+
var record = reader.getRecord(ipAddress, cls);
262262

263-
T o = record.data();
263+
var o = record.data();
264264

265265
return new LookupResult<>(o, ipAddress.getHostAddress(), record.network());
266266
}
@@ -281,8 +281,8 @@ private <T> Optional<T> getResponse(
281281
DatabaseType expectedType,
282282
String caller
283283
) throws IOException {
284-
LookupResult<T> result = this.get(ipAddress, cls, expectedType, caller);
285-
T response = result.model();
284+
var result = this.get(ipAddress, cls, expectedType, caller);
285+
var response = result.model();
286286
if (response == null) {
287287
return Optional.empty();
288288
}
@@ -311,7 +311,7 @@ public void close() throws IOException {
311311
@Override
312312
public CountryResponse country(InetAddress ipAddress) throws IOException,
313313
GeoIp2Exception {
314-
Optional<CountryResponse> r = tryCountry(ipAddress);
314+
var r = tryCountry(ipAddress);
315315
if (r.isEmpty()) {
316316
throw new AddressNotFoundException("The address "
317317
+ ipAddress.getHostAddress() + " is not in the database.");
@@ -322,7 +322,7 @@ public CountryResponse country(InetAddress ipAddress) throws IOException,
322322
@Override
323323
public Optional<CountryResponse> tryCountry(InetAddress ipAddress) throws IOException,
324324
GeoIp2Exception {
325-
Optional<CountryResponse> response = getResponse(
325+
var response = getResponse(
326326
ipAddress,
327327
CountryResponse.class,
328328
DatabaseType.COUNTRY,
@@ -334,7 +334,7 @@ public Optional<CountryResponse> tryCountry(InetAddress ipAddress) throws IOExce
334334
@Override
335335
public CityResponse city(InetAddress ipAddress) throws IOException,
336336
GeoIp2Exception {
337-
Optional<CityResponse> r = tryCity(ipAddress);
337+
var r = tryCity(ipAddress);
338338
if (r.isEmpty()) {
339339
throw new AddressNotFoundException("The address "
340340
+ ipAddress.getHostAddress() + " is not in the database.");
@@ -345,7 +345,7 @@ public CityResponse city(InetAddress ipAddress) throws IOException,
345345
@Override
346346
public Optional<CityResponse> tryCity(InetAddress ipAddress) throws IOException,
347347
GeoIp2Exception {
348-
Optional<CityResponse> response = getResponse(
348+
var response = getResponse(
349349
ipAddress,
350350
CityResponse.class,
351351
DatabaseType.CITY,
@@ -365,7 +365,7 @@ public Optional<CityResponse> tryCity(InetAddress ipAddress) throws IOException,
365365
@Override
366366
public AnonymousIpResponse anonymousIp(InetAddress ipAddress) throws IOException,
367367
GeoIp2Exception {
368-
Optional<AnonymousIpResponse> r = tryAnonymousIp(ipAddress);
368+
var r = tryAnonymousIp(ipAddress);
369369
if (r.isEmpty()) {
370370
throw new AddressNotFoundException("The address "
371371
+ ipAddress.getHostAddress() + " is not in the database.");
@@ -395,7 +395,7 @@ public Optional<AnonymousIpResponse> tryAnonymousIp(InetAddress ipAddress) throw
395395
@Override
396396
public AnonymousPlusResponse anonymousPlus(InetAddress ipAddress) throws IOException,
397397
GeoIp2Exception {
398-
Optional<AnonymousPlusResponse> r = tryAnonymousPlus(ipAddress);
398+
var r = tryAnonymousPlus(ipAddress);
399399
if (r.isEmpty()) {
400400
throw new AddressNotFoundException("The address "
401401
+ ipAddress.getHostAddress() + " is not in the database.");
@@ -427,7 +427,7 @@ public Optional<AnonymousPlusResponse> tryAnonymousPlus(InetAddress ipAddress)
427427
@Override
428428
public IpRiskResponse ipRisk(InetAddress ipAddress) throws IOException,
429429
GeoIp2Exception {
430-
Optional<IpRiskResponse> r = tryIpRisk(ipAddress);
430+
var r = tryIpRisk(ipAddress);
431431
if (r.isEmpty()) {
432432
throw new AddressNotFoundException("The address "
433433
+ ipAddress.getHostAddress() + " is not in the database.");
@@ -452,7 +452,7 @@ public Optional<IpRiskResponse> tryIpRisk(InetAddress ipAddress) throws IOExcept
452452
@Override
453453
public AsnResponse asn(InetAddress ipAddress) throws IOException,
454454
GeoIp2Exception {
455-
Optional<AsnResponse> r = tryAsn(ipAddress);
455+
var r = tryAsn(ipAddress);
456456
if (r.isEmpty()) {
457457
throw new AddressNotFoundException("The address "
458458
+ ipAddress.getHostAddress() + " is not in the database.");
@@ -477,7 +477,7 @@ public Optional<AsnResponse> tryAsn(InetAddress ipAddress) throws IOException,
477477
@Override
478478
public ConnectionTypeResponse connectionType(InetAddress ipAddress)
479479
throws IOException, GeoIp2Exception {
480-
Optional<ConnectionTypeResponse> r = tryConnectionType(ipAddress);
480+
var r = tryConnectionType(ipAddress);
481481
if (r.isEmpty()) {
482482
throw new AddressNotFoundException("The address "
483483
+ ipAddress.getHostAddress() + " is not in the database.");
@@ -507,7 +507,7 @@ public Optional<ConnectionTypeResponse> tryConnectionType(InetAddress ipAddress)
507507
@Override
508508
public DomainResponse domain(InetAddress ipAddress) throws IOException,
509509
GeoIp2Exception {
510-
Optional<DomainResponse> r = tryDomain(ipAddress);
510+
var r = tryDomain(ipAddress);
511511
if (r.isEmpty()) {
512512
throw new AddressNotFoundException("The address "
513513
+ ipAddress.getHostAddress() + " is not in the database.");
@@ -532,7 +532,7 @@ public Optional<DomainResponse> tryDomain(InetAddress ipAddress) throws IOExcept
532532
@Override
533533
public EnterpriseResponse enterprise(InetAddress ipAddress) throws IOException,
534534
GeoIp2Exception {
535-
Optional<EnterpriseResponse> r = tryEnterprise(ipAddress);
535+
var r = tryEnterprise(ipAddress);
536536
if (r.isEmpty()) {
537537
throw new AddressNotFoundException("The address "
538538
+ ipAddress.getHostAddress() + " is not in the database.");
@@ -543,7 +543,7 @@ public EnterpriseResponse enterprise(InetAddress ipAddress) throws IOException,
543543
@Override
544544
public Optional<EnterpriseResponse> tryEnterprise(InetAddress ipAddress) throws IOException,
545545
GeoIp2Exception {
546-
Optional<EnterpriseResponse> response = getResponse(
546+
var response = getResponse(
547547
ipAddress,
548548
EnterpriseResponse.class,
549549
DatabaseType.ENTERPRISE,
@@ -563,7 +563,7 @@ public Optional<EnterpriseResponse> tryEnterprise(InetAddress ipAddress) throws
563563
@Override
564564
public IspResponse isp(InetAddress ipAddress) throws IOException,
565565
GeoIp2Exception {
566-
Optional<IspResponse> r = tryIsp(ipAddress);
566+
var r = tryIsp(ipAddress);
567567
if (r.isEmpty()) {
568568
throw new AddressNotFoundException("The address "
569569
+ ipAddress.getHostAddress() + " is not in the database.");

src/main/java/com/maxmind/geoip2/InetAddressDeserializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public InetAddressDeserializer() {
2121
@Override
2222
public InetAddress deserialize(JsonParser p, DeserializationContext ctxt)
2323
throws IOException {
24-
String value = p.getValueAsString();
24+
var value = p.getValueAsString();
2525
if (value == null || value.isEmpty()) {
2626
return null;
2727
}

src/main/java/com/maxmind/geoip2/NamedRecord.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public interface NamedRecord extends JsonSerializable {
3737
*/
3838
@JsonIgnore
3939
default String name() {
40-
for (String lang : locales()) {
40+
for (var lang : locales()) {
4141
if (names().containsKey(lang)) {
4242
return names().get(lang);
4343
}

src/main/java/com/maxmind/geoip2/NetworkDeserializer.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,21 @@ public NetworkDeserializer(Class<?> vc) {
3333
public Network deserialize(JsonParser jsonparser, DeserializationContext context)
3434
throws IOException {
3535

36-
final String cidr = jsonparser.getValueAsString();
36+
final var cidr = jsonparser.getValueAsString();
3737
if (cidr == null || cidr.isBlank()) {
3838
return null;
3939
}
4040
return parseCidr(cidr);
4141
}
4242

4343
private static Network parseCidr(String cidr) throws IOException {
44-
final String[] parts = cidr.split("/", 2);
44+
final var parts = cidr.split("/", 2);
4545
if (parts.length != 2) {
4646
throw new IllegalArgumentException("Invalid CIDR format: " + cidr);
4747
}
4848

49-
final String addrPart = parts[0];
50-
final String prefixPart = parts[1];
49+
final var addrPart = parts[0];
50+
final var prefixPart = parts[1];
5151

5252
final InetAddress address;
5353
try {
@@ -56,20 +56,23 @@ private static Network parseCidr(String cidr) throws IOException {
5656
throw new IOException("Unknown host in CIDR: " + cidr, e);
5757
}
5858

59-
final int prefixLength;
60-
try {
61-
prefixLength = Integer.parseInt(prefixPart);
62-
} catch (NumberFormatException e) {
63-
throw new IllegalArgumentException(
64-
"Invalid prefix length in CIDR: " + cidr, e);
65-
}
59+
final var prefixLength = parsePrefixLength(prefixPart, cidr);
6660

67-
final int maxPrefix = (address.getAddress().length == 4) ? 32 : 128;
61+
final var maxPrefix = (address.getAddress().length == 4) ? 32 : 128;
6862
if (prefixLength < 0 || prefixLength > maxPrefix) {
6963
throw new IllegalArgumentException(
7064
"Prefix length out of range (0-" + maxPrefix + ") for CIDR: " + cidr);
7165
}
7266

7367
return new Network(address, prefixLength);
7468
}
69+
70+
private static int parsePrefixLength(String prefixPart, String cidr) {
71+
try {
72+
return Integer.parseInt(prefixPart);
73+
} catch (NumberFormatException e) {
74+
throw new IllegalArgumentException(
75+
"Invalid prefix length in CIDR: " + cidr, e);
76+
}
77+
}
7578
}

src/main/java/com/maxmind/geoip2/WebServiceClient.java

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -356,34 +356,33 @@ public InsightsResponse insights(InetAddress ipAddress) throws IOException,
356356

357357
private <T> T responseFor(String path, InetAddress ipAddress, Class<T> cls)
358358
throws IOException, GeoIp2Exception {
359-
URI uri = createUri(path, ipAddress);
359+
var uri = createUri(path, ipAddress);
360360

361-
HttpRequest request = HttpRequest.newBuilder()
361+
var request = HttpRequest.newBuilder()
362362
.uri(uri)
363363
.timeout(this.requestTimeout)
364364
.header("Accept", "application/json")
365365
.header("Authorization", authHeader)
366366
.header("User-Agent", this.userAgent)
367367
.GET()
368368
.build();
369-
HttpResponse<InputStream> response = null;
370369
try {
371-
response = this.httpClient
370+
var response = this.httpClient
372371
.send(request, HttpResponse.BodyHandlers.ofInputStream());
373-
return handleResponse(response, cls);
374-
} catch (InterruptedException e) {
375-
throw new GeoIp2Exception("Interrupted sending request", e);
376-
} finally {
377-
if (response != null) {
372+
try {
373+
return handleResponse(response, cls);
374+
} finally {
378375
response.body().close();
379376
}
377+
} catch (InterruptedException e) {
378+
throw new GeoIp2Exception("Interrupted sending request", e);
380379
}
381380
}
382381

383382
private <T> T handleResponse(HttpResponse<InputStream> response, Class<T> cls)
384383
throws GeoIp2Exception, IOException {
385-
int status = response.statusCode();
386-
URI uri = response.uri();
384+
var status = response.statusCode();
385+
var uri = response.uri();
387386

388387
if (status >= 400 && status < 500) {
389388
this.handle4xxStatus(response);
@@ -397,7 +396,7 @@ private <T> T handleResponse(HttpResponse<InputStream> response, Class<T> cls)
397396
+ status + ") for " + uri, status, uri);
398397
}
399398

400-
InjectableValues inject = new InjectableValues.Std()
399+
var inject = new InjectableValues.Std()
401400
.addValue("locales", locales);
402401

403402
try {
@@ -410,20 +409,17 @@ private <T> T handleResponse(HttpResponse<InputStream> response, Class<T> cls)
410409

411410
private void handle4xxStatus(HttpResponse<InputStream> response)
412411
throws GeoIp2Exception, IOException {
413-
int status = response.statusCode();
414-
URI uri = response.uri();
415-
416-
String body;
417-
try (InputStream bodyStream = response.body()) {
418-
body = new String(bodyStream.readAllBytes(), StandardCharsets.UTF_8);
419-
if (body.equals("")) {
420-
throw new HttpException("Received a " + status + " error for "
421-
+ uri + " with no body", status, uri);
422-
}
412+
var status = response.statusCode();
413+
var uri = response.uri();
414+
415+
final var body = readBody(response);
416+
if (body.equals("")) {
417+
throw new HttpException("Received a " + status + " error for "
418+
+ uri + " with no body", status, uri);
423419
}
424420

425421
try {
426-
Map<String, String> content = mapper.readValue(body,
422+
var content = mapper.readValue(body,
427423
new TypeReference<HashMap<String, String>>() {
428424
});
429425
handleErrorWithJsonBody(content, body, status, uri);
@@ -439,8 +435,8 @@ private void handle4xxStatus(HttpResponse<InputStream> response)
439435
private static void handleErrorWithJsonBody(Map<String, String> content,
440436
String body, int status, URI uri)
441437
throws GeoIp2Exception, HttpException {
442-
String error = content.get("error");
443-
String code = content.get("code");
438+
var error = content.get("error");
439+
var code = content.get("code");
444440

445441
if (error == null || code == null) {
446442
throw new HttpException(
@@ -471,7 +467,7 @@ private static void handleErrorWithJsonBody(Map<String, String> content,
471467
}
472468

473469
private URI createUri(String service, InetAddress ipAddress) throws GeoIp2Exception {
474-
String path = "/geoip/v2.1/" + service + "/"
470+
var path = "/geoip/v2.1/" + service + "/"
475471
+ (ipAddress == null ? "me" : ipAddress.getHostAddress());
476472
try {
477473
return new URI(
@@ -489,7 +485,7 @@ private URI createUri(String service, InetAddress ipAddress) throws GeoIp2Except
489485
}
490486

491487
private void exhaustBody(HttpResponse<InputStream> response) throws HttpException {
492-
try (InputStream body = response.body()) {
488+
try (var body = response.body()) {
493489
// Make sure we read the stream until the end so that
494490
// the connection can be reused.
495491
while (body.read() != -1) {
@@ -500,6 +496,11 @@ private void exhaustBody(HttpResponse<InputStream> response) throws HttpExceptio
500496
}
501497
}
502498

499+
private static String readBody(HttpResponse<InputStream> response) throws IOException {
500+
try (var bodyStream = response.body()) {
501+
return new String(bodyStream.readAllBytes(), StandardCharsets.UTF_8);
502+
}
503+
}
503504

504505
@Override
505506
public String toString() {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ private static ArrayList<Subdivision> mapSubdivisions(
130130
List<Subdivision> subdivisions,
131131
List<String> locales
132132
) {
133-
ArrayList<Subdivision> subdivisions2 = new ArrayList<>(subdivisions.size());
134-
for (Subdivision subdivision : subdivisions) {
133+
var subdivisions2 = new ArrayList<Subdivision>(subdivisions.size());
134+
for (var subdivision : subdivisions) {
135135
subdivisions2.add(new Subdivision(subdivision, locales));
136136
}
137137
return subdivisions2;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ private static ArrayList<Subdivision> mapSubdivisions(
130130
List<Subdivision> subdivisions,
131131
List<String> locales
132132
) {
133-
ArrayList<Subdivision> subdivisions2 = new ArrayList<>(subdivisions.size());
134-
for (Subdivision subdivision : subdivisions) {
133+
var subdivisions2 = new ArrayList<Subdivision>(subdivisions.size());
134+
for (var subdivision : subdivisions) {
135135
subdivisions2.add(new Subdivision(subdivision, locales));
136136
}
137137
return subdivisions2;

0 commit comments

Comments
 (0)