@@ -0,0 +1,38 @@
|
||||
package com.baeldung.vavr;
|
||||
|
||||
public class Person {
|
||||
private String name;
|
||||
private int age;
|
||||
|
||||
public Person(String name, int age) {
|
||||
super();
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public Person() {
|
||||
super();
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Person [name=" + name + ", age=" + age + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.vavr;
|
||||
|
||||
import io.vavr.collection.Seq;
|
||||
import io.vavr.control.Validation;
|
||||
|
||||
class PersonValidator {
|
||||
String NAME_ERR = "Invalid characters in name: ";
|
||||
String AGE_ERR = "Age must be at least 0";
|
||||
|
||||
public Validation<Seq<String>, Person> validatePerson(String name, int age) {
|
||||
return Validation.combine(validateName(name), validateAge(age)).ap(Person::new);
|
||||
}
|
||||
|
||||
private Validation<String, String> validateName(String name) {
|
||||
String invalidChars = name.replaceAll("[a-zA-Z ]", "");
|
||||
return invalidChars.isEmpty() ? Validation.valid(name) : Validation.invalid(NAME_ERR + invalidChars);
|
||||
}
|
||||
|
||||
private Validation<String, Integer> validateAge(int age) {
|
||||
return age < 0 ? Validation.invalid(AGE_ERR) : Validation.valid(age);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.vavr.exception.handling;
|
||||
|
||||
import com.baeldung.vavr.exception.handling.client.ClientException;
|
||||
import com.baeldung.vavr.exception.handling.client.HttpClient;
|
||||
import com.baeldung.vavr.exception.handling.client.Response;
|
||||
|
||||
public class JavaTryCatch {
|
||||
private HttpClient httpClient;
|
||||
|
||||
public JavaTryCatch(HttpClient httpClient) {
|
||||
this.httpClient = httpClient;
|
||||
}
|
||||
|
||||
public Response getResponse() {
|
||||
try {
|
||||
return httpClient.call();
|
||||
} catch (ClientException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.vavr.exception.handling;
|
||||
|
||||
|
||||
import com.baeldung.vavr.exception.handling.client.HttpClient;
|
||||
import com.baeldung.vavr.exception.handling.client.Response;
|
||||
import io.vavr.control.Try;
|
||||
|
||||
public class VavrTry {
|
||||
private final HttpClient httpClient;
|
||||
|
||||
public VavrTry(HttpClient httpClient) {
|
||||
this.httpClient = httpClient;
|
||||
}
|
||||
|
||||
public Try<Response> getResponse() {
|
||||
return Try.of(httpClient::call);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.vavr.exception.handling.client;
|
||||
|
||||
|
||||
public class ClientException extends Exception {
|
||||
public ClientException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.baeldung.vavr.exception.handling.client;
|
||||
|
||||
|
||||
public interface HttpClient {
|
||||
Response call() throws ClientException;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.vavr.exception.handling.client;
|
||||
|
||||
public class Response {
|
||||
public final String id;
|
||||
|
||||
public Response(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user