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.MetisDataItem.MetisDataFieldId;
21  import io.github.tonywasher.joceanus.metis.field.MetisFieldItem;
22  import io.github.tonywasher.joceanus.metis.field.MetisFieldSet;
23  import io.github.tonywasher.joceanus.metis.list.MetisListKey;
24  import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataItem;
25  
26  import java.util.LinkedHashMap;
27  import java.util.Map;
28  import java.util.function.Function;
29  
30  /**
31   * InstanceMaps for DataSet.
32   */
33  public class PrometheusMapsDataSetInstance
34          implements MetisFieldItem {
35      /**
36       * Report fields.
37       */
38      private static final MetisFieldSet<PrometheusMapsDataSetInstance> FIELD_DEFS
39              = MetisFieldSet.newFieldSet(PrometheusMapsDataSetInstance.class);
40  
41      /*
42       * Declare Fields.
43       */
44      static {
45          FIELD_DEFS.declareLocalField(PrometheusMapsResource.MAPS_INSTANCEMAP, PrometheusMapsDataSetInstance::getMap);
46      }
47  
48      /**
49       * The item.
50       */
51      private final Map<MetisListKey, PrometheusMapsListInstance> theMap;
52  
53      /**
54       * Constructor.
55       */
56      PrometheusMapsDataSetInstance() {
57          theMap = new LinkedHashMap<>();
58      }
59  
60      /**
61       * Constructor.
62       *
63       * @param pSource the source dataset map
64       */
65      PrometheusMapsDataSetInstance(final PrometheusMapsDataSetInstance pSource) {
66          this();
67  
68          /* Recreate underlying maps */
69          for (PrometheusMapsListInstance myMap : pSource.getMap().values()) {
70              /* Create a new ListMap */
71              theMap.put(myMap.getListKey(), new PrometheusMapsListInstance(this, myMap));
72          }
73      }
74  
75      @Override
76      public MetisFieldSetDef getDataFieldSet() {
77          return FIELD_DEFS;
78      }
79  
80      @Override
81      public String formatObject(final OceanusDataFormatter pFormatter) {
82          return PrometheusMapsDataSetInstance.class.getSimpleName();
83      }
84  
85      /**
86       * Obtain the map.
87       *
88       * @return the map
89       */
90      private Map<MetisListKey, PrometheusMapsListInstance> getMap() {
91          return theMap;
92      }
93  
94      /**
95       * Obtain the list map for listKey.
96       *
97       * @param pKey the listKey
98       * @return the map
99       */
100     PrometheusMapsListInstance getList(final MetisListKey pKey) {
101         return theMap.get(pKey);
102     }
103 
104     /**
105      * Declare fieldId map.
106      *
107      * @param pListKey the listKey
108      * @param pFieldId the fieldId
109      */
110     void declareFieldIdMap(final MetisListKey pListKey,
111                            final MetisDataFieldId pFieldId) {
112         final PrometheusMapsListInstance myMap = theMap.computeIfAbsent(pListKey, PrometheusMapsListInstance::new);
113         myMap.declareFieldIdMap(pFieldId);
114     }
115 
116     /**
117      * Declare fieldId map.
118      *
119      * @param pListKey the listKey
120      * @param pFieldId the fieldId
121      * @param pFilter  the filter
122      */
123     void declareFieldIdMap(final MetisListKey pListKey,
124                            final MetisDataFieldId pFieldId,
125                            final Function<PrometheusDataItem, Boolean> pFilter) {
126         final PrometheusMapsListInstance myMap = theMap.computeIfAbsent(pListKey, PrometheusMapsListInstance::new);
127         myMap.declareFieldIdMap(pFieldId, pFilter);
128     }
129 
130     /**
131      * Declare shared fieldId map.
132      *
133      * @param pListKey   the listKey
134      * @param pFieldId   the fieldId
135      * @param pSharedKey the shared listKey
136      */
137     void declareFieldIdMap(final MetisListKey pListKey,
138                            final MetisDataFieldId pFieldId,
139                            final MetisListKey pSharedKey) {
140         final PrometheusMapsListInstance myMap = theMap.computeIfAbsent(pListKey, PrometheusMapsListInstance::new);
141         final PrometheusMapsListInstance mySharedMap = theMap.get(pSharedKey);
142         myMap.declareFieldIdMap(pFieldId, mySharedMap);
143     }
144 
145     /**
146      * Declare dateId map.
147      *
148      * @param pListKey   the listKey
149      * @param pOwnerId   the ownerId
150      * @param pDateId    the dateId
151      * @param pAllowNull do we allow null value?
152      */
153     void declareDateIdMap(final MetisListKey pListKey,
154                           final MetisDataFieldId pOwnerId,
155                           final MetisDataFieldId pDateId,
156                           final boolean pAllowNull) {
157         final PrometheusMapsListInstance myMap = theMap.computeIfAbsent(pListKey, PrometheusMapsListInstance::new);
158         myMap.declareDateIdMap(pOwnerId, pDateId, pAllowNull);
159     }
160 
161     /**
162      * add item to map.
163      *
164      * @param pItem the item
165      */
166     void addItemToMaps(final PrometheusDataItem pItem) {
167         final PrometheusMapsListInstance myMap = theMap.get(pItem.getItemType());
168         if (myMap != null) {
169             myMap.addItemToMaps(pItem);
170         }
171     }
172 
173     /**
174      * Is the key duplicate?
175      *
176      * @param pFieldId the fieldId
177      * @param pItem    the item
178      * @return true/false
179      */
180     boolean isKeyDuplicate(final MetisDataFieldId pFieldId,
181                            final PrometheusDataItem pItem) {
182         final PrometheusMapsListInstance myMap = theMap.get(pItem.getItemType());
183         return myMap != null && myMap.isKeyDuplicate(pFieldId, pItem);
184     }
185 
186     /**
187      * Is the key available?
188      *
189      * @param pListKey the listKey
190      * @param pFieldId the fieldId
191      * @param pKey     the key
192      * @return true/false
193      */
194     boolean isKeyAvailable(final MetisListKey pListKey,
195                            final MetisDataFieldId pFieldId,
196                            final Object pKey) {
197         final PrometheusMapsListInstance myMap = theMap.get(pListKey);
198         return myMap == null || myMap.isKeyAvailable(pFieldId, pKey);
199     }
200 
201     /**
202      * Obtain the item for the key.
203      *
204      * @param pListKey the listKey
205      * @param pFieldId the fieldId
206      * @param pKey     the key
207      * @return the item
208      */
209     PrometheusDataItem getItemForKey(final MetisListKey pListKey,
210                                      final MetisDataFieldId pFieldId,
211                                      final Object pKey) {
212         final PrometheusMapsListInstance myMap = theMap.get(pListKey);
213         return myMap == null ? null : myMap.getItemForKey(pFieldId, pKey);
214     }
215 
216     /**
217      * Reset Maps.
218      */
219     void resetMaps() {
220         /* Reset each map */
221         for (PrometheusMapsListInstance myMap : theMap.values()) {
222             myMap.resetMaps();
223         }
224     }
225 }