View Javadoc
1   /*
2    * Prometheus: Application Framework
3    * Copyright 2012-2026. Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6    * use this file except in compliance with the License.  You may obtain a copy
7    * of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
14   * License for the specific language governing permissions and limitations under
15   * the License.
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   * DataTouchMap for data/EditSet.
30   */
31  public class PrometheusMapsTouchCtl
32          implements MetisFieldItem {
33      /**
34       * Report fields.
35       */
36      private static final MetisFieldSet<PrometheusMapsTouchCtl> FIELD_DEFS = MetisFieldSet.newFieldSet(PrometheusMapsTouchCtl.class);
37  
38      /*
39       * Declare Fields.
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       * The history.
48       */
49      private final Map<Integer, PrometheusMapsDataSetTouch> theHistory;
50  
51      /**
52       * The current version.
53       */
54      private Integer theVersion;
55  
56      /**
57       * The current dataSetTouchMap.
58       */
59      private PrometheusMapsDataSetTouch theTouchMap;
60  
61      /**
62       * Constructor.
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       * Obtain the version.
82       *
83       * @return the version
84       */
85      private Integer getVersion() {
86          return theVersion;
87      }
88  
89      /**
90       * Obtain the touch map.
91       *
92       * @return the map
93       */
94      private PrometheusMapsDataSetTouch getTouchMap() {
95          return theTouchMap;
96      }
97  
98      /**
99       * Set version.
100      *
101      * @param pVersion the version
102      */
103     void setVersion(final Integer pVersion) {
104         /* If we are rolling back */
105         if (pVersion < theVersion) {
106             /* Delete any redundant versions */
107             theHistory.entrySet().removeIf(myEntry -> myEntry.getKey() > pVersion);
108         }
109 
110         /* Obtain the map */
111         theTouchMap = theHistory.computeIfAbsent(pVersion, i -> new PrometheusMapsDataSetTouch());
112         theVersion = pVersion;
113     }
114 
115     /**
116      * Record touch.
117      *
118      * @param pTouchedItem  the item that is touched
119      * @param pTouchingItem the item that touches
120      */
121     void recordTouch(final PrometheusDataItem pTouchedItem,
122                      final PrometheusDataItem pTouchingItem) {
123         /* Access correct map and record the touch */
124         theTouchMap.recordTouch(pTouchedItem, pTouchingItem);
125     }
126 
127     /**
128      * Is the item touched?
129      *
130      * @param pItem the item
131      * @return true/false
132      */
133     boolean isTouched(final PrometheusDataItem pItem) {
134         return theTouchMap.isTouched(pItem);
135     }
136 
137     /**
138      * Reset the map.
139      */
140     void resetMap() {
141         theTouchMap.resetMap();
142     }
143 }