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.MetisDataResource;
21 import io.github.tonywasher.joceanus.metis.field.MetisFieldItem;
22 import io.github.tonywasher.joceanus.metis.field.MetisFieldSet;
23 import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataItem;
24
25 import java.util.HashMap;
26 import java.util.Map;
27
28
29
30
31 public class PrometheusMapsTouchCtl
32 implements MetisFieldItem {
33
34
35
36 private static final MetisFieldSet<PrometheusMapsTouchCtl> FIELD_DEFS = MetisFieldSet.newFieldSet(PrometheusMapsTouchCtl.class);
37
38
39
40
41 static {
42 FIELD_DEFS.declareLocalField(MetisDataResource.DATA_VERSION, PrometheusMapsTouchCtl::getVersion);
43 FIELD_DEFS.declareLocalField(PrometheusMapsResource.MAPS_TOUCHMAP, PrometheusMapsTouchCtl::getTouchMap);
44 }
45
46
47
48
49 private final Map<Integer, PrometheusMapsDataSetTouch> theHistory;
50
51
52
53
54 private Integer theVersion;
55
56
57
58
59 private PrometheusMapsDataSetTouch theTouchMap;
60
61
62
63
64 PrometheusMapsTouchCtl() {
65 theHistory = new HashMap<>();
66 theVersion = 0;
67 setVersion(0);
68 }
69
70 @Override
71 public MetisFieldSetDef getDataFieldSet() {
72 return FIELD_DEFS;
73 }
74
75 @Override
76 public String formatObject(final OceanusDataFormatter pFormatter) {
77 return PrometheusMapsTouchCtl.class.getSimpleName();
78 }
79
80
81
82
83
84
85 private Integer getVersion() {
86 return theVersion;
87 }
88
89
90
91
92
93
94 private PrometheusMapsDataSetTouch getTouchMap() {
95 return theTouchMap;
96 }
97
98
99
100
101
102
103 void setVersion(final Integer pVersion) {
104
105 if (pVersion < theVersion) {
106
107 theHistory.entrySet().removeIf(myEntry -> myEntry.getKey() > pVersion);
108 }
109
110
111 theTouchMap = theHistory.computeIfAbsent(pVersion, i -> new PrometheusMapsDataSetTouch());
112 theVersion = pVersion;
113 }
114
115
116
117
118
119
120
121 void recordTouch(final PrometheusDataItem pTouchedItem,
122 final PrometheusDataItem pTouchingItem) {
123
124 theTouchMap.recordTouch(pTouchedItem, pTouchingItem);
125 }
126
127
128
129
130
131
132
133 boolean isTouched(final PrometheusDataItem pItem) {
134 return theTouchMap.isTouched(pItem);
135 }
136
137
138
139
140 void resetMap() {
141 theTouchMap.resetMap();
142 }
143 }