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.metis.data.MetisDataItem.MetisDataFieldId;
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.oceanus.format.OceanusDataFormatter;
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, PrometheusMapsListHolder {
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      @Override
95      public PrometheusMapsListInstance getList(final MetisListKey pKey) {
96          return theMap.get(pKey);
97      }
98  
99      /**
100      * Declare fieldId map.
101      *
102      * @param pListKey the listKey
103      * @param pFieldId the fieldId
104      */
105     void declareFieldIdMap(final MetisListKey pListKey,
106                            final MetisDataFieldId pFieldId) {
107         final PrometheusMapsListInstance myMap = theMap.computeIfAbsent(pListKey, PrometheusMapsListInstance::new);
108         myMap.declareFieldIdMap(pFieldId);
109     }
110 
111     /**
112      * Declare fieldId map.
113      *
114      * @param pListKey the listKey
115      * @param pFieldId the fieldId
116      * @param pFilter  the filter
117      */
118     void declareFieldIdMap(final MetisListKey pListKey,
119                            final MetisDataFieldId pFieldId,
120                            final Function<PrometheusDataItem, Boolean> pFilter) {
121         final PrometheusMapsListInstance myMap = theMap.computeIfAbsent(pListKey, PrometheusMapsListInstance::new);
122         myMap.declareFieldIdMap(pFieldId, pFilter);
123     }
124 
125     /**
126      * Declare shared fieldId map.
127      *
128      * @param pListKey   the listKey
129      * @param pFieldId   the fieldId
130      * @param pSharedKey the shared listKey
131      */
132     void declareFieldIdMap(final MetisListKey pListKey,
133                            final MetisDataFieldId pFieldId,
134                            final MetisListKey pSharedKey) {
135         final PrometheusMapsListInstance myMap = theMap.computeIfAbsent(pListKey, PrometheusMapsListInstance::new);
136         final PrometheusMapsListInstance mySharedMap = theMap.get(pSharedKey);
137         myMap.declareFieldIdMap(pFieldId, mySharedMap);
138     }
139 
140     /**
141      * Declare dateId map.
142      *
143      * @param pListKey   the listKey
144      * @param pOwnerId   the ownerId
145      * @param pDateId    the dateId
146      * @param pAllowNull do we allow null value?
147      */
148     void declareDateIdMap(final MetisListKey pListKey,
149                           final MetisDataFieldId pOwnerId,
150                           final MetisDataFieldId pDateId,
151                           final boolean pAllowNull) {
152         final PrometheusMapsListInstance myMap = theMap.computeIfAbsent(pListKey, PrometheusMapsListInstance::new);
153         myMap.declareDateIdMap(pOwnerId, pDateId, pAllowNull);
154     }
155 
156     /**
157      * add item to map.
158      *
159      * @param pItem the item
160      */
161     void addItemToMaps(final PrometheusDataItem pItem) {
162         final PrometheusMapsListInstance myMap = theMap.get(pItem.getItemType());
163         if (myMap != null) {
164             myMap.addItemToMaps(pItem);
165         }
166     }
167 
168     /**
169      * Is the key duplicate?
170      *
171      * @param pFieldId the fieldId
172      * @param pItem    the item
173      * @return true/false
174      */
175     boolean isKeyDuplicate(final MetisDataFieldId pFieldId,
176                            final PrometheusDataItem pItem) {
177         final PrometheusMapsListInstance myMap = theMap.get(pItem.getItemType());
178         return myMap != null && myMap.isKeyDuplicate(pFieldId, pItem);
179     }
180 
181     /**
182      * Is the key available?
183      *
184      * @param pListKey the listKey
185      * @param pFieldId the fieldId
186      * @param pKey     the key
187      * @return true/false
188      */
189     boolean isKeyAvailable(final MetisListKey pListKey,
190                            final MetisDataFieldId pFieldId,
191                            final Object pKey) {
192         final PrometheusMapsListInstance myMap = theMap.get(pListKey);
193         return myMap == null || myMap.isKeyAvailable(pFieldId, pKey);
194     }
195 
196     /**
197      * Obtain the item for the key.
198      *
199      * @param pListKey the listKey
200      * @param pFieldId the fieldId
201      * @param pKey     the key
202      * @return the item
203      */
204     PrometheusDataItem getItemForKey(final MetisListKey pListKey,
205                                      final MetisDataFieldId pFieldId,
206                                      final Object pKey) {
207         final PrometheusMapsListInstance myMap = theMap.get(pListKey);
208         return myMap == null ? null : myMap.getItemForKey(pFieldId, pKey);
209     }
210 
211     /**
212      * Reset Maps.
213      */
214     void resetMaps() {
215         /* Reset each map */
216         for (PrometheusMapsListInstance myMap : theMap.values()) {
217             myMap.resetMaps();
218         }
219     }
220 }