1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.github.tonywasher.joceanus.prometheus.maps;
18
19 import io.github.tonywasher.joceanus.metis.data.MetisDataItem.MetisDataFieldId;
20 import io.github.tonywasher.joceanus.metis.field.MetisFieldItem;
21 import io.github.tonywasher.joceanus.metis.field.MetisFieldSet;
22 import io.github.tonywasher.joceanus.metis.list.MetisListKey;
23 import io.github.tonywasher.joceanus.oceanus.date.OceanusDate;
24 import io.github.tonywasher.joceanus.oceanus.format.OceanusDataFormatter;
25 import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataItem;
26 import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataResource;
27
28 import java.util.LinkedHashMap;
29 import java.util.Map;
30 import java.util.function.Function;
31
32
33
34
35 public class PrometheusMapsListInstance
36 implements MetisFieldItem, PrometheusMapsFieldHolder {
37
38
39
40 private static final MetisFieldSet<PrometheusMapsListInstance> FIELD_DEFS
41 = MetisFieldSet.newFieldSet(PrometheusMapsListInstance.class);
42
43
44
45
46 static {
47 FIELD_DEFS.declareLocalField(PrometheusDataResource.DATAITEM_TYPE, PrometheusMapsListInstance::getListKey);
48 FIELD_DEFS.declareLocalField(PrometheusMapsResource.MAPS_INSTANCEMAP, PrometheusMapsListInstance::getFieldMap);
49 FIELD_DEFS.declareLocalField(PrometheusMapsResource.MAPS_DATEMAP, PrometheusMapsListInstance::getDateMap);
50 }
51
52
53
54
55 private final MetisListKey theListKey;
56
57
58
59
60 private final Map<MetisDataFieldId, PrometheusMapsFieldInstance> theFieldMap;
61
62
63
64
65 private PrometheusMapsDateInstance theDateMap;
66
67
68
69
70
71
72 PrometheusMapsListInstance(final MetisListKey pKey) {
73 theListKey = pKey;
74 theFieldMap = new LinkedHashMap<>();
75 }
76
77
78
79
80
81
82
83 PrometheusMapsListInstance(final PrometheusMapsListHolder pListHolder,
84 final PrometheusMapsListInstance pSource) {
85
86 this(pSource.getListKey());
87
88
89 for (PrometheusMapsFieldInstance myMap : pSource.getFieldMap().values()) {
90
91 final MetisDataFieldId myFieldId = myMap.getFieldId();
92 final MetisListKey myListKey = myMap.getListKey();
93
94
95 if (theListKey.equals(myListKey)) {
96
97 theFieldMap.put(myFieldId, new PrometheusMapsFieldInstance(myMap));
98
99
100 } else {
101
102 final PrometheusMapsFieldHolder mySharedList = pListHolder.getList(myListKey);
103 final PrometheusMapsFieldInstance mySharedField = mySharedList.getFieldMap().get(myFieldId);
104 theFieldMap.put(myFieldId, mySharedField);
105 }
106 }
107
108
109 if (theDateMap != null) {
110 theDateMap = new PrometheusMapsDateInstance(theDateMap);
111 }
112 }
113
114 @Override
115 public MetisFieldSetDef getDataFieldSet() {
116 return FIELD_DEFS;
117 }
118
119 @Override
120 public String formatObject(final OceanusDataFormatter pFormatter) {
121 return pFormatter.formatObject(theListKey);
122 }
123
124
125
126
127
128
129 MetisListKey getListKey() {
130 return theListKey;
131 }
132
133 @Override
134 public Map<MetisDataFieldId, PrometheusMapsFieldInstance> getFieldMap() {
135 return theFieldMap;
136 }
137
138
139
140
141
142
143 private PrometheusMapsDateInstance getDateMap() {
144 return theDateMap;
145 }
146
147
148
149
150
151
152 void declareFieldIdMap(final MetisDataFieldId pFieldId) {
153 theFieldMap.put(pFieldId, new PrometheusMapsFieldInstance(theListKey, pFieldId));
154 }
155
156
157
158
159
160
161
162 void declareFieldIdMap(final MetisDataFieldId pFieldId,
163 final Function<PrometheusDataItem, Boolean> pFilter) {
164 theFieldMap.put(pFieldId, new PrometheusMapsFieldInstance(theListKey, pFieldId, pFilter));
165 }
166
167
168
169
170
171
172
173 void declareFieldIdMap(final MetisDataFieldId pFieldId,
174 final PrometheusMapsListInstance pMap) {
175 theFieldMap.put(pFieldId, pMap.getFieldMap().get(pFieldId));
176 }
177
178
179
180
181
182
183
184
185 void declareDateIdMap(final MetisDataFieldId pOwnerId,
186 final MetisDataFieldId pDateId,
187 final boolean pAllowNull) {
188 theDateMap = new PrometheusMapsDateInstance(theListKey, pOwnerId, pDateId, pAllowNull);
189 }
190
191
192
193
194
195
196 void addItemToMaps(final PrometheusDataItem pItem) {
197
198 for (PrometheusMapsFieldInstance myMap : theFieldMap.values()) {
199 myMap.addItemToMap(pItem);
200 }
201
202
203 if (theDateMap != null) {
204 theDateMap.addItemToMap(pItem);
205 }
206 }
207
208
209
210
211
212
213
214
215 boolean isKeyDuplicate(final MetisDataFieldId pFieldId,
216 final PrometheusDataItem pItem) {
217 final PrometheusMapsFieldInstance myMap = theFieldMap.get(pFieldId);
218 return myMap != null && myMap.isKeyDuplicate(pItem);
219 }
220
221
222
223
224
225
226
227
228 boolean isKeyAvailable(final MetisDataFieldId pFieldId,
229 final Object pKey) {
230 final PrometheusMapsFieldInstance myMap = theFieldMap.get(pFieldId);
231 return myMap == null || myMap.isKeyAvailable(pKey);
232 }
233
234
235
236
237
238
239
240
241 PrometheusDataItem getItemForKey(final MetisDataFieldId pFieldId,
242 final Object pKey) {
243 final PrometheusMapsFieldInstance myMap = theFieldMap.get(pFieldId);
244 return myMap == null ? null : myMap.findItemInMap(pKey);
245 }
246
247
248
249
250 void resetMaps() {
251
252 for (PrometheusMapsFieldInstance myMap : theFieldMap.values()) {
253 myMap.resetMap();
254 }
255
256
257 if (theDateMap != null) {
258 theDateMap.resetMap();
259 }
260 }
261
262
263
264
265
266
267
268
269 boolean isDateAvailable(final PrometheusDataItem pOwner,
270 final OceanusDate pDate) {
271 return theDateMap != null && theDateMap.isDateAvailable(pOwner, pDate);
272 }
273
274
275
276
277
278
279
280 boolean isDateDuplicate(final PrometheusDataItem pItem) {
281 return theDateMap != null && theDateMap.isDateDuplicate(pItem);
282 }
283 }