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.field.MetisFieldItem;
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.LinkedHashMap;
27  import java.util.Map;
28  
29  /**
30   * DataTouchMap for List.
31   */
32  public class PrometheusMapsListTouch
33          implements MetisFieldItem {
34      /**
35       * Report fields.
36       */
37      private static final MetisFieldSet<PrometheusMapsListTouch> FIELD_DEFS
38              = MetisFieldSet.newFieldSet(PrometheusMapsListTouch.class);
39  
40      /*
41       * Declare Fields.
42       */
43      static {
44          FIELD_DEFS.declareLocalField(PrometheusDataResource.DATAITEM_TYPE, PrometheusMapsListTouch::getListKey);
45          FIELD_DEFS.declareLocalField(PrometheusMapsResource.MAPS_TOUCHMAP, PrometheusMapsListTouch::getTouchMap);
46      }
47  
48      /**
49       * listKey.
50       */
51      private final MetisListKey theListKey;
52  
53      /**
54       * The map of itemId to touch map.
55       */
56      private final Map<Integer, PrometheusMapsItemTouch> theTouchMap;
57  
58      /**
59       * Constructor.
60       *
61       * @param pKey the listKey
62       */
63      PrometheusMapsListTouch(final MetisListKey pKey) {
64          theListKey = pKey;
65          theTouchMap = new LinkedHashMap<>();
66      }
67  
68      @Override
69      public MetisFieldSetDef getDataFieldSet() {
70          return FIELD_DEFS;
71      }
72  
73      @Override
74      public String formatObject(final OceanusDataFormatter pFormatter) {
75          return pFormatter.formatObject(theListKey);
76      }
77  
78      /**
79       * Obtain the listKey.
80       *
81       * @return the listKey
82       */
83      private MetisListKey getListKey() {
84          return theListKey;
85      }
86  
87      /**
88       * Obtain the touchedBy map.
89       *
90       * @return the map
91       */
92      private Map<Integer, PrometheusMapsItemTouch> getTouchMap() {
93          return theTouchMap;
94      }
95  
96      /**
97       * Record touchedBy.
98       *
99       * @param pTouchedItem  the item that is touched
100      * @param pTouchingItem the item that touches
101      */
102     void recordTouchedBy(final PrometheusDataItem pTouchedItem,
103                          final PrometheusDataItem pTouchingItem) {
104         /* Access correct touchedBy map */
105         final PrometheusMapsItemTouch myMap = theTouchMap.computeIfAbsent(pTouchedItem.getIndexedId(),
106                 i -> new PrometheusMapsItemTouch(pTouchedItem));
107         myMap.touchedByItem(pTouchingItem);
108     }
109 
110 
111     /**
112      * Record touches.
113      *
114      * @param pTouchedItem  the item that is touched
115      * @param pTouchingItem the item that touches
116      */
117     void recordTouches(final PrometheusDataItem pTouchedItem,
118                        final PrometheusDataItem pTouchingItem) {
119         /* Access correct touches map */
120         final PrometheusMapsItemTouch myMap = theTouchMap.computeIfAbsent(pTouchingItem.getIndexedId(),
121                 i -> new PrometheusMapsItemTouch(pTouchingItem));
122         myMap.touchesItem(pTouchedItem);
123     }
124 
125     /**
126      * Is the item touched?
127      *
128      * @param pItem the item
129      * @return true/false
130      */
131     boolean isTouched(final PrometheusDataItem pItem) {
132         final PrometheusMapsItemTouch myMap = theTouchMap.get(pItem.getIndexedId());
133         return myMap != null && myMap.isTouched();
134     }
135 }