View Javadoc
1   /*
2    * Copyright 2014 Jin Kwon <jinahya_at_gmail.com>.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * A context between clients and file backs.
33   *
34   * @author Jin Kwon <jinahya_at_gmail.com>
35   */
36  public interface FileContext {
37  
38  
39      /**
40       * Properties can be configured within file contexts.
41       */
42      public static enum PropertyKey {
43  
44  
45          /**
46           * A constant for the key of a property whose value is an instance of
47           * {@code Supplier<FileOperation>} which supplies the target operation.
48           */
49          FILE_OPERATION_SUPPLIER,
50          /**
51           * A constant for the key of a property whose value is an instance of
52           * {@code Consumer<String>} which consumes the path name.
53           */
54          PATH_NAME_CONSUMER,
55          /**
56           * A constant for the key of a property whose values si an instance of
57           * {@code Supplier<String>} which supplies the path name.
58           */
59          PATH_NAME_SUPPLIER,
60          /**
61           * A constant for the key of a property whose value is an instance of
62           * {@code Consumer<ReadableByteChannel>} which consumes source file
63           * channel.
64           */
65          SOURCE_CHANNEL_CONSUMER,
66          /**
67           * A constant for the key of a property whose value is an instance of
68           * {@code Consumer<WritableByteChannel>} which consumes the target file
69           * channel.
70           */
71          TARGET_CHANNEL_CONSUMER,
72          /**
73           * A constant for the key of a property whose value is an instance of
74           * {@code Supplier<RedableByteChanel>} which supplies the source file
75           * channel.
76           */
77          SOURCE_CHANNEL_SUPPLIER,
78          /**
79           * A constant for the key of a property whose value is an instance of
80           * {@code Supplier<WritableByteChannel>} which supplies a target file
81           * channel.
82           */
83          TARGET_CHANNEL_SUPPLIER,
84          /**
85           * A constants for the key of a property whose value is an instance of
86           * {@code Consumer<Long>} which consumes the number of bytes copied from
87           * the source file part.
88           */
89          SOURCE_COPIED_CONSUMER,
90          /**
91           * A constant for the key of a property whose value is an instance of
92           * {@code Consumer<Long>} which consumes the number of bytes copied to
93           * target file part.
94           */
95          TARGET_COPIED_CONSUMER,
96          /**
97           * A constant for the key of a property whose value is an instance of
98           * {@code Supplier<ByteBuffer>} which supplies the key bytes for
99           * locating the source file part.
100          */
101         SOURCE_KEY_SUPPLIER,
102         /**
103          * A constant for the key of a property whose value is an instance of
104          * {@code Supplier<ByteBuffer>} which supplies the key bytes the
105          * locating the target file part.
106          */
107         TARGET_KEY_SUPPLIER,
108         /**
109          * A constant for the key of a property whose value is an instance of
110          * {@code Consumer<Object>} which consumes an implementation specific
111          * type of source file reference.
112          */
113         SOURCE_OBJECT_CONSUMER,
114         /**
115          * A constant for the key of a property whose value is an instance of
116          * {@code Consumer<Object>} which consumes an implementation specific
117          * type of target file reference.
118          */
119         TARGET_OBJECT_CONSUMER
120 
121 
122     }
123 
124 
125     /**
126      * Returns an optional property value mapped to specified
127      * {@code propertyKey}.
128      *
129      * @param propertyKey the property key
130      *
131      * @return an optional value.
132      */
133     Optional<Object> property(PropertyKey propertyKey);
134 
135 
136     /**
137      * Returns an optional property value mapped to specified
138      * {@code propertyKey}.
139      *
140      * @param <T> value type parameter
141      * @param proprtyKey property key
142      * @param valueType value type.
143      *
144      * @return an optional value of property.
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      * Sets a new property value mapped to specified {@code propertyKey}. Note
159      * that providing {@code null} for {@code propertyValue} will remove
160      * previous mapping.
161      *
162      * @param propertyKey the property key
163      * @param propertyValue the property value; {@code null} for removal of
164      * mapping.
165      *
166      * @return an optional property value previously mapped to specified
167      * {@code propertyKey}.
168      */
169     Optional<Object> property(PropertyKey propertyKey, Object propertyValue);
170 
171 
172     /**
173      * Sets a new property value mapped to specified {@code propertyKey}.
174      *
175      * @param <T> value type parameter
176      * @param propertyKey property key
177      * @param propertyValue the new property value.
178      * @param valueType value type.
179      *
180      * @return an optional property value previously mapped to specified
181      * {@code propertyKey}.
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      * Return the current property value mapped to
198      * {@link PropertyKey#FILE_OPERATION_SUPPLIER}.
199      *
200      * @return the current value mapped to
201      * {@link PropertyKey#FILE_OPERATION_SUPPLIER} or {@code null} if no
202      * mappings found.
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      * Sets a new property value mapped to
215      * {@link PropertyKey#FILE_OPERATION_SUPPLIER}.
216      *
217      * @param fileOperationSupplier the new value.
218      *
219      * @return the previous value mapped to
220      * {@link PropertyKey#FILE_OPERATION_SUPPLIER} or {@code null} if there is
221      * no mappings.
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      * Returns the current property value mapped to
309      * {@link PropertyKey#SOURCE_COPIED_CONSUMER}.
310      *
311      * @return the current property value mapped to
312      * {@link PropertyKey#SOURCE_COPIED_CONSUMER} or {@code null} if no mappings
313      * found.
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      * Sets the new value for {@link PropertyKey#SOURCE_COPIED_CONSUMER}.
325      *
326      * @param sourceCopiedConsumer the new value; {@code null} for removal of
327      * entry.
328      *
329      * @return previous value mapped; possibly {@code null}.
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