Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private ResultSetHandler<ExternalMapping> emHandler(Optional<Provider> p0) {
- return rs -> ExternalMapping.builder()
- .id(rs.getLong("id"))
- .provider(p0.isPresent() ? // let's get rid of this conditional
- p0.get() :
- Provider.fromInt(rs.getInt("provider")))
- .isDeleted(rs.getBoolean("isDeleted"))
- .entityUuid(UUID.fromString(rs.getString("entityUuid")))
- .externalId(rs.getString("externalId"))
- .entityType(EntityType.fromInt(rs.getInt("entityType")))
- .createdAt(rs.getTimestamp("createdAt").toInstant())
- .updatedAt(rs.getTimestamp("updatedAt").toInstant())
- .build()
- }
- // aaand...
- private ResultSetHandler<ExternalMapping> emHandler(Optional<Provider> p0) {
- return rs -> ExternalMapping.builder()
- .id(rs.getLong("id"))
- // OMG, sooooooooooooooo ugly
- .provider(p0.orElseGet(() -> {
- try {
- return Provider.fromInt(rs.getInt("provider"));
- } catch (SQLException e) {
- throw new RuntimeException(e);
- }
- }))
- .isDeleted(rs.getBoolean("isDeleted"))
- .entityUuid(UUID.fromString(rs.getString("entityUuid")))
- .externalId(rs.getString("externalId"))
- .entityType(EntityType.fromInt(rs.getInt("entityType")))
- .createdAt(rs.getTimestamp("createdAt").toInstant())
- .updatedAt(rs.getTimestamp("updatedAt").toInstant())
- .build()
- }
- // however Callable<T> helps us here:
- private static <T> Supplier<T> rethrow(Callable<T> callable) {
- return () -> {
- try {
- return callable.call();
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- };
- }
- // with this we can polish the handler:
- private ResultSetHandler<ExternalMapping> emHandler(Optional<Provider> p0) {
- return rs -> ExternalMapping.builder()
- .id(rs.getLong("id"))
- // looks better now
- .provider(p0.orElseGet(rethrow(() -> Provider.fromInt(rs.getInt("provider")))))
- .isDeleted(rs.getBoolean("isDeleted"))
- .entityUuid(UUID.fromString(rs.getString("entityUuid")))
- .externalId(rs.getString("externalId"))
- .entityType(EntityType.fromInt(rs.getInt("entityType")))
- .createdAt(rs.getTimestamp("createdAt").toInstant())
- .updatedAt(rs.getTimestamp("updatedAt").toInstant())
- .build();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement