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.data;
18  
19  import io.github.tonywasher.joceanus.oceanus.format.OceanusDataFormatter;
20  import io.github.tonywasher.joceanus.metis.data.MetisDataItem.MetisDataMap;
21  import io.github.tonywasher.joceanus.metis.data.MetisDataItem.MetisDataObjectFormat;
22  import io.github.tonywasher.joceanus.metis.list.MetisListKey;
23  import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataTouch.PrometheusTouchCounter;
24  
25  import java.util.HashMap;
26  import java.util.Iterator;
27  import java.util.Map;
28  
29  /**
30   * Class to record reference to a DataItem via another data item.
31   */
32  public class PrometheusDataTouch
33          implements MetisDataObjectFormat, MetisDataMap<MetisListKey, PrometheusTouchCounter> {
34      /**
35       * Map of touches.
36       */
37      private final Map<MetisListKey, PrometheusTouchCounter> theTouchMap;
38  
39      /**
40       * Constructor.
41       */
42      public PrometheusDataTouch() {
43          /* Create the map */
44          theTouchMap = new HashMap<>();
45      }
46  
47      @Override
48      public Map<MetisListKey, PrometheusTouchCounter> getUnderlyingMap() {
49          return theTouchMap;
50      }
51  
52      @Override
53      public String formatObject(final OceanusDataFormatter pFormatter) {
54          return getClass().getSimpleName();
55      }
56  
57      /**
58       * Constructor.
59       *
60       * @param pSource the source map
61       */
62      protected void copyMap(final PrometheusDataTouch pSource) {
63          /* Create the map */
64          theTouchMap.putAll(pSource.getUnderlyingMap());
65      }
66  
67      /**
68       * Reset all touches.
69       */
70      public void resetTouches() {
71          /* Clear the map */
72          theTouchMap.clear();
73      }
74  
75      /**
76       * Reset touches for a dataType.
77       *
78       * @param pItemType the ItemType
79       */
80      public void resetTouches(final MetisListKey pItemType) {
81          theTouchMap.remove(pItemType);
82      }
83  
84      /**
85       * Touch an item.
86       *
87       * @param pItemType the item type
88       */
89      public void touchItem(final MetisListKey pItemType) {
90          /* Access the record for the item type */
91          final PrometheusTouchCounter myCounter = getCounter(pItemType);
92  
93          /* If this is a new dataType */
94          if (myCounter == null) {
95              /* Store a new counter */
96              theTouchMap.put(pItemType, new PrometheusTouchCounter(pItemType));
97  
98              /* else just record the touch */
99          } else {
100             myCounter.touch();
101         }
102     }
103 
104     /**
105      * Is the item touched by the ItemType?
106      *
107      * @param pItemType the item type
108      * @return true/false
109      */
110     public boolean touchedBy(final MetisListKey pItemType) {
111         /* Access the record for the item type */
112         final PrometheusTouchCounter myCounter = getCounter(pItemType);
113 
114         /* If this is a new dataType */
115         return myCounter != null;
116     }
117 
118     /**
119      * Is the item active.
120      *
121      * @return true/false
122      */
123     public boolean isActive() {
124         return !isEmpty();
125     }
126 
127     /**
128      * Obtain item count.
129      *
130      * @param pItemType the item type
131      * @return the counter (or null)
132      */
133     public PrometheusTouchCounter getCounter(final MetisListKey pItemType) {
134         return theTouchMap.get(pItemType);
135     }
136 
137     /**
138      * Obtain iterator.
139      *
140      * @return the iterator
141      */
142     public Iterator<PrometheusTouchCounter> iterator() {
143         return theTouchMap.values().iterator();
144     }
145 
146     /**
147      * Simple counter.
148      */
149     public static final class PrometheusTouchCounter
150             implements MetisDataObjectFormat {
151         /**
152          * The item type.
153          */
154         private final MetisListKey theItemType;
155 
156         /**
157          * The number of touches.
158          */
159         private int theTouches;
160 
161         /**
162          * Constructor.
163          *
164          * @param pItemType the item type
165          */
166         private PrometheusTouchCounter(final MetisListKey pItemType) {
167             theItemType = pItemType;
168             theTouches = 1;
169         }
170 
171         @Override
172         public String formatObject(final OceanusDataFormatter pFormatter) {
173             return Integer.toString(theTouches);
174         }
175 
176         /**
177          * Obtain the item type.
178          *
179          * @return the item type
180          */
181         public MetisListKey getItemType() {
182             return theItemType;
183         }
184 
185         /**
186          * Obtain the touch count.
187          *
188          * @return the touches
189          */
190         public int getTouches() {
191             return theTouches;
192         }
193 
194         /**
195          * Increment counter.
196          */
197         private void touch() {
198             theTouches++;
199         }
200     }
201 }