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  
25  import java.util.LinkedHashMap;
26  import java.util.Map;
27  
28  /**
29   * DataTouchMap for dataSet/editSet.
30   */
31  public class PrometheusMapsDataSetTouch
32          implements MetisFieldItem {
33      /**
34       * Report fields.
35       */
36      private static final MetisFieldSet<PrometheusMapsDataSetTouch> FIELD_DEFS = MetisFieldSet.newFieldSet(PrometheusMapsDataSetTouch.class);
37  
38      /*
39       * Declare Fields.
40       */
41      static {
42          FIELD_DEFS.declareLocalField(PrometheusMapsResource.MAPS_TOUCHMAP, PrometheusMapsDataSetTouch::getTouchMap);
43      }
44  
45      /**
46       * The map of listKey to listMap.
47       */
48      private final Map<MetisListKey, PrometheusMapsListTouch> theListMap;
49  
50      /**
51       * Constructor.
52       */
53      PrometheusMapsDataSetTouch() {
54          theListMap = new LinkedHashMap<>();
55      }
56  
57      @Override
58      public MetisFieldSetDef getDataFieldSet() {
59          return FIELD_DEFS;
60      }
61  
62      @Override
63      public String formatObject(final OceanusDataFormatter pFormatter) {
64          return PrometheusMapsDataSetTouch.class.getSimpleName();
65      }
66  
67      /**
68       * Obtain the list map.
69       *
70       * @return the map
71       */
72      private Map<MetisListKey, PrometheusMapsListTouch> getTouchMap() {
73          return theListMap;
74      }
75  
76      /**
77       * Record touch.
78       *
79       * @param pTouchedItem  the item that is touched
80       * @param pTouchingItem the item that touches
81       */
82      void recordTouch(final PrometheusDataItem pTouchedItem,
83                       final PrometheusDataItem pTouchingItem) {
84          /* Access correct map and record the touch */
85          MetisListKey myKey = pTouchedItem.getItemType();
86          PrometheusMapsListTouch myMap = theListMap.computeIfAbsent(myKey,
87                  PrometheusMapsListTouch::new);
88          myMap.recordTouchedBy(pTouchedItem, pTouchingItem);
89  
90          /* Access correct map and record the touch */
91          myKey = pTouchingItem.getItemType();
92          myMap = theListMap.computeIfAbsent(myKey,
93                  PrometheusMapsListTouch::new);
94          myMap.recordTouches(pTouchedItem, pTouchingItem);
95      }
96  
97      /**
98       * Is the item touched?
99       *
100      * @param pItem the item
101      * @return true/false
102      */
103     boolean isTouched(final PrometheusDataItem pItem) {
104         final PrometheusMapsListTouch myMap = theListMap.get(pItem.getItemType());
105         return myMap != null && myMap.isTouched(pItem);
106     }
107 
108     /**
109      * Reset the map.
110      */
111     void resetMap() {
112         theListMap.clear();
113     }
114 }