/
Iteratee.java
150 lines (112 loc) · 3.55 KB
/
Iteratee.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package com.technozor.ouertanitee;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
/**
* @author slim ouertani
* @param <E>
* @param <A>
*/
public interface Iteratee<E, A> {
<B> CompletableFuture<B> handle(Function<Iteratee<E, A>, CompletableFuture<B>> step);
default Function<Input<E>, Iteratee<E, A>> handler() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
enum StepState {
Done, CONT, ERROR
}
default StepState onState() {
return StepState.CONT;
}
static <E> Error<E> Error(String msg, Input<E> input) {
return new Error(msg, input);
}
static <E, A> Cont<E, A> Cont(Function<Input<E>, Iteratee<E, A>> k) {
return new Cont(k);
}
static <E, A> Done<E, A> Done(A a, Input<E> e) {
return new Done(a, e);
}
static <E, A> Done<E, A> Done(A a) {
return new Done(a);
}
class Done<E, A> implements Iteratee<E, A> {
private final A a;
private final Input<E> input;
@Override
public StepState onState() {
return StepState.Done;
}
public A getA() {
return a;
}
public Input<E> getInput() {
return input;
}
public Done(A a, Input<E> e) {
this.a = a;
this.input = e;
}
public Done(A a) {
this(a, Input.EMPTY);
}
@Override
public <B> CompletableFuture<B> handle(Function<Iteratee<E, A>, CompletableFuture<B>> folder) {
return folder.apply(Iteratee.Done(a, input));
}
@Override
public String toString() {
return "Done{" + "a=" + a + ", input=" + input + '}';
}
@Override
public Function<Input<E>, Iteratee<E, A>> handler() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
class Cont<E, A> implements Iteratee<E, A> {
private final Function<Input<E>, Iteratee<E, A>> k;
public Cont(Function<Input<E>, Iteratee<E, A>> k) {
this.k = k;
}
public Function<Input<E>, Iteratee<E, A>> getK() {
return k;
}
@Override
public StepState onState() {
return StepState.CONT;
}
@Override
public String toString() {
return "Cont{" + "k=" + k + '}';
}
@Override
public <B> CompletableFuture<B> handle(Function<Iteratee<E, A>, CompletableFuture<B>> folder) {
return folder.apply(Iteratee.Cont(k));
}
}
class Error<E> implements Iteratee<E, Object> {
private final String msg;
private final Input<E> input;
public Error(String msg, Input<E> input) {
this.msg = msg;
this.input = input;
}
@Override
public StepState onState() {
return StepState.ERROR;
}
public String getMsg() {
return msg;
}
public Input<E> getInput() {
return input;
}
@Override
public <B> CompletableFuture<B> handle(Function<Iteratee<E, Object>, CompletableFuture<B>> folder) {
return folder.apply(Iteratee.Error(msg, input));
}
@Override
public String toString() {
return "Error{" + "msg=" + msg + ", input=" + input + '}';
}
}
}