Advertisement
NLinker

Checked exceptions sucks

Dec 16th, 2016
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.63 KB | None | 0 0
  1.     private ResultSetHandler<ExternalMapping> emHandler(Optional<Provider> p0) {
  2.         return rs -> ExternalMapping.builder()
  3.             .id(rs.getLong("id"))
  4.             .provider(p0.isPresent() ? // let's get rid of this conditional
  5.                 p0.get() :
  6.                 Provider.fromInt(rs.getInt("provider")))
  7.             .isDeleted(rs.getBoolean("isDeleted"))
  8.             .entityUuid(UUID.fromString(rs.getString("entityUuid")))
  9.             .externalId(rs.getString("externalId"))
  10.             .entityType(EntityType.fromInt(rs.getInt("entityType")))
  11.             .createdAt(rs.getTimestamp("createdAt").toInstant())
  12.             .updatedAt(rs.getTimestamp("updatedAt").toInstant())
  13.             .build()
  14.     }
  15. // aaand...
  16.     private ResultSetHandler<ExternalMapping> emHandler(Optional<Provider> p0) {
  17.         return rs -> ExternalMapping.builder()
  18.             .id(rs.getLong("id"))
  19.             // OMG, sooooooooooooooo ugly
  20.             .provider(p0.orElseGet(() -> {
  21.                 try {
  22.                     return Provider.fromInt(rs.getInt("provider"));
  23.                 } catch (SQLException e) {
  24.                     throw new RuntimeException(e);
  25.                 }
  26.             }))
  27.             .isDeleted(rs.getBoolean("isDeleted"))
  28.             .entityUuid(UUID.fromString(rs.getString("entityUuid")))
  29.             .externalId(rs.getString("externalId"))
  30.             .entityType(EntityType.fromInt(rs.getInt("entityType")))
  31.             .createdAt(rs.getTimestamp("createdAt").toInstant())
  32.             .updatedAt(rs.getTimestamp("updatedAt").toInstant())
  33.             .build()
  34.     }
  35.  
  36. // however Callable<T> helps us here:
  37.     private static <T> Supplier<T> rethrow(Callable<T> callable) {
  38.         return () -> {
  39.             try {
  40.                 return callable.call();
  41.             } catch (Exception e) {
  42.                 throw new RuntimeException(e);
  43.             }
  44.         };
  45.     }
  46.    
  47.     // with this we can polish the handler:
  48.     private ResultSetHandler<ExternalMapping> emHandler(Optional<Provider> p0) {
  49.         return rs -> ExternalMapping.builder()
  50.             .id(rs.getLong("id"))
  51.             // looks better now
  52.             .provider(p0.orElseGet(rethrow(() -> Provider.fromInt(rs.getInt("provider")))))
  53.             .isDeleted(rs.getBoolean("isDeleted"))
  54.             .entityUuid(UUID.fromString(rs.getString("entityUuid")))
  55.             .externalId(rs.getString("externalId"))
  56.             .entityType(EntityType.fromInt(rs.getInt("entityType")))
  57.             .createdAt(rs.getTimestamp("createdAt").toInstant())
  58.             .updatedAt(rs.getTimestamp("updatedAt").toInstant())
  59.             .build();
  60.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement