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.format.OceanusDataFormatter;
20 import io.github.tonywasher.joceanus.metis.data.MetisDataItem.MetisDataFieldId;
21 import io.github.tonywasher.joceanus.metis.field.MetisFieldSet;
22 import io.github.tonywasher.joceanus.metis.list.MetisListKey;
23 import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataItem;
24 import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataResource;
25
26 import java.util.function.Function;
27
28
29
30
31 public class PrometheusMapsFieldInstance
32 extends PrometheusMapsBaseInstance {
33
34
35
36 private static final MetisFieldSet<PrometheusMapsFieldInstance> FIELD_DEFS
37 = MetisFieldSet.newFieldSet(PrometheusMapsFieldInstance.class);
38
39
40
41
42 static {
43 FIELD_DEFS.declareLocalField(PrometheusDataResource.DATAITEM_TYPE, PrometheusMapsFieldInstance::getListKey);
44 FIELD_DEFS.declareLocalField(PrometheusMapsResource.MAPS_FIELD, PrometheusMapsFieldInstance::getFieldId);
45 }
46
47
48
49
50 private final MetisListKey theListKey;
51
52
53
54
55 private final MetisDataFieldId theFieldId;
56
57
58
59
60 private final Function<PrometheusDataItem, Boolean> theFilter;
61
62
63
64
65 private final boolean allowNull;
66
67
68
69
70
71
72
73 PrometheusMapsFieldInstance(final MetisListKey pKey,
74 final MetisDataFieldId pFieldId) {
75 this(pKey, pFieldId, i -> true);
76 }
77
78
79
80
81
82
83
84
85 PrometheusMapsFieldInstance(final MetisListKey pKey,
86 final MetisDataFieldId pFieldId,
87 final boolean pAllowNull) {
88 this(pKey, pFieldId, i -> true, pAllowNull);
89 }
90
91
92
93
94
95
96
97
98 PrometheusMapsFieldInstance(final MetisListKey pKey,
99 final MetisDataFieldId pFieldId,
100 final Function<PrometheusDataItem, Boolean> pFilter) {
101 this(pKey, pFieldId, pFilter, false);
102 }
103
104
105
106
107
108
109
110
111
112 PrometheusMapsFieldInstance(final MetisListKey pKey,
113 final MetisDataFieldId pFieldId,
114 final Function<PrometheusDataItem, Boolean> pFilter,
115 final boolean pAllowNull) {
116 theListKey = pKey;
117 theFieldId = pFieldId;
118 theFilter = pFilter;
119 allowNull = pAllowNull;
120 }
121
122
123
124
125
126
127 PrometheusMapsFieldInstance(final PrometheusMapsFieldInstance pSource) {
128 theListKey = pSource.getListKey();
129 theFieldId = pSource.getFieldId();
130 theFilter = pSource.theFilter;
131 allowNull = pSource.allowNull;
132 }
133
134 @Override
135 public MetisFieldSetDef getDataFieldSet() {
136 return FIELD_DEFS;
137 }
138
139 @Override
140 public String formatObject(final OceanusDataFormatter pFormatter) {
141 return PrometheusMapsFieldInstance.class.getSimpleName();
142 }
143
144
145
146
147
148
149 MetisListKey getListKey() {
150 return theListKey;
151 }
152
153
154
155
156
157
158 MetisDataFieldId getFieldId() {
159 return theFieldId;
160 }
161
162
163
164
165
166
167 void addItemToMap(final PrometheusDataItem pItem) {
168 if (theFilter.apply(pItem)) {
169 final MetisFieldSetDef myFieldSet = pItem.getDataFieldSet();
170 final MetisFieldDef myField = myFieldSet.getField(theFieldId);
171 final Object myValue = myField.getFieldValue(pItem);
172 if (allowNull || myValue != null) {
173 addItemToMap(myValue, pItem);
174 }
175 }
176 }
177
178
179
180
181
182
183
184 boolean isKeyDuplicate(final PrometheusDataItem pItem) {
185 if (theFilter.apply(pItem)) {
186 final MetisFieldSetDef myFieldSet = pItem.getDataFieldSet();
187 final MetisFieldDef myField = myFieldSet.getField(theFieldId);
188 final Object myValue = myField.getFieldValue(pItem);
189 return isKeyDuplicate(myValue);
190 }
191 return false;
192 }
193
194
195
196
197
198
199
200 public PrometheusDataItem findItemInMap(final Object pKey) {
201 return getItemForKey(pKey);
202 }
203 }