Skip to content

Commit 810d22e

Browse files
committed
feat(context): naive storage for values in a context
Refs: #4
1 parent 9df2f29 commit 810d22e

File tree

3 files changed

+146
-4
lines changed

3 files changed

+146
-4
lines changed

.editorconfig

+4-4
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ ij_java_case_statement_on_separate_line = true
6868
ij_java_catch_on_new_line = false
6969
ij_java_class_annotation_wrap = split_into_lines
7070
ij_java_class_brace_style = end_of_line
71-
ij_java_class_count_to_use_import_on_demand = 5
71+
ij_java_class_count_to_use_import_on_demand = 100
7272
ij_java_class_names_in_javadoc = 1
7373
ij_java_do_not_indent_top_level_class_members = false
7474
ij_java_do_not_wrap_after_single_annotation = false
@@ -104,7 +104,7 @@ ij_java_for_statement_wrap = off
104104
ij_java_generate_final_locals = false
105105
ij_java_generate_final_parameters = false
106106
ij_java_if_brace_force = never
107-
ij_java_imports_layout = *,|,javax.**,java.**,|,$*
107+
ij_java_imports_layout = *
108108
ij_java_indent_case_from_switch = true
109109
ij_java_insert_inner_class_imports = false
110110
ij_java_insert_override_annotation = true
@@ -124,7 +124,7 @@ ij_java_keep_simple_methods_in_one_line = false
124124
ij_java_label_indent_absolute = false
125125
ij_java_label_indent_size = 0
126126
ij_java_lambda_brace_style = end_of_line
127-
ij_java_layout_static_imports_separately = true
127+
ij_java_layout_static_imports_separately = false
128128
ij_java_line_comment_add_space = false
129129
ij_java_line_comment_at_first_column = true
130130
ij_java_method_annotation_wrap = split_into_lines
@@ -134,7 +134,7 @@ ij_java_method_parameters_new_line_after_left_paren = false
134134
ij_java_method_parameters_right_paren_on_new_line = false
135135
ij_java_method_parameters_wrap = off
136136
ij_java_modifier_list_wrap = false
137-
ij_java_names_count_to_use_import_on_demand = 3
137+
ij_java_names_count_to_use_import_on_demand = 100
138138
ij_java_packages_to_use_import_on_demand = java.awt.*,javax.swing.*
139139
ij_java_parameter_annotation_wrap = off
140140
ij_java_parentheses_expression_new_line_after_left_paren = false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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.List;
28+
import org.hamcrest.MatcherAssert;
29+
import org.hamcrest.Matchers;
30+
import org.junit.jupiter.api.Test;
31+
32+
/**
33+
* I am a test for the {@link ChainContext}.
34+
*
35+
* @since 0.1.0
36+
*/
37+
final class ChainContextTest {
38+
@Test
39+
void shouldGetValueOutOfContext() {
40+
final ChainContext target = new ChainContext();
41+
final List<String> actual = target.fetch("data-by-key");
42+
MatcherAssert.assertThat(
43+
"Should be empty for a non-existent key?",
44+
actual,
45+
Matchers.empty()
46+
);
47+
}
48+
49+
@Test
50+
void shouldBeAbleToAddNewData() {
51+
final ChainContext target = new ChainContext();
52+
target.add("data-by-key", () -> List.of("value"));
53+
MatcherAssert.assertThat(
54+
"Should be able to add and retrieve data",
55+
target.fetch("data-by-key"),
56+
Matchers.contains("value")
57+
);
58+
}
59+
}

0 commit comments

Comments
 (0)