forked from Apress/functional-interfaces-in-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSect7_Ex5.java
More file actions
27 lines (25 loc) · 738 Bytes
/
Sect7_Ex5.java
File metadata and controls
27 lines (25 loc) · 738 Bytes
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
package chapter11;
import java.util.Optional;
class Resource
{
static int count = 3;
public Resource()
{
count--;
System.out.println("Resource consumed, " + count
+ " remaining.");
}
}
public class Sect7_Ex5
{
public static void main(String[] args)
{
Optional.of(new Resource()) // Resource.count = 2
.filter(x -> x.count > 0)
.map(x -> new Resource()) // Resource.count = 1
.filter(x -> x.count > 0)
.map(x -> new Resource()) // Resource.count = 0
.filter(x -> x.count > 0) // Optional(null)
.map(x -> new Resource()); // doesn't execute
}
}