-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathMutableTest.java
57 lines (55 loc) · 1.24 KB
/
MutableTest.java
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
package org.lambda.utils;
import org.approvaltests.Approvals;
import org.junit.jupiter.api.Test;
import org.lambda.functions.Function0;
public class MutableTest
{
@Test
public void exampleOfSingleElementArray()
{
// begin-snippet: single_element_array
final int[] i = {1};
Function0<Integer> counter = () -> i[0]++;
// end-snippet
}
@Test
public void exampleOfMutable()
{
// begin-snippet: mutable_example
Mutable<String> i = new Mutable<>("Brian");
Scheduler scheduler = new Scheduler(() -> i.get());
scheduler.addEvent();
i.update(n -> "Mr. " + n);
scheduler.rsvp();
i.set("Steve");
scheduler.bookHotel();
// end-snippet
Approvals.verify(scheduler);
}
private class Scheduler
{
private Function0<String> namer;
private String log = "";
public Scheduler(Function0<String> namer)
{
this.namer = namer;
}
public void rsvp()
{
log += "rsvping as " + namer.call() + "\n";
}
public void addEvent()
{
log += "adding event as " + namer.call() + "\n";
}
public void bookHotel()
{
log += "booking hotel as " + namer.call() + "\n";
}
@Override
public String toString()
{
return log;
}
}
}