Skip to content

Commit 79e3d9c

Browse files
oschwaldclaude
andcommitted
Use Optional.orElseThrow() pattern
Replace the manual isEmpty() check and get() pattern with the more functional and concise orElseThrow() method. This eliminates 30 lines of boilerplate code across 10 lookup methods while maintaining the exact same behavior. Before: var r = tryCountry(ipAddress); if (r.isEmpty()) { throw new AddressNotFoundException(...); } return r.get(); After: return tryCountry(ipAddress).orElseThrow(() -> new AddressNotFoundException(...)); 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent f3af96e commit 79e3d9c

File tree

1 file changed

+30
-60
lines changed

1 file changed

+30
-60
lines changed

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

Lines changed: 30 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -311,12 +311,9 @@ public void close() throws IOException {
311311
@Override
312312
public CountryResponse country(InetAddress ipAddress) throws IOException,
313313
GeoIp2Exception {
314-
var r = tryCountry(ipAddress);
315-
if (r.isEmpty()) {
316-
throw new AddressNotFoundException("The address "
317-
+ ipAddress.getHostAddress() + " is not in the database.");
318-
}
319-
return r.get();
314+
return tryCountry(ipAddress).orElseThrow(() ->
315+
new AddressNotFoundException("The address "
316+
+ ipAddress.getHostAddress() + " is not in the database."));
320317
}
321318

322319
@Override
@@ -334,12 +331,9 @@ public Optional<CountryResponse> tryCountry(InetAddress ipAddress) throws IOExce
334331
@Override
335332
public CityResponse city(InetAddress ipAddress) throws IOException,
336333
GeoIp2Exception {
337-
var r = tryCity(ipAddress);
338-
if (r.isEmpty()) {
339-
throw new AddressNotFoundException("The address "
340-
+ ipAddress.getHostAddress() + " is not in the database.");
341-
}
342-
return r.get();
334+
return tryCity(ipAddress).orElseThrow(() ->
335+
new AddressNotFoundException("The address "
336+
+ ipAddress.getHostAddress() + " is not in the database."));
343337
}
344338

345339
@Override
@@ -365,12 +359,9 @@ public Optional<CityResponse> tryCity(InetAddress ipAddress) throws IOException,
365359
@Override
366360
public AnonymousIpResponse anonymousIp(InetAddress ipAddress) throws IOException,
367361
GeoIp2Exception {
368-
var r = tryAnonymousIp(ipAddress);
369-
if (r.isEmpty()) {
370-
throw new AddressNotFoundException("The address "
371-
+ ipAddress.getHostAddress() + " is not in the database.");
372-
}
373-
return r.get();
362+
return tryAnonymousIp(ipAddress).orElseThrow(() ->
363+
new AddressNotFoundException("The address "
364+
+ ipAddress.getHostAddress() + " is not in the database."));
374365
}
375366

376367
@Override
@@ -395,12 +386,9 @@ public Optional<AnonymousIpResponse> tryAnonymousIp(InetAddress ipAddress) throw
395386
@Override
396387
public AnonymousPlusResponse anonymousPlus(InetAddress ipAddress) throws IOException,
397388
GeoIp2Exception {
398-
var r = tryAnonymousPlus(ipAddress);
399-
if (r.isEmpty()) {
400-
throw new AddressNotFoundException("The address "
401-
+ ipAddress.getHostAddress() + " is not in the database.");
402-
}
403-
return r.get();
389+
return tryAnonymousPlus(ipAddress).orElseThrow(() ->
390+
new AddressNotFoundException("The address "
391+
+ ipAddress.getHostAddress() + " is not in the database."));
404392
}
405393

406394
@Override
@@ -427,12 +415,9 @@ public Optional<AnonymousPlusResponse> tryAnonymousPlus(InetAddress ipAddress)
427415
@Override
428416
public IpRiskResponse ipRisk(InetAddress ipAddress) throws IOException,
429417
GeoIp2Exception {
430-
var r = tryIpRisk(ipAddress);
431-
if (r.isEmpty()) {
432-
throw new AddressNotFoundException("The address "
433-
+ ipAddress.getHostAddress() + " is not in the database.");
434-
}
435-
return r.get();
418+
return tryIpRisk(ipAddress).orElseThrow(() ->
419+
new AddressNotFoundException("The address "
420+
+ ipAddress.getHostAddress() + " is not in the database."));
436421
}
437422

438423
@Override
@@ -452,12 +437,9 @@ public Optional<IpRiskResponse> tryIpRisk(InetAddress ipAddress) throws IOExcept
452437
@Override
453438
public AsnResponse asn(InetAddress ipAddress) throws IOException,
454439
GeoIp2Exception {
455-
var r = tryAsn(ipAddress);
456-
if (r.isEmpty()) {
457-
throw new AddressNotFoundException("The address "
458-
+ ipAddress.getHostAddress() + " is not in the database.");
459-
}
460-
return r.get();
440+
return tryAsn(ipAddress).orElseThrow(() ->
441+
new AddressNotFoundException("The address "
442+
+ ipAddress.getHostAddress() + " is not in the database."));
461443
}
462444

463445
@Override
@@ -477,12 +459,9 @@ public Optional<AsnResponse> tryAsn(InetAddress ipAddress) throws IOException,
477459
@Override
478460
public ConnectionTypeResponse connectionType(InetAddress ipAddress)
479461
throws IOException, GeoIp2Exception {
480-
var r = tryConnectionType(ipAddress);
481-
if (r.isEmpty()) {
482-
throw new AddressNotFoundException("The address "
483-
+ ipAddress.getHostAddress() + " is not in the database.");
484-
}
485-
return r.get();
462+
return tryConnectionType(ipAddress).orElseThrow(() ->
463+
new AddressNotFoundException("The address "
464+
+ ipAddress.getHostAddress() + " is not in the database."));
486465
}
487466

488467
@Override
@@ -507,12 +486,9 @@ public Optional<ConnectionTypeResponse> tryConnectionType(InetAddress ipAddress)
507486
@Override
508487
public DomainResponse domain(InetAddress ipAddress) throws IOException,
509488
GeoIp2Exception {
510-
var r = tryDomain(ipAddress);
511-
if (r.isEmpty()) {
512-
throw new AddressNotFoundException("The address "
513-
+ ipAddress.getHostAddress() + " is not in the database.");
514-
}
515-
return r.get();
489+
return tryDomain(ipAddress).orElseThrow(() ->
490+
new AddressNotFoundException("The address "
491+
+ ipAddress.getHostAddress() + " is not in the database."));
516492
}
517493

518494
@Override
@@ -532,12 +508,9 @@ public Optional<DomainResponse> tryDomain(InetAddress ipAddress) throws IOExcept
532508
@Override
533509
public EnterpriseResponse enterprise(InetAddress ipAddress) throws IOException,
534510
GeoIp2Exception {
535-
var r = tryEnterprise(ipAddress);
536-
if (r.isEmpty()) {
537-
throw new AddressNotFoundException("The address "
538-
+ ipAddress.getHostAddress() + " is not in the database.");
539-
}
540-
return r.get();
511+
return tryEnterprise(ipAddress).orElseThrow(() ->
512+
new AddressNotFoundException("The address "
513+
+ ipAddress.getHostAddress() + " is not in the database."));
541514
}
542515

543516
@Override
@@ -563,12 +536,9 @@ public Optional<EnterpriseResponse> tryEnterprise(InetAddress ipAddress) throws
563536
@Override
564537
public IspResponse isp(InetAddress ipAddress) throws IOException,
565538
GeoIp2Exception {
566-
var r = tryIsp(ipAddress);
567-
if (r.isEmpty()) {
568-
throw new AddressNotFoundException("The address "
569-
+ ipAddress.getHostAddress() + " is not in the database.");
570-
}
571-
return r.get();
539+
return tryIsp(ipAddress).orElseThrow(() ->
540+
new AddressNotFoundException("The address "
541+
+ ipAddress.getHostAddress() + " is not in the database."));
572542
}
573543

574544
@Override

0 commit comments

Comments
 (0)