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.oceanus.date.OceanusDate;
20 import io.github.tonywasher.joceanus.oceanus.format.OceanusDataFormatter;
21 import io.github.tonywasher.joceanus.metis.data.MetisDataItem.MetisDataFieldId;
22 import io.github.tonywasher.joceanus.metis.field.MetisFieldItem;
23 import io.github.tonywasher.joceanus.metis.field.MetisFieldSet;
24 import io.github.tonywasher.joceanus.metis.list.MetisListKey;
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 {
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 PrometheusMapsDataSetInstance pDataSet,
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 PrometheusMapsListInstance mySharedList = pDataSet.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
134
135
136
137
138 private Map<MetisDataFieldId, PrometheusMapsFieldInstance> getFieldMap() {
139 return theFieldMap;
140 }
141
142
143
144
145
146
147 private PrometheusMapsDateInstance getDateMap() {
148 return theDateMap;
149 }
150
151
152
153
154
155
156 void declareFieldIdMap(final MetisDataFieldId pFieldId) {
157 theFieldMap.put(pFieldId, new PrometheusMapsFieldInstance(theListKey, pFieldId));
158 }
159
160
161
162
163
164
165
166 void declareFieldIdMap(final MetisDataFieldId pFieldId,
167 final Function<PrometheusDataItem, Boolean> pFilter) {
168 theFieldMap.put(pFieldId, new PrometheusMapsFieldInstance(theListKey, pFieldId, pFilter));
169 }
170
171
172
173
174
175
176
177 void declareFieldIdMap(final MetisDataFieldId pFieldId,
178 final PrometheusMapsListInstance pMap) {
179 theFieldMap.put(pFieldId, pMap.getFieldMap().get(pFieldId));
180 }
181
182
183
184
185
186
187
188
189 void declareDateIdMap(final MetisDataFieldId pOwnerId,
190 final MetisDataFieldId pDateId,
191 final boolean pAllowNull) {
192 theDateMap = new PrometheusMapsDateInstance(theListKey, pOwnerId, pDateId, pAllowNull);
193 }
194
195
196
197
198
199
200 void addItemToMaps(final PrometheusDataItem pItem) {
201
202 for (PrometheusMapsFieldInstance myMap : theFieldMap.values()) {
203 myMap.addItemToMap(pItem);
204 }
205
206
207 if (theDateMap != null) {
208 theDateMap.addItemToMap(pItem);
209 }
210 }
211
212
213
214
215
216
217
218
219 boolean isKeyDuplicate(final MetisDataFieldId pFieldId,
220 final PrometheusDataItem pItem) {
221 final PrometheusMapsFieldInstance myMap = theFieldMap.get(pFieldId);
222 return myMap != null && myMap.isKeyDuplicate(pItem);
223 }
224
225
226
227
228
229
230
231
232 boolean isKeyAvailable(final MetisDataFieldId pFieldId,
233 final Object pKey) {
234 final PrometheusMapsFieldInstance myMap = theFieldMap.get(pFieldId);
235 return myMap == null || myMap.isKeyAvailable(pKey);
236 }
237
238
239
240
241
242
243
244
245 PrometheusDataItem getItemForKey(final MetisDataFieldId pFieldId,
246 final Object pKey) {
247 final PrometheusMapsFieldInstance myMap = theFieldMap.get(pFieldId);
248 return myMap == null ? null : myMap.findItemInMap(pKey);
249 }
250
251
252
253
254 void resetMaps() {
255
256 for (PrometheusMapsFieldInstance myMap : theFieldMap.values()) {
257 myMap.resetMap();
258 }
259
260
261 if (theDateMap != null) {
262 theDateMap.resetMap();
263 }
264 }
265
266
267
268
269
270
271
272
273 boolean isDateAvailable(final PrometheusDataItem pOwner,
274 final OceanusDate pDate) {
275 return theDateMap != null && theDateMap.isDateAvailable(pOwner, pDate);
276 }
277
278
279
280
281
282
283
284 boolean isDateDuplicate(final PrometheusDataItem pItem) {
285 return theDateMap != null && theDateMap.isDateDuplicate(pItem);
286 }
287 }