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.date.OceanusDate;
20  import io.github.tonywasher.joceanus.oceanus.format.OceanusDataFormatter;
21  import io.github.tonywasher.joceanus.metis.data.MetisDataItem.MetisDataFieldId;
22  import io.github.tonywasher.joceanus.metis.field.MetisFieldItem;
23  import io.github.tonywasher.joceanus.metis.field.MetisFieldSet;
24  import io.github.tonywasher.joceanus.metis.list.MetisListKey;
25  import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataItem;
26  import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataResource;
27  
28  import java.util.LinkedHashMap;
29  import java.util.Map;
30  
31  /**
32   * Instance maps for dates.
33   */
34  public class PrometheusMapsDateInstance
35          implements MetisFieldItem {
36      /**
37       * Report fields.
38       */
39      private static final MetisFieldSet<PrometheusMapsDateInstance> FIELD_DEFS
40              = MetisFieldSet.newFieldSet(PrometheusMapsDateInstance.class);
41  
42      /*
43       * Declare Fields.
44       */
45      static {
46          FIELD_DEFS.declareLocalField(PrometheusDataResource.DATAITEM_TYPE, PrometheusMapsDateInstance::getListKey);
47          FIELD_DEFS.declareLocalField(PrometheusMapsResource.MAPS_FIELD, PrometheusMapsDateInstance::getOwnerId);
48          FIELD_DEFS.declareLocalField(PrometheusMapsResource.MAPS_INSTANCEMAP, PrometheusMapsDateInstance::getMap);
49      }
50  
51      /**
52       * listKey.
53       */
54      private final MetisListKey theListKey;
55  
56      /**
57       * The owner fieldId.
58       */
59      private final MetisDataFieldId theOwnerId;
60  
61      /**
62       * The date fieldId.
63       */
64      private final MetisDataFieldId theDateId;
65  
66      /**
67       * The map of owner to dateMap.
68       */
69      private final Map<PrometheusDataItem, PrometheusMapsFieldInstance> theMap;
70  
71      /**
72       * Do we allow null value?
73       */
74      private final boolean allowNull;
75  
76      /**
77       * Constructor.
78       *
79       * @param pListKey   the listKey
80       * @param pOwnerId   the ownerFieldId
81       * @param pDateId    the date fieldId
82       * @param pAllowNull do we allow null value?
83       */
84      PrometheusMapsDateInstance(final MetisListKey pListKey,
85                                 final MetisDataFieldId pOwnerId,
86                                 final MetisDataFieldId pDateId,
87                                 final boolean pAllowNull) {
88          theListKey = pListKey;
89          theOwnerId = pOwnerId;
90          theDateId = pDateId;
91          allowNull = pAllowNull;
92          theMap = new LinkedHashMap<>();
93      }
94  
95      /**
96       * Constructor.
97       *
98       * @param pSource the source map
99       */
100     PrometheusMapsDateInstance(final PrometheusMapsDateInstance pSource) {
101         theListKey = pSource.getListKey();
102         theOwnerId = pSource.getOwnerId();
103         theDateId = pSource.getDateId();
104         theMap = new LinkedHashMap<>();
105         allowNull = pSource.allowNull;
106     }
107 
108     @Override
109     public MetisFieldSetDef getDataFieldSet() {
110         return FIELD_DEFS;
111     }
112 
113     @Override
114     public String formatObject(final OceanusDataFormatter pFormatter) {
115         return PrometheusMapsDateInstance.class.getSimpleName();
116     }
117 
118     /**
119      * Obtain the listKey.
120      *
121      * @return the listKey
122      */
123     MetisListKey getListKey() {
124         return theListKey;
125     }
126 
127     /**
128      * Obtain the ownerId.
129      *
130      * @return the ownerId
131      */
132     MetisDataFieldId getOwnerId() {
133         return theOwnerId;
134     }
135 
136     /**
137      * Obtain the dateId.
138      *
139      * @return the dateId
140      */
141     MetisDataFieldId getDateId() {
142         return theDateId;
143     }
144 
145     /**
146      * Obtain the map.
147      *
148      * @return the fieldId
149      */
150     Map<PrometheusDataItem, PrometheusMapsFieldInstance> getMap() {
151         return theMap;
152     }
153 
154     /**
155      * add item to map.
156      *
157      * @param pItem the item
158      */
159     void addItemToMap(final PrometheusDataItem pItem) {
160         /* Access the owner */
161         final MetisFieldSetDef myFieldSet = pItem.getDataFieldSet();
162         final MetisFieldDef myField = myFieldSet.getField(theOwnerId);
163         final PrometheusDataItem myOwner = myField.getFieldValue(pItem, PrometheusDataItem.class);
164 
165         /* Add to the date map */
166         final PrometheusMapsFieldInstance myMap = theMap.computeIfAbsent(myOwner,
167                 r -> new PrometheusMapsFieldInstance(theListKey, theDateId, allowNull));
168         myMap.addItemToMap(pItem);
169     }
170 
171     /**
172      * Is the date duplicate?
173      *
174      * @param pItem the item
175      * @return true/false
176      */
177     boolean isDateDuplicate(final PrometheusDataItem pItem) {
178         /* Access the owner */
179         final MetisFieldSetDef myFieldSet = pItem.getDataFieldSet();
180         MetisFieldDef myField = myFieldSet.getField(theOwnerId);
181         final PrometheusDataItem myOwner = myField.getFieldValue(pItem, PrometheusDataItem.class);
182 
183         /* Check the map for duplicate */
184         final PrometheusMapsFieldInstance myMap = theMap.get(myOwner);
185         if (myMap != null) {
186             myField = myFieldSet.getField(theDateId);
187             final Object myDate = myField.getFieldValue(pItem);
188             return myMap.isKeyDuplicate(myDate);
189         }
190         return false;
191     }
192 
193     /**
194      * Is the date available?
195      *
196      * @param pOwner the owner
197      * @param pDate  the date
198      * @return true/false
199      */
200     boolean isDateAvailable(final PrometheusDataItem pOwner,
201                             final OceanusDate pDate) {
202         final PrometheusMapsFieldInstance myMap = theMap.get(pOwner);
203         return myMap == null || myMap.isKeyAvailable(pDate);
204     }
205 
206     /**
207      * Reset Maps.
208      */
209     void resetMap() {
210         theMap.clear();
211     }
212 }