diff --git a/core-java-modules/core-java-lambdas/src/main/java/com/baeldung/suppliercallable/data/User.java b/core-java-modules/core-java-lambdas/src/main/java/com/baeldung/suppliercallable/data/User.java new file mode 100644 index 0000000000..488f12a099 --- /dev/null +++ b/core-java-modules/core-java-lambdas/src/main/java/com/baeldung/suppliercallable/data/User.java @@ -0,0 +1,59 @@ +package com.baeldung.suppliercallable.data; + +import java.time.LocalDate; + +public class User { + + private String name; + private String surname; + private LocalDate birthDate; + private Integer age; + private Boolean canDriveACar = false; + + public User(String name, String surname, LocalDate birthDate) { + this.name = name; + this.surname = surname; + this.birthDate = birthDate; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getSurname() { + return surname; + } + + public void setSurname(String surname) { + this.surname = surname; + } + + public LocalDate getBirthDate() { + return birthDate; + } + + public void setBirthDate(LocalDate birthDate) { + this.birthDate = birthDate; + } + + public void setAge(Integer age) { + this.age = age; + } + + public Integer getAge() { + return age; + } + + public Boolean getCanDriveACar() { + return canDriveACar; + } + + public void setCanDriveACar(Boolean canDriveACar) { + this.canDriveACar = canDriveACar; + } + +} diff --git a/core-java-modules/core-java-lambdas/src/main/java/com/baeldung/suppliercallable/service/Service.java b/core-java-modules/core-java-lambdas/src/main/java/com/baeldung/suppliercallable/service/Service.java new file mode 100644 index 0000000000..cdae828d8a --- /dev/null +++ b/core-java-modules/core-java-lambdas/src/main/java/com/baeldung/suppliercallable/service/Service.java @@ -0,0 +1,8 @@ +package com.baeldung.suppliercallable.service; + +import com.baeldung.suppliercallable.data.User; + +public interface Service { + + User execute(User user); +} diff --git a/core-java-modules/core-java-lambdas/src/main/java/com/baeldung/suppliercallable/service/callable/AgeCalculatorCallable.java b/core-java-modules/core-java-lambdas/src/main/java/com/baeldung/suppliercallable/service/callable/AgeCalculatorCallable.java new file mode 100644 index 0000000000..f42e4922b6 --- /dev/null +++ b/core-java-modules/core-java-lambdas/src/main/java/com/baeldung/suppliercallable/service/callable/AgeCalculatorCallable.java @@ -0,0 +1,20 @@ +package com.baeldung.suppliercallable.service.callable; + +import java.time.LocalDate; +import java.time.Period; +import java.util.concurrent.Callable; + +public class AgeCalculatorCallable implements Callable { + + private final LocalDate birthDate; + + @Override + public Integer call() throws Exception { + return Period.between(birthDate, LocalDate.now()) + .getYears(); + } + + public AgeCalculatorCallable(LocalDate birthDate) { + this.birthDate = birthDate; + } +} diff --git a/core-java-modules/core-java-lambdas/src/main/java/com/baeldung/suppliercallable/service/callable/CallableServiceImpl.java b/core-java-modules/core-java-lambdas/src/main/java/com/baeldung/suppliercallable/service/callable/CallableServiceImpl.java new file mode 100644 index 0000000000..baf6b5bbd9 --- /dev/null +++ b/core-java-modules/core-java-lambdas/src/main/java/com/baeldung/suppliercallable/service/callable/CallableServiceImpl.java @@ -0,0 +1,31 @@ +package com.baeldung.suppliercallable.service.callable; + +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +import com.baeldung.suppliercallable.data.User; +import com.baeldung.suppliercallable.service.Service; + +public class CallableServiceImpl implements Service { + + @Override + public User execute(User user) { + ExecutorService executorService = Executors.newCachedThreadPool(); + + try { + Future ageFuture = executorService.submit(new AgeCalculatorCallable(user.getBirthDate())); + Integer age = ageFuture.get(); + Future canDriveACarFuture = executorService.submit(new CarDriverValidatorCallable(age)); + Boolean canDriveACar = canDriveACarFuture.get(); + user.setAge(age); + user.setCanDriveACar(canDriveACar); + } catch (ExecutionException | InterruptedException e) { + throw new RuntimeException(e.getCause()); + } + + return user; + } + +} diff --git a/core-java-modules/core-java-lambdas/src/main/java/com/baeldung/suppliercallable/service/callable/CarDriverValidatorCallable.java b/core-java-modules/core-java-lambdas/src/main/java/com/baeldung/suppliercallable/service/callable/CarDriverValidatorCallable.java new file mode 100644 index 0000000000..e282cdd626 --- /dev/null +++ b/core-java-modules/core-java-lambdas/src/main/java/com/baeldung/suppliercallable/service/callable/CarDriverValidatorCallable.java @@ -0,0 +1,17 @@ +package com.baeldung.suppliercallable.service.callable; + +import java.util.concurrent.Callable; + +public class CarDriverValidatorCallable implements Callable { + + private final Integer age; + + @Override + public Boolean call() throws Exception { + return age > 18; + } + + public CarDriverValidatorCallable(Integer age) { + this.age = age; + } +} diff --git a/core-java-modules/core-java-lambdas/src/main/java/com/baeldung/suppliercallable/service/supplier/SupplierServiceImpl.java b/core-java-modules/core-java-lambdas/src/main/java/com/baeldung/suppliercallable/service/supplier/SupplierServiceImpl.java new file mode 100644 index 0000000000..9de0b430eb --- /dev/null +++ b/core-java-modules/core-java-lambdas/src/main/java/com/baeldung/suppliercallable/service/supplier/SupplierServiceImpl.java @@ -0,0 +1,26 @@ +package com.baeldung.suppliercallable.service.supplier; + +import java.time.LocalDate; +import java.time.Period; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +import com.baeldung.suppliercallable.data.User; +import com.baeldung.suppliercallable.service.Service; + +public class SupplierServiceImpl implements Service { + @Override + public User execute(User user) { + ExecutorService executorService = Executors.newCachedThreadPool(); + CompletableFuture ageFut = CompletableFuture.supplyAsync(() -> Period.between(user.getBirthDate(), LocalDate.now()) + .getYears(), executorService) + .exceptionally((throwable -> null)); + CompletableFuture canDriveACarFut = ageFut.thenComposeAsync(age -> CompletableFuture.supplyAsync(() -> age > 18, executorService)) + .exceptionally((ex) -> false); + user.setAge(ageFut.join()); + user.setCanDriveACar(canDriveACarFut.join()); + return user; + } + +} diff --git a/core-java-modules/core-java-lambdas/src/test/java/com/baeldung/suppliercallable/CallableSupplierUnitTest.java b/core-java-modules/core-java-lambdas/src/test/java/com/baeldung/suppliercallable/CallableSupplierUnitTest.java new file mode 100644 index 0000000000..c9be58c675 --- /dev/null +++ b/core-java-modules/core-java-lambdas/src/test/java/com/baeldung/suppliercallable/CallableSupplierUnitTest.java @@ -0,0 +1,64 @@ +package com.baeldung.suppliercallable; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import java.time.LocalDate; +import java.time.Month; +import org.junit.jupiter.api.Test; +import com.baeldung.suppliercallable.data.User; +import com.baeldung.suppliercallable.service.Service; +import com.baeldung.suppliercallable.service.callable.CallableServiceImpl; +import com.baeldung.suppliercallable.service.supplier.SupplierServiceImpl; + +public class CallableSupplierUnitTest { + + @Test + void givenCallableService_whenUserIsAnAdult_thenCanDriveACar() { + User user = new User("Test", "Test", LocalDate.of(2000, Month.JANUARY, 19)); + Service service = new CallableServiceImpl(); + service.execute(user); + assertEquals(true, user.getCanDriveACar()); + } + + @Test + void givenCallableService_whenUserIsNotAnAdult_thenCannotDriveACar() { + User user = new User("Test", "Test", LocalDate.of(2010, Month.JANUARY, 19)); + Service service = new CallableServiceImpl(); + service.execute(user); + assertEquals(false, user.getCanDriveACar()); + } + + @Test + void givenCallableService_whenBirthDateIsNull_thenShouldThrowAnException() { + User user = new User("Test", "Test", null); + Service service = new CallableServiceImpl(); + assertThrows(RuntimeException.class, () -> service.execute(user)); + } + + @Test + void givenSupplierService_whenUserIsAnAdult_thenCanDriveACar() { + User user = new User("Test", "Test", LocalDate.of(2000, Month.JANUARY, 19)); + Service service = new SupplierServiceImpl(); + service.execute(user); + assertEquals(true, user.getCanDriveACar()); + } + + @Test + void givenSupplierService_whenUserIsNotAnAdult_thenCannotDriveACar() { + User user = new User("Test", "Test", LocalDate.of(2010, Month.JANUARY, 19)); + Service service = new SupplierServiceImpl(); + service.execute(user); + assertEquals(false, user.getCanDriveACar()); + } + + @Test + void givenSupplierService_whenBirthDateIsNull_thenCannotDriveACarAndAgeIsNull() { + User user = new User("Test", "Test", null); + Service service = new SupplierServiceImpl(); + service.execute(user); + assertEquals(false, user.getCanDriveACar()); + assertNull(user.getBirthDate()); + } + +}