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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
| // Maybe Monad (Optional의 확장)
public abstract class Maybe<T> {
public abstract boolean isPresent();
public abstract T get();
public static <T> Maybe<T> of(T value) {
return value != null ? new Some<>(value) : new None<>();
}
public static <T> Maybe<T> empty() {
return new None<>();
}
// Functor 구현
public abstract <U> Maybe<U> map(Function<T, U> mapper);
// Monad 구현
public abstract <U> Maybe<U> flatMap(Function<T, Maybe<U>> mapper);
// Applicative 구현
public static <T, U, R> Maybe<R> lift2(
Function<T, Function<U, R>> f,
Maybe<T> maybeT,
Maybe<U> maybeU) {
return maybeT.flatMap(t -> maybeU.map(u -> f.apply(t).apply(u)));
}
// 유틸리티 메서드들
public abstract Maybe<T> filter(Predicate<T> predicate);
public abstract T orElse(T defaultValue);
public abstract T orElseGet(Supplier<T> supplier);
public abstract Maybe<T> or(Supplier<Maybe<T>> alternative);
// Some 구현
private static class Some<T> extends Maybe<T> {
private final T value;
Some(T value) {
this.value = value;
}
@Override
public boolean isPresent() {
return true;
}
@Override
public T get() {
return value;
}
@Override
public <U> Maybe<U> map(Function<T, U> mapper) {
try {
return Maybe.of(mapper.apply(value));
} catch (Exception e) {
return Maybe.empty();
}
}
@Override
public <U> Maybe<U> flatMap(Function<T, Maybe<U>> mapper) {
try {
return mapper.apply(value);
} catch (Exception e) {
return Maybe.empty();
}
}
@Override
public Maybe<T> filter(Predicate<T> predicate) {
return predicate.test(value) ? this : Maybe.empty();
}
@Override
public T orElse(T defaultValue) {
return value;
}
@Override
public T orElseGet(Supplier<T> supplier) {
return value;
}
@Override
public Maybe<T> or(Supplier<Maybe<T>> alternative) {
return this;
}
}
// None 구현
private static class None<T> extends Maybe<T> {
@Override
public boolean isPresent() {
return false;
}
@Override
public T get() {
throw new NoSuchElementException("No value present");
}
@Override
public <U> Maybe<U> map(Function<T, U> mapper) {
return Maybe.empty();
}
@Override
public <U> Maybe<U> flatMap(Function<T, Maybe<U>> mapper) {
return Maybe.empty();
}
@Override
public Maybe<T> filter(Predicate<T> predicate) {
return this;
}
@Override
public T orElse(T defaultValue) {
return defaultValue;
}
@Override
public T orElseGet(Supplier<T> supplier) {
return supplier.get();
}
@Override
public Maybe<T> or(Supplier<Maybe<T>> alternative) {
return alternative.get();
}
}
}
// Either Monad (성공/실패 처리)
public abstract class Either<L, R> {
public abstract boolean isLeft();
public abstract boolean isRight();
public abstract L getLeft();
public abstract R getRight();
public static <L, R> Either<L, R> left(L value) {
return new Left<>(value);
}
public static <L, R> Either<L, R> right(R value) {
return new Right<>(value);
}
// Functor
public abstract <T> Either<L, T> map(Function<R, T> mapper);
// Monad
public abstract <T> Either<L, T> flatMap(Function<R, Either<L, T>> mapper);
// Error handling
public abstract Either<L, R> mapLeft(Function<L, L> mapper);
public abstract <T> Either<T, R> mapError(Function<L, T> mapper);
// Left 구현
private static class Left<L, R> extends Either<L, R> {
private final L value;
Left(L value) {
this.value = value;
}
@Override
public boolean isLeft() { return true; }
@Override
public boolean isRight() { return false; }
@Override
public L getLeft() { return value; }
@Override
public R getRight() {
throw new NoSuchElementException("Right value on Left");
}
@Override
public <T> Either<L, T> map(Function<R, T> mapper) {
return Either.left(value);
}
@Override
public <T> Either<L, T> flatMap(Function<R, Either<L, T>> mapper) {
return Either.left(value);
}
@Override
public Either<L, R> mapLeft(Function<L, L> mapper) {
return Either.left(mapper.apply(value));
}
@Override
public <T> Either<T, R> mapError(Function<L, T> mapper) {
return Either.left(mapper.apply(value));
}
}
// Right 구현
private static class Right<L, R> extends Either<L, R> {
private final R value;
Right(R value) {
this.value = value;
}
@Override
public boolean isLeft() { return false; }
@Override
public boolean isRight() { return true; }
@Override
public L getLeft() {
throw new NoSuchElementException("Left value on Right");
}
@Override
public R getRight() { return value; }
@Override
public <T> Either<L, T> map(Function<R, T> mapper) {
return Either.right(mapper.apply(value));
}
@Override
public <T> Either<L, T> flatMap(Function<R, Either<L, T>> mapper) {
return mapper.apply(value);
}
@Override
public Either<L, R> mapLeft(Function<L, L> mapper) {
return this;
}
@Override
public <T> Either<T, R> mapError(Function<L, T> mapper) {
return Either.right(value);
}
}
}
// 함수형 서비스 구현 예시
public class FunctionalUserService {
public Either<String, User> createUser(UserRequest request) {
return validateRequest(request)
.flatMap(this::checkUserNotExists)
.flatMap(this::createUserEntity)
.flatMap(this::saveUser)
.flatMap(this::sendWelcomeEmail);
}
private Either<String, UserRequest> validateRequest(UserRequest request) {
if (request.getName() == null || request.getName().trim().isEmpty()) {
return Either.left("Name is required");
}
if (request.getEmail() == null || !request.getEmail().contains("@")) {
return Either.left("Valid email is required");
}
return Either.right(request);
}
private Either<String, UserRequest> checkUserNotExists(UserRequest request) {
// 실제로는 데이터베이스 체크
if (userExists(request.getEmail())) {
return Either.left("User already exists: " + request.getEmail());
}
return Either.right(request);
}
private Either<String, User> createUserEntity(UserRequest request) {
try {
User user = new User(request.getName(), request.getEmail(), UserRole.USER);
return Either.right(user);
} catch (Exception e) {
return Either.left("Failed to create user: " + e.getMessage());
}
}
private Either<String, User> saveUser(User user) {
try {
// 실제로는 데이터베이스 저장
return Either.right(user);
} catch (Exception e) {
return Either.left("Failed to save user: " + e.getMessage());
}
}
private Either<String, User> sendWelcomeEmail(User user) {
try {
// 실제로는 이메일 발송
return Either.right(user);
} catch (Exception e) {
return Either.left("Failed to send welcome email: " + e.getMessage());
}
}
private boolean userExists(String email) {
// 실제 구현
return false;
}
}
|