|
17 | 17 |
|
18 | 18 | import java.util.HashMap; |
19 | 19 | import java.util.Map; |
20 | | -import java.util.Map.Entry; |
21 | | -import java.util.Set; |
| 20 | +import java.util.function.Function; |
22 | 21 |
|
23 | 22 | /** |
24 | | - * Context for holding the output returned by the {@link Collector} |
25 | | - * implementations. |
| 23 | + * Context for holding data which can be set by custom walkers or validators and |
| 24 | + * can be retrieved later from the execution context. |
26 | 25 | */ |
27 | 26 | public class CollectorContext { |
28 | 27 | /** |
29 | | - * Map for holding the name and {@link Collector} or a simple Object. |
| 28 | + * Map for the data. |
30 | 29 | */ |
31 | | - private final Map<String, Object> collectorMap; |
32 | | - |
33 | | - /** |
34 | | - * Map for holding the name and {@link Collector} class collect method output. |
35 | | - */ |
36 | | - private final Map<String, Object> collectorLoadMap; |
| 30 | + private final Map<Object, Object> data; |
37 | 31 |
|
38 | 32 | /** |
39 | 33 | * Default constructor will use an unsynchronized HashMap to store data. This is |
40 | 34 | * suitable if the collector context is not shared with multiple threads. |
41 | 35 | */ |
42 | 36 | public CollectorContext() { |
43 | | - this(new HashMap<>(), new HashMap<>()); |
| 37 | + this(new HashMap<>()); |
44 | 38 | } |
45 | 39 |
|
46 | | - /** |
47 | | - * Constructor that creates the context using the specified instances to store |
48 | | - * data. |
49 | | - * <p> |
50 | | - * If for instance the collector context needs to be shared with multiple |
51 | | - * threads a ConcurrentHashMap can be used. |
52 | | - * |
53 | | - * @param collectorMap the collector map |
54 | | - * @param collectorLoadMap the collector load map |
55 | | - */ |
56 | | - public CollectorContext(Map<String, Object> collectorMap, Map<String, Object> collectorLoadMap) { |
57 | | - this.collectorMap = collectorMap; |
58 | | - this.collectorLoadMap = collectorLoadMap; |
59 | | - } |
60 | | - |
61 | | - /** |
62 | | - * Adds a collector with give name. Preserving this method for backward |
63 | | - * compatibility. |
64 | | - * |
65 | | - * @param <E> element |
66 | | - * @param name String |
67 | | - * @param collector Collector |
68 | | - */ |
69 | | - public <E> void add(String name, Collector<E> collector) { |
70 | | - this.collectorMap.put(name, collector); |
| 40 | + /** |
| 41 | + * Constructor that creates the context using the specified instances to store |
| 42 | + * data. |
| 43 | + * <p> |
| 44 | + * If for instance the collector context needs to be shared with multiple |
| 45 | + * threads a ConcurrentHashMap can be used. |
| 46 | + * <p> |
| 47 | + * It is however more likely that the data will only be used after the walk or |
| 48 | + * validation is complete rather then during processing. |
| 49 | + * |
| 50 | + * @param data the data map |
| 51 | + */ |
| 52 | + public CollectorContext(Map<Object, Object> data) { |
| 53 | + this.data = data; |
71 | 54 | } |
72 | 55 |
|
73 | 56 | /** |
74 | | - * Adds a collector or a simple object with give name. |
| 57 | + * Sets data associated with a given key. |
75 | 58 | * |
76 | | - * @param <E> element |
77 | | - * @param object Object |
78 | | - * @param name String |
| 59 | + * @param <T> the return type |
| 60 | + * @param key the key |
| 61 | + * @param value the value |
| 62 | + * @return the previous value |
79 | 63 | */ |
80 | | - public <E> void add(String name, Object object) { |
81 | | - this.collectorMap.put(name, object); |
| 64 | + @SuppressWarnings("unchecked") |
| 65 | + public <T> T put(Object key, Object value) { |
| 66 | + return (T) this.data.put(key, value); |
82 | 67 | } |
83 | 68 |
|
84 | 69 | /** |
85 | | - * Gets the data associated with a given name. Please note if you are collecting |
86 | | - * {@link Collector} instances you should wait till the validation is complete |
87 | | - * to gather all data. |
88 | | - * <p> |
89 | | - * When {@link CollectorContext} is used to collect {@link Collector} instances |
90 | | - * for a particular key, this method will return the {@link Collector} instance |
91 | | - * as long as {@link #loadCollectors} method is not called. Once |
92 | | - * the {@link #loadCollectors} method is called this method will |
93 | | - * return the actual data collected by collector. |
94 | | - * |
95 | | - * @param name String |
96 | | - * @return Object |
97 | | - */ |
98 | | - public Object get(String name) { |
99 | | - Object object = this.collectorMap.get(name); |
100 | | - if (object instanceof Collector<?> && (this.collectorLoadMap.get(name) != null)) { |
101 | | - return this.collectorLoadMap.get(name); |
102 | | - } |
103 | | - return this.collectorMap.get(name); |
104 | | - } |
105 | | - |
106 | | - /** |
107 | | - * Gets the collector map. |
| 70 | + * Gets the data associated with a given key. |
108 | 71 | * |
109 | | - * @return the collector map |
| 72 | + * @param <T> the return type |
| 73 | + * @param key the key |
| 74 | + * @return the value |
110 | 75 | */ |
111 | | - public Map<String, Object> getCollectorMap() { |
112 | | - return this.collectorMap; |
| 76 | + @SuppressWarnings("unchecked") |
| 77 | + public <T> T get(Object key) { |
| 78 | + return (T) this.data.get(key); |
113 | 79 | } |
114 | 80 |
|
115 | 81 | /** |
116 | | - * Returns all the collected data. Please look into {@link #get(String)} method for more details. |
117 | | - * @return Map |
118 | | - */ |
119 | | - public Map<String, Object> getAll() { |
120 | | - Map<String, Object> mergedMap = new HashMap<>(); |
121 | | - mergedMap.putAll(this.collectorMap); |
122 | | - mergedMap.putAll(this.collectorLoadMap); |
123 | | - return mergedMap; |
124 | | - } |
125 | | - |
126 | | - /** |
127 | | - * Combines data with Collector identified by the given name. |
| 82 | + * Computes the value if absent. |
128 | 83 | * |
129 | | - * @param name String |
130 | | - * @param data Object |
| 84 | + * @param <T> the return type |
| 85 | + * @param key the key |
| 86 | + * @param mappingFunction the mapping function |
| 87 | + * @return the value |
131 | 88 | */ |
132 | | - public void combineWithCollector(String name, Object data) { |
133 | | - Object object = this.collectorMap.get(name); |
134 | | - if (object instanceof Collector<?>) { |
135 | | - Collector<?> collector = (Collector<?>) object; |
136 | | - collector.combine(data); |
137 | | - } |
| 89 | + @SuppressWarnings("unchecked") |
| 90 | + public <T> T computeIfAbsent(Object key, Function<Object,Object> mappingFunction) { |
| 91 | + return (T) this.data.computeIfAbsent(key, mappingFunction); |
138 | 92 | } |
139 | 93 |
|
140 | 94 | /** |
141 | | - * Loads data from all collectors. |
| 95 | + * Gets the data map. |
| 96 | + * |
| 97 | + * @return the data map |
142 | 98 | */ |
143 | | - public void loadCollectors() { |
144 | | - Set<Entry<String, Object>> entrySet = this.collectorMap.entrySet(); |
145 | | - for (Entry<String, Object> entry : entrySet) { |
146 | | - if (entry.getValue() instanceof Collector<?>) { |
147 | | - Collector<?> collector = (Collector<?>) entry.getValue(); |
148 | | - this.collectorLoadMap.put(entry.getKey(), collector.collect()); |
149 | | - } |
150 | | - } |
| 99 | + public Map<Object, Object> getData() { |
| 100 | + return this.data; |
151 | 101 | } |
152 | 102 | } |
0 commit comments