This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try { | |
database.start(); | |
fetchMeasurements(geoCoordinatesReader, airlyService, measurementsRepository); | |
} catch (SQLException exc) { | |
log.error("Cannot start the database", exc); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Try | |
.run(database::start) | |
.onSuccess(ignore -> log.info("Database started successfully")) | |
.onFailure(exc -> log.error("Cannot start the database", exc)) | |
.andThen( | |
() -> fetchMeasurements(geoCoordinatesReader, airlyService, measurementsRepository) | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Try.of( | |
() -> geoCoordinatesReader.fromCsvFile("./src/main/resources/cities.csv") | |
) | |
.onSuccess(coords -> log.info("Coordinates read: {}", coords)) | |
.onFailure(exc -> log.error("Cannot read coordinates from a file", exc)) | |
.recover(FileNotFoundException.class, (exc) -> provideBackupCoordinates()) | |
.getOrElse(List.empty()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static List<GeoCoordinates> provideBackupCoordinates() { | |
log.warn("Calling for backup data"); | |
return List.of( | |
GeoCoordinates.builder() | |
.city("Warszawa") | |
.latitude("52.25") | |
.longitude("21") | |
.build() | |
); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Try.of(() -> Uri.create("http://airapi.airly.eu/v2/measurements/nearest/...")) | |
.flatMap(this::callAirly) | |
.map(responseBody -> new RawMeasurements(coordinates.city(), responseBody)) | |
.mapTry(this::parse); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private Try<String> callAirly(URI airlyUri) { | |
HttpRequest req = requestBuilder.uri(airlyUri).build(); | |
return Try | |
.of(() -> httpClient.send(req, bodyHandler)) | |
.map(HttpResponse::body); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Try.of(this::computation) | |
.onFailure(exc -> log.error("Computation failed!", exc)) | |
.andThen(this::storeInDb) | |
.onFailure(exc -> log.error("Storing in database failed!", exc)) | |
.andThen(this::sendEmail) | |
.onFailure(exc -> log.error("Email sending failed!", exc)) | |
.onSuccess(value -> log.info("Value computed, stored in db and sent in email")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Target({ ElementType.METHOD, ElementType.FIELD }) | |
@Retention(RetentionPolicy.RUNTIME) | |
@Constraint(validatedBy = IbanValidator.class) | |
public @interface Iban { | |
String message() default "{iban.constraint}"; | |
Class<?>[] groups() default {}; | |
Class<? extends Payload>[] payload() default {}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Slf4j | |
public class IbanValidator implements ConstraintValidator<Iban, String> { | |
@Override | |
public boolean isValid(String value, ConstraintValidatorContext context) { | |
if (value == null) { | |
return false; | |
} | |
return Try.of(() -> { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
BankAccount bankAccount = newBankAccount("invalid_iban", "invalid_bic", null); | |
final Set<ConstraintViolation<BankAccount>> constraints = | |
Validation.buildDefaultValidatorFactory().getValidator().validate(bankAccount); |
OlderNewer