[BAEL 5909]-Callable vs Supplier and When to Use Them in Java (#13041)

* Added test class for a simple shallow copy and deep copy

* Added test class for a simple shallow copy and deep copy

* refactor naming of test method

* formatted

* refactor test whenIsAShallowCopyDoneByCopyConstructor_thenImmutableObjectWillNotChange

* Renamed package and added md file

* refactor README.md

* first push

* refactor

* Revert "refactor README.md"

This reverts commit eae77c453ba0bf2af62bad52dc1ed61d07931e34.

* Revert "Renamed package and added md file"

This reverts commit 42c6f97cbde39cc0a5e0bacf34f86a32ded4f4aa.

* Revert "refactor test whenIsAShallowCopyDoneByCopyConstructor_thenImmutableObjectWillNotChange"

This reverts commit 44fb57fe2b51857f960dc216d33508e718e5414f.

* Revert "formatted"

This reverts commit 44be87ef25e566b8e9175cb0fdeed7f0ef485dd3.

* Revert "refactor naming of test method"

This reverts commit 6133c31057e39b19c4978f960cda1c0ba5559aae.

* Revert "Added test class for a simple shallow copy and deep copy"

This reverts commit 2cae083578883ae693d1c5e76fd4948e213e9ea0.

* Revert "Added test class for a simple shallow copy and deep copy"

This reverts commit f43312e2c1979410409f46020a3f7d555e11e966.

* Merge prohect java-supplier-callable to project core-java-lambdas

* adjusted package name

* removed AbstractAgeCalculator.java

* added test for supplier-callable

Co-authored-by: Cesare <cesare.valenti@hotmail.com>
This commit is contained in:
cesarevalenti90 2022-12-19 14:53:17 +01:00 committed by GitHub
parent 39f29ec690
commit 0fd77aa664
7 changed files with 225 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -0,0 +1,8 @@
package com.baeldung.suppliercallable.service;
import com.baeldung.suppliercallable.data.User;
public interface Service {
User execute(User user);
}

View File

@ -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<Integer> {
private final LocalDate birthDate;
@Override
public Integer call() throws Exception {
return Period.between(birthDate, LocalDate.now())
.getYears();
}
public AgeCalculatorCallable(LocalDate birthDate) {
this.birthDate = birthDate;
}
}

View File

@ -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<Integer> ageFuture = executorService.submit(new AgeCalculatorCallable(user.getBirthDate()));
Integer age = ageFuture.get();
Future<Boolean> 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;
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.suppliercallable.service.callable;
import java.util.concurrent.Callable;
public class CarDriverValidatorCallable implements Callable<Boolean> {
private final Integer age;
@Override
public Boolean call() throws Exception {
return age > 18;
}
public CarDriverValidatorCallable(Integer age) {
this.age = age;
}
}

View File

@ -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<Integer> ageFut = CompletableFuture.supplyAsync(() -> Period.between(user.getBirthDate(), LocalDate.now())
.getYears(), executorService)
.exceptionally((throwable -> null));
CompletableFuture<Boolean> canDriveACarFut = ageFut.thenComposeAsync(age -> CompletableFuture.supplyAsync(() -> age > 18, executorService))
.exceptionally((ex) -> false);
user.setAge(ageFut.join());
user.setCanDriveACar(canDriveACarFut.join());
return user;
}
}

View File

@ -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());
}
}