BAEL-5844: Convert Anonymous Class into Lambda in Java (#13088)
This commit is contained in:
parent
29d97308f6
commit
a288f8db7e
|
@ -0,0 +1,10 @@
|
|||
package com.baeldung.anonymousclass;
|
||||
|
||||
public class EmailSenderService implements SenderService {
|
||||
|
||||
@Override
|
||||
public String callSender(Sender sender) {
|
||||
return sender.send("Email Notification");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package com.baeldung.anonymousclass;
|
||||
|
||||
public interface Sender {
|
||||
|
||||
String send(final String message);
|
||||
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package com.baeldung.anonymousclass;
|
||||
|
||||
public interface SenderService {
|
||||
|
||||
String callSender(Sender sender);
|
||||
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package com.baeldung.anonymousclass;
|
||||
|
||||
public class SmsSenderService implements SenderService {
|
||||
|
||||
@Override
|
||||
public String callSender(Sender sender) {
|
||||
return sender.send("SMS Notification");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package com.baeldung.anonymousclass;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class AnonymousClassToLambdaIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void whenPassingAnonymousClass_thenSuccess() {
|
||||
final SenderService emailSenderService = new EmailSenderService();
|
||||
|
||||
final String emailNotif = emailSenderService.callSender(new Sender() {
|
||||
@Override
|
||||
public String send(String message) {
|
||||
return message;
|
||||
}
|
||||
});
|
||||
|
||||
assertEquals(emailNotif, "Email Notification");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPassingLambdaExpression_thenSuccess() {
|
||||
final SenderService smsSenderService = new SmsSenderService();
|
||||
|
||||
final String smsNotif = smsSenderService.callSender((String message) -> {
|
||||
return message;
|
||||
});
|
||||
|
||||
assertEquals(smsNotif, "SMS Notification");
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue