Repository navigation
Expand file tree
/
Copy pathCallback.java
More file actions
74 lines (60 loc) · 2.09 KB
/
Copy pathCallback.java
File metadata and controls
74 lines (60 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.umer.javafunctional.callback;
import java.util.function.Consumer;
public class Callback {
private static final String EMPTY_STRING = " ";
public static void main(String[] args) {
System.out.println(EMPTY_STRING);
System.out.println("------------------------------------------------------------");
System.out.println("Last name is provided and Callback is null.");
System.out.println("------------------------------------------------------------");
hello("John", "Montana", null);
System.out.println(EMPTY_STRING);
System.out.println(EMPTY_STRING);
System.out.println("------------------------------------------------------------");
System.out.println("Last name is null and Callback is processing this condition.");
System.out.println("------------------------------------------------------------");
hello("John",
null,
value ->{
System.out.println("No last name provided for "+value);
});
System.out.println(EMPTY_STRING);
System.out.println(EMPTY_STRING);
System.out.println("------------------------------------------------------------");
System.out.println("Last name is null and Callback With Runnable is processing this condition.");
System.out.println("------------------------------------------------------------");
helloWithRunnable("John",
null,
()->{
System.out.println("No last name provided");
});
System.out.println(EMPTY_STRING);
}
static void hello(String firstName, String lastName, Consumer<String> callback) {
System.out.println(firstName);
if(lastName!=null) {
System.out.println(lastName);
}else {
callback.accept(firstName);
}
}
static void helloWithRunnable(String firstName, String lastName, Runnable callback) {
System.out.println(firstName);
if(lastName!=null) {
System.out.println(lastName);
}else {
callback.run();
}
}
/**
* Sample java script code which does callback()
*/
// function hello(firstName, lastName, callback) {
// console.log(firstName);
// if(lastName) {
// console.log(lastName);
// }else {
// callback();
// }
// }
}