1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package com.github.jinahya.simple.file.back;
19
20
21 import com.github.jinahya.simple.file.back.FileBack.FileOperation;
22 import java.nio.ByteBuffer;
23 import java.nio.channels.ReadableByteChannel;
24 import java.nio.channels.WritableByteChannel;
25 import java.util.Optional;
26 import static java.util.Optional.ofNullable;
27 import java.util.function.Consumer;
28 import java.util.function.Supplier;
29
30
31
32
33
34
35
36 public interface FileContext {
37
38
39
40
41
42 public static enum PropertyKey {
43
44
45
46
47
48
49 FILE_OPERATION_SUPPLIER,
50
51
52
53
54 PATH_NAME_CONSUMER,
55
56
57
58
59 PATH_NAME_SUPPLIER,
60
61
62
63
64
65 SOURCE_CHANNEL_CONSUMER,
66
67
68
69
70
71 TARGET_CHANNEL_CONSUMER,
72
73
74
75
76
77 SOURCE_CHANNEL_SUPPLIER,
78
79
80
81
82
83 TARGET_CHANNEL_SUPPLIER,
84
85
86
87
88
89 SOURCE_COPIED_CONSUMER,
90
91
92
93
94
95 TARGET_COPIED_CONSUMER,
96
97
98
99
100
101 SOURCE_KEY_SUPPLIER,
102
103
104
105
106
107 TARGET_KEY_SUPPLIER,
108
109
110
111
112
113 SOURCE_OBJECT_CONSUMER,
114
115
116
117
118
119 TARGET_OBJECT_CONSUMER
120
121
122 }
123
124
125
126
127
128
129
130
131
132
133 Optional<Object> property(PropertyKey propertyKey);
134
135
136
137
138
139
140
141
142
143
144
145
146 default <T> Optional<T> property(final PropertyKey proprtyKey,
147 final Class<T> valueType) {
148
149 if (valueType == null) {
150 throw new NullPointerException("null type");
151 }
152
153 return ofNullable(valueType.cast(property(proprtyKey).orElse(null)));
154 }
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169 Optional<Object> property(PropertyKey propertyKey, Object propertyValue);
170
171
172
173
174
175
176
177
178
179
180
181
182
183 default <T> Optional<T> property(final PropertyKey propertyKey,
184 final T propertyValue,
185 final Class<T> valueType) {
186
187 if (valueType == null) {
188 throw new NullPointerException("null type");
189 }
190
191 return ofNullable(valueType.cast(property(propertyKey, propertyValue)
192 .orElse(null)));
193 }
194
195
196
197
198
199
200
201
202
203
204 @SuppressWarnings("unchecked")
205 default Supplier<FileOperation> fileOperationSupplier() {
206
207 return (Supplier<FileOperation>) property(
208 PropertyKey.FILE_OPERATION_SUPPLIER)
209 .orElse(null);
210 }
211
212
213
214
215
216
217
218
219
220
221
222
223 @SuppressWarnings("unchecked")
224 default Supplier<FileOperation> fileOperationSupplier(
225 final Supplier<FileOperation> fileOperationSupplier) {
226
227 return (Supplier<FileOperation>) property(
228 PropertyKey.FILE_OPERATION_SUPPLIER, fileOperationSupplier)
229 .orElse(null);
230 }
231
232
233 @SuppressWarnings("unchecked")
234 default Consumer<String> pathNameConsumer() {
235
236 return (Consumer<String>) property(PropertyKey.PATH_NAME_CONSUMER)
237 .orElse(null);
238 }
239
240
241 @SuppressWarnings("unchecked")
242 default Consumer<String> pathNameConsumer(
243 final Consumer<String> pathNameConsumer) {
244
245 return (Consumer<String>) property(PropertyKey.PATH_NAME_CONSUMER,
246 pathNameConsumer)
247 .orElse(null);
248 }
249
250
251 @SuppressWarnings("unchecked")
252 default Supplier<String> pathNameSupplier() {
253
254 return (Supplier<String>) property(PropertyKey.PATH_NAME_SUPPLIER)
255 .orElse(null);
256 }
257
258
259 @SuppressWarnings("unchecked")
260 default Supplier<String> pathNameSupplier(
261 final Supplier<String> pathNameSupplier) {
262
263 return (Supplier<String>) property(PropertyKey.PATH_NAME_SUPPLIER,
264 pathNameSupplier)
265 .orElse(null);
266 }
267
268
269 @SuppressWarnings("unchecked")
270 default Consumer<ReadableByteChannel> sourceChannelConsumer() {
271
272 return (Consumer<ReadableByteChannel>) property(
273 PropertyKey.SOURCE_CHANNEL_CONSUMER)
274 .orElse(null);
275 }
276
277
278 @SuppressWarnings("unchecked")
279 default Consumer<ReadableByteChannel> sourceChannelConsumer(
280 final Consumer<ReadableByteChannel> sourceChannelConsumer) {
281
282 return (Consumer<ReadableByteChannel>) property(
283 PropertyKey.SOURCE_CHANNEL_CONSUMER, sourceChannelConsumer)
284 .orElse(null);
285 }
286
287
288 @SuppressWarnings("unchecked")
289 default Supplier<ReadableByteChannel> sourceChannelSupplier() {
290
291 return (Supplier<ReadableByteChannel>) property(
292 PropertyKey.SOURCE_CHANNEL_SUPPLIER)
293 .orElse(null);
294 }
295
296
297 @SuppressWarnings("unchecked")
298 default Supplier<ReadableByteChannel> sourceChannelSupplier(
299 final Supplier<ReadableByteChannel> sourceChannelSuppleir) {
300
301 return (Supplier<ReadableByteChannel>) property(
302 PropertyKey.SOURCE_CHANNEL_SUPPLIER, sourceChannelSuppleir)
303 .orElse(null);
304 }
305
306
307
308
309
310
311
312
313
314
315 @SuppressWarnings("unchecked")
316 default Consumer<Long> sourceCopiedConsumer() {
317
318 return (Consumer<Long>) property(PropertyKey.SOURCE_COPIED_CONSUMER)
319 .orElse(null);
320 }
321
322
323
324
325
326
327
328
329
330
331 @SuppressWarnings("unchecked")
332 default Consumer<Long> sourceCopiedConsumer(
333 final Consumer<Long> sourceCopiedConsumer) {
334
335 return (Consumer<Long>) property(PropertyKey.SOURCE_COPIED_CONSUMER,
336 sourceCopiedConsumer)
337 .orElse(null);
338 }
339
340
341 @SuppressWarnings("unchecked")
342 default Supplier<ByteBuffer> sourceKeySupplier() {
343
344 return (Supplier<ByteBuffer>) property(PropertyKey.SOURCE_KEY_SUPPLIER)
345 .orElse(null);
346 }
347
348
349 @SuppressWarnings("unchecked")
350 default Supplier<ByteBuffer> sourceKeySupplier(
351 final Supplier<ByteBuffer> sourceKeySupplier) {
352
353 return (Supplier<ByteBuffer>) property(PropertyKey.SOURCE_KEY_SUPPLIER,
354 sourceKeySupplier)
355 .orElse(null);
356 }
357
358
359 @SuppressWarnings("unchecked")
360 default Consumer<Object> sourceObjectConsumer() {
361
362 return (Consumer<Object>) property(PropertyKey.SOURCE_OBJECT_CONSUMER)
363 .orElse(null);
364 }
365
366
367 @SuppressWarnings("unchecked")
368 default Consumer<Object> sourceObjectConsumer(
369 final Consumer<Object> sourceObjectConsumer) {
370
371 return (Consumer<Object>) property(PropertyKey.SOURCE_OBJECT_CONSUMER,
372 sourceObjectConsumer)
373 .orElse(null);
374 }
375
376
377 @SuppressWarnings("unchecked")
378 default Consumer<Long> targetCopiedConsumer() {
379
380 return (Consumer<Long>) property(PropertyKey.TARGET_COPIED_CONSUMER)
381 .orElse(null);
382 }
383
384
385 @SuppressWarnings("unchecked")
386 default Consumer<Long> targetCopiedConsumer(
387 final Consumer<Long> targetCopiedConsumer) {
388
389 return (Consumer<Long>) property(PropertyKey.TARGET_COPIED_CONSUMER,
390 targetCopiedConsumer)
391 .orElse(null);
392 }
393
394
395 @SuppressWarnings("unchecked")
396 default Supplier<ByteBuffer> targetKeySupplier() {
397
398 return (Supplier<ByteBuffer>) property(PropertyKey.TARGET_KEY_SUPPLIER)
399 .orElse(null);
400 }
401
402
403 @SuppressWarnings("unchecked")
404 default Supplier<ByteBuffer> targetKeySupplier(
405 final Supplier<ByteBuffer> targetKeySupplier) {
406
407 return (Supplier<ByteBuffer>) property(PropertyKey.TARGET_KEY_SUPPLIER,
408 targetKeySupplier)
409 .orElse(null);
410 }
411
412
413 @SuppressWarnings("unchecked")
414 default Consumer<WritableByteChannel> targetChannelConsumer() {
415
416 return (Consumer<WritableByteChannel>) property(
417 PropertyKey.TARGET_CHANNEL_CONSUMER)
418 .orElse(null);
419 }
420
421
422 @SuppressWarnings("unchecked")
423 default Consumer<WritableByteChannel> targetChannelConsumer(
424 final Consumer<WritableByteChannel> targetChannelConsumer) {
425
426 return (Consumer<WritableByteChannel>) property(
427 PropertyKey.TARGET_CHANNEL_CONSUMER, targetChannelConsumer)
428 .orElse(null);
429 }
430
431
432 @SuppressWarnings("unchecked")
433 default Supplier<WritableByteChannel> targetChannelSupplier() {
434
435 return (Supplier<WritableByteChannel>) property(
436 PropertyKey.TARGET_CHANNEL_SUPPLIER)
437 .orElse(null);
438 }
439
440
441 @SuppressWarnings("unchecked")
442 default Supplier<WritableByteChannel> targetChannelSupplier(
443 final Supplier<WritableByteChannel> targetChannelSupplier) {
444
445 return (Supplier<WritableByteChannel>) property(
446 PropertyKey.TARGET_CHANNEL_SUPPLIER, targetChannelSupplier)
447 .orElse(null);
448 }
449
450
451 @SuppressWarnings("unchecked")
452 default Consumer<Object> targetObjectConsumer() {
453
454 return (Consumer<Object>) property(PropertyKey.TARGET_OBJECT_CONSUMER)
455 .orElse(null);
456 }
457
458
459 @SuppressWarnings("unchecked")
460 default Consumer<Object> targetObjectConsumer(
461 final Consumer<Object> targetObjectConsumer) {
462
463 return (Consumer<Object>) property(PropertyKey.TARGET_OBJECT_CONSUMER,
464 targetObjectConsumer)
465 .orElse(null);
466 }
467
468
469 }
470