完成所有代码的更新和提交
This commit is contained in:
+10
@@ -0,0 +1,10 @@
|
||||
package com.ossez.anonymousclass;
|
||||
|
||||
public class EmailSenderService implements SenderService {
|
||||
|
||||
@Override
|
||||
public String callSender(Sender sender) {
|
||||
return sender.send("Email Notification");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.ossez.anonymousclass;
|
||||
|
||||
public interface Sender {
|
||||
|
||||
String send(final String message);
|
||||
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package com.ossez.anonymousclass;
|
||||
|
||||
public interface SenderService {
|
||||
|
||||
String callSender(Sender sender);
|
||||
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package com.ossez.anonymousclass;
|
||||
|
||||
public class SmsSenderService implements SenderService {
|
||||
|
||||
@Override
|
||||
public String callSender(Sender sender) {
|
||||
return sender.send("SMS Notification");
|
||||
}
|
||||
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package com.ossez.java8.lambda.serialization;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class SerializableLambdaExpression {
|
||||
public static Object getLambdaExpressionObject() {
|
||||
Runnable r = (Runnable & Serializable) () -> System.out.println("please serialize this message");
|
||||
return r;
|
||||
}
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
package com.ossez.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;
|
||||
}
|
||||
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package com.ossez.suppliercallable.service;
|
||||
|
||||
import com.ossez.suppliercallable.data.User;
|
||||
|
||||
public interface Service {
|
||||
|
||||
User execute(User user);
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.ossez.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;
|
||||
}
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package com.ossez.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.ossez.suppliercallable.data.User;
|
||||
import com.ossez.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;
|
||||
}
|
||||
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.ossez.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;
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package com.ossez.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.ossez.suppliercallable.data.User;
|
||||
import com.ossez.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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user