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.MetisFieldSet;
22  import io.github.tonywasher.joceanus.metis.list.MetisListKey;
23  import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataItem;
24  import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataResource;
25  
26  import java.util.function.Function;
27  
28  /**
29   * InstanceMaps for Field.
30   */
31  public class PrometheusMapsFieldInstance
32          extends PrometheusMapsBaseInstance {
33      /**
34       * Report fields.
35       */
36      private static final MetisFieldSet<PrometheusMapsFieldInstance> FIELD_DEFS
37              = MetisFieldSet.newFieldSet(PrometheusMapsFieldInstance.class);
38  
39      /*
40       * Declare Fields.
41       */
42      static {
43          FIELD_DEFS.declareLocalField(PrometheusDataResource.DATAITEM_TYPE, PrometheusMapsFieldInstance::getListKey);
44          FIELD_DEFS.declareLocalField(PrometheusMapsResource.MAPS_FIELD, PrometheusMapsFieldInstance::getFieldId);
45      }
46  
47      /**
48       * listKey.
49       */
50      private final MetisListKey theListKey;
51  
52      /**
53       * The fieldId.
54       */
55      private final MetisDataFieldId theFieldId;
56  
57      /**
58       * The filter.
59       */
60      private final Function<PrometheusDataItem, Boolean> theFilter;
61  
62      /**
63       * Do we allow null value?
64       */
65      private final boolean allowNull;
66  
67      /**
68       * Constructor.
69       *
70       * @param pKey     the listKey
71       * @param pFieldId the fieldId
72       */
73      PrometheusMapsFieldInstance(final MetisListKey pKey,
74                                  final MetisDataFieldId pFieldId) {
75          this(pKey, pFieldId, i -> true);
76      }
77  
78      /**
79       * Constructor.
80       *
81       * @param pKey       the listKey
82       * @param pFieldId   the fieldId
83       * @param pAllowNull do we allow null value?
84       */
85      PrometheusMapsFieldInstance(final MetisListKey pKey,
86                                  final MetisDataFieldId pFieldId,
87                                  final boolean pAllowNull) {
88          this(pKey, pFieldId, i -> true, pAllowNull);
89      }
90  
91      /**
92       * Constructor.
93       *
94       * @param pKey     the listKey
95       * @param pFieldId the fieldId
96       * @param pFilter  the filter
97       */
98      PrometheusMapsFieldInstance(final MetisListKey pKey,
99                                  final MetisDataFieldId pFieldId,
100                                 final Function<PrometheusDataItem, Boolean> pFilter) {
101         this(pKey, pFieldId, pFilter, false);
102     }
103 
104     /**
105      * Constructor.
106      *
107      * @param pKey       the listKey
108      * @param pFieldId   the fieldId
109      * @param pFilter    the filter
110      * @param pAllowNull do we allow null value?
111      */
112     PrometheusMapsFieldInstance(final MetisListKey pKey,
113                                 final MetisDataFieldId pFieldId,
114                                 final Function<PrometheusDataItem, Boolean> pFilter,
115                                 final boolean pAllowNull) {
116         theListKey = pKey;
117         theFieldId = pFieldId;
118         theFilter = pFilter;
119         allowNull = pAllowNull;
120     }
121 
122     /**
123      * Constructor.
124      *
125      * @param pSource the source fieldMap
126      */
127     PrometheusMapsFieldInstance(final PrometheusMapsFieldInstance pSource) {
128         theListKey = pSource.getListKey();
129         theFieldId = pSource.getFieldId();
130         theFilter = pSource.theFilter;
131         allowNull = pSource.allowNull;
132     }
133 
134     @Override
135     public MetisFieldSetDef getDataFieldSet() {
136         return FIELD_DEFS;
137     }
138 
139     @Override
140     public String formatObject(final OceanusDataFormatter pFormatter) {
141         return PrometheusMapsFieldInstance.class.getSimpleName();
142     }
143 
144     /**
145      * Obtain the listKey.
146      *
147      * @return the listKey
148      */
149     MetisListKey getListKey() {
150         return theListKey;
151     }
152 
153     /**
154      * Obtain the fieldId.
155      *
156      * @return the fieldId
157      */
158     MetisDataFieldId getFieldId() {
159         return theFieldId;
160     }
161 
162     /**
163      * add item to map.
164      *
165      * @param pItem the item
166      */
167     void addItemToMap(final PrometheusDataItem pItem) {
168         if (theFilter.apply(pItem)) {
169             final MetisFieldSetDef myFieldSet = pItem.getDataFieldSet();
170             final MetisFieldDef myField = myFieldSet.getField(theFieldId);
171             final Object myValue = myField.getFieldValue(pItem);
172             if (allowNull || myValue != null) {
173                 addItemToMap(myValue, pItem);
174             }
175         }
176     }
177 
178     /**
179      * Is the key duplicate?
180      *
181      * @param pItem the item
182      * @return true/false
183      */
184     boolean isKeyDuplicate(final PrometheusDataItem pItem) {
185         if (theFilter.apply(pItem)) {
186             final MetisFieldSetDef myFieldSet = pItem.getDataFieldSet();
187             final MetisFieldDef myField = myFieldSet.getField(theFieldId);
188             final Object myValue = myField.getFieldValue(pItem);
189             return isKeyDuplicate(myValue);
190         }
191         return false;
192     }
193 
194     /**
195      * Find item in map.
196      *
197      * @param pKey the key
198      * @return the item
199      */
200     public PrometheusDataItem findItemInMap(final Object pKey) {
201         return getItemForKey(pKey);
202     }
203 }