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.field.MetisFieldItem;
21 import io.github.tonywasher.joceanus.metis.field.MetisFieldSet;
22 import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataItem;
23 import io.github.tonywasher.joceanus.prometheus.maps.PrometheusMapsInstanceElement.PrometheusMapsInstanceElementItem;
24 import io.github.tonywasher.joceanus.prometheus.maps.PrometheusMapsInstanceElement.PrometheusMapsInstanceElementList;
25
26 import java.util.LinkedHashMap;
27 import java.util.Map;
28
29
30
31
32 public abstract class PrometheusMapsBaseInstance
33 implements MetisFieldItem {
34
35
36
37 private static final MetisFieldSet<PrometheusMapsBaseInstance> FIELD_DEFS
38 = MetisFieldSet.newFieldSet(PrometheusMapsBaseInstance.class);
39
40
41
42
43 static {
44 FIELD_DEFS.declareLocalField(PrometheusMapsResource.MAPS_INSTANCEMAP, PrometheusMapsBaseInstance::getMap);
45 }
46
47
48
49
50 private final Map<Object, PrometheusMapsInstanceElement> theMap;
51
52
53
54
55 PrometheusMapsBaseInstance() {
56 theMap = new LinkedHashMap<>();
57 }
58
59 @Override
60 public MetisFieldSetDef getDataFieldSet() {
61 return FIELD_DEFS;
62 }
63
64 @Override
65 public String formatObject(final OceanusDataFormatter pFormatter) {
66 return PrometheusMapsBaseInstance.class.getSimpleName();
67 }
68
69
70
71
72
73
74 private Map<Object, PrometheusMapsInstanceElement> getMap() {
75 return theMap;
76 }
77
78
79
80
81
82
83
84 void addItemToMap(final Object pKey,
85 final PrometheusDataItem pItem) {
86 final PrometheusMapsInstanceElement myCurr = theMap.get(pKey);
87 if (myCurr != null) {
88 theMap.put(pKey, new PrometheusMapsInstanceElementList(myCurr, pItem));
89 } else {
90 theMap.put(pKey, new PrometheusMapsInstanceElementItem(pItem));
91 }
92 }
93
94
95
96
97
98
99
100 boolean isKeyDuplicate(final Object pKey) {
101 return theMap.get(pKey) instanceof PrometheusMapsInstanceElementList;
102 }
103
104
105
106
107
108
109
110 boolean isKeyAvailable(final Object pKey) {
111 return theMap.get(pKey) == null;
112 }
113
114
115
116
117
118
119
120 PrometheusDataItem getItemForKey(final Object pKey) {
121 final PrometheusMapsInstanceElement myElement = theMap.get(pKey);
122 if (myElement instanceof PrometheusMapsInstanceElementItem myItem) {
123 return myItem.getItem();
124 }
125 if (myElement instanceof PrometheusMapsInstanceElementList) {
126 return myElement.getList().get(0);
127 }
128 return null;
129 }
130
131
132
133
134 void resetMap() {
135 theMap.clear();
136 }
137 }