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.prometheus.data.PrometheusDataItem;
23  
24  import java.util.LinkedHashMap;
25  import java.util.Map;
26  
27  /**
28   * DataTouchMap for Item.
29   */
30  public class PrometheusMapsItemTouch
31          implements MetisFieldItem {
32      /**
33       * Report fields.
34       */
35      private static final MetisFieldSet<PrometheusMapsItemTouch> FIELD_DEFS
36              = MetisFieldSet.newFieldSet(PrometheusMapsItemTouch.class);
37  
38      /*
39       * Declare Fields.
40       */
41      static {
42          FIELD_DEFS.declareLocalField(PrometheusMapsResource.MAPS_ITEM, PrometheusMapsItemTouch::getItem);
43          FIELD_DEFS.declareLocalField(PrometheusMapsResource.TOUCH_TOUCHEDBY, PrometheusMapsItemTouch::getTouchedByMap);
44          FIELD_DEFS.declareLocalField(PrometheusMapsResource.TOUCH_TOUCHES, PrometheusMapsItemTouch::getTouchesMap);
45      }
46  
47      /**
48       * Item itself.
49       */
50      private final PrometheusDataItem theItem;
51  
52      /**
53       * The map of items that touch this item.
54       */
55      private final Map<PrometheusMapsItemId, PrometheusDataItem> theTouchedBy;
56  
57      /**
58       * The map of items that this item touches.
59       */
60      private final Map<PrometheusMapsItemId, PrometheusDataItem> theTouches;
61  
62      /**
63       * Constructor.
64       *
65       * @param pItem the item
66       */
67      PrometheusMapsItemTouch(final PrometheusDataItem pItem) {
68          theItem = pItem;
69          theTouchedBy = new LinkedHashMap<>();
70          theTouches = new LinkedHashMap<>();
71      }
72  
73      @Override
74      public MetisFieldSetDef getDataFieldSet() {
75          return FIELD_DEFS;
76      }
77  
78      @Override
79      public String formatObject(final OceanusDataFormatter pFormatter) {
80          return theItem.formatObject(pFormatter);
81      }
82  
83      /**
84       * Obtain the item.
85       *
86       * @return the item
87       */
88      private PrometheusDataItem getItem() {
89          return theItem;
90      }
91  
92      /**
93       * Obtain the touchedBy map.
94       *
95       * @return the map
96       */
97      private Map<PrometheusMapsItemId, PrometheusDataItem> getTouchedByMap() {
98          return theTouchedBy;
99      }
100 
101     /**
102      * Obtain the touches map.
103      *
104      * @return the map
105      */
106     private Map<PrometheusMapsItemId, PrometheusDataItem> getTouchesMap() {
107         return theTouches;
108     }
109 
110     /**
111      * Register touchedBy item.
112      *
113      * @param pItem the item that touches this item
114      */
115     void touchedByItem(final PrometheusDataItem pItem) {
116         final PrometheusMapsItemId myId = new PrometheusMapsItemId(pItem);
117         theTouchedBy.put(myId, pItem);
118     }
119 
120     /**
121      * Register touches item.
122      *
123      * @param pItem the item that is touched by this item
124      */
125     void touchesItem(final PrometheusDataItem pItem) {
126         final PrometheusMapsItemId myId = new PrometheusMapsItemId(pItem);
127         theTouches.put(myId, pItem);
128     }
129 
130     /**
131      * Is the item touched?
132      *
133      * @return true/false
134      */
135     boolean isTouched() {
136         return !theTouchedBy.isEmpty();
137     }
138 }