|
| 1 | +/* |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2024 Decision-Driven Development |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +package ewc.composer.core; |
| 26 | + |
| 27 | +import java.util.HashMap; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Map; |
| 30 | +import java.util.function.Supplier; |
| 31 | + |
| 32 | +/** |
| 33 | + * I am a context for a chain of operations. I provide a way to pass data between operations. |
| 34 | + * I can (and should, actually) be extended in order to provide application-specific amenities to |
| 35 | + * all the operations in the chain. |
| 36 | + * |
| 37 | + * @since 0.1.0 |
| 38 | + */ |
| 39 | +public class ChainContext { |
| 40 | + /** |
| 41 | + * The storage for all the data that needs to be passed between operations. The data is stored |
| 42 | + * as a map of keys and suppliers of lists of strings. The suppliers are used to provide the |
| 43 | + * data only when it is needed, so the real data is not stored in the map until it is requested. |
| 44 | + */ |
| 45 | + private final Map<String, Supplier<List<String>>> data; |
| 46 | + |
| 47 | + /** |
| 48 | + * Ctor. |
| 49 | + */ |
| 50 | + public ChainContext() { |
| 51 | + this(new HashMap<>()); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Ctor. |
| 56 | + * |
| 57 | + * @param data The data to store in the context. |
| 58 | + */ |
| 59 | + private ChainContext(final Map<String, Supplier<List<String>>> data) { |
| 60 | + this.data = data; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Retrieves the data from the context by the key. If the data is not present, an empty list is |
| 65 | + * returned. |
| 66 | + * |
| 67 | + * @param key The key to retrieve the data by. |
| 68 | + * @return The data stored under the key or an empty list if the data is not present. |
| 69 | + */ |
| 70 | + public List<String> fetch(final String key) { |
| 71 | + return this.data.getOrDefault(key, List::of).get(); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Adds the data to the context under the provided key. |
| 76 | + * |
| 77 | + * @param key The key to store the data under. |
| 78 | + * @param supplier The supplier of the data to store. |
| 79 | + */ |
| 80 | + public void add(final String key, final Supplier<List<String>> supplier) { |
| 81 | + this.data.put(key, supplier); |
| 82 | + } |
| 83 | +} |
0 commit comments