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.data.MetisDataResource;
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  
27  import java.util.HashMap;
28  import java.util.Map;
29  import java.util.function.Function;
30  
31  /**
32   * DataTouchMap for data/EditSet.
33   */
34  public class PrometheusMapsInstanceCtl
35          implements MetisFieldItem {
36      /**
37       * Report fields.
38       */
39      private static final MetisFieldSet<PrometheusMapsInstanceCtl> FIELD_DEFS = MetisFieldSet.newFieldSet(PrometheusMapsInstanceCtl.class);
40  
41      /*
42       * Declare Fields.
43       */
44      static {
45          FIELD_DEFS.declareLocalField(MetisDataResource.DATA_VERSION, PrometheusMapsInstanceCtl::getVersion);
46          FIELD_DEFS.declareLocalField(PrometheusMapsResource.MAPS_INSTANCEMAP, PrometheusMapsInstanceCtl::getInstanceMap);
47      }
48  
49      /**
50       * The history.
51       */
52      private final Map<Integer, PrometheusMapsDataSetInstance> theHistory;
53  
54      /**
55       * The current version.
56       */
57      private Integer theVersion;
58  
59      /**
60       * The current dataSetInstanceMap.
61       */
62      private PrometheusMapsDataSetInstance theInstanceMap;
63  
64      /**
65       * Constructor.
66       */
67      PrometheusMapsInstanceCtl() {
68          theHistory = new HashMap<>();
69          theVersion = 0;
70          setVersion(0);
71      }
72  
73      @Override
74      public MetisFieldSetDef getDataFieldSet() {
75          return FIELD_DEFS;
76      }
77  
78      @Override
79      public String formatObject(final OceanusDataFormatter pFormatter) {
80          return PrometheusMapsInstanceCtl.class.getSimpleName();
81      }
82  
83      /**
84       * Obtain the version.
85       *
86       * @return the version
87       */
88      private Integer getVersion() {
89          return theVersion;
90      }
91  
92      /**
93       * Obtain the instance map.
94       *
95       * @return the map
96       */
97      private PrometheusMapsDataSetInstance getInstanceMap() {
98          return theInstanceMap;
99      }
100 
101     /**
102      * Set version.
103      *
104      * @param pVersion the version
105      */
106     void setVersion(final Integer pVersion) {
107         /* If we are rolling back */
108         if (pVersion < theVersion) {
109             /* Delete any redundant versions */
110             theHistory.entrySet().removeIf(myEntry -> myEntry.getKey() > pVersion);
111         }
112 
113         /* Obtain the map */
114         theInstanceMap = theInstanceMap == null
115                 ? new PrometheusMapsDataSetInstance()
116                 : theHistory.computeIfAbsent(pVersion, i -> new PrometheusMapsDataSetInstance(theInstanceMap));
117         theVersion = pVersion;
118     }
119 
120     /**
121      * Declare fieldId map.
122      *
123      * @param pListKey the listKey
124      * @param pFieldId the fieldId
125      */
126     void declareFieldIdMap(final MetisListKey pListKey,
127                            final MetisDataFieldId pFieldId) {
128         theInstanceMap.declareFieldIdMap(pListKey, pFieldId);
129     }
130 
131     /**
132      * Declare fieldId map.
133      *
134      * @param pListKey the listKey
135      * @param pFieldId the fieldId
136      * @param pFilter  the filter
137      */
138     void declareFieldIdMap(final MetisListKey pListKey,
139                            final MetisDataFieldId pFieldId,
140                            final Function<PrometheusDataItem, Boolean> pFilter) {
141         theInstanceMap.declareFieldIdMap(pListKey, pFieldId, pFilter);
142     }
143 
144     /**
145      * Declare shared fieldId map.
146      *
147      * @param pListKey   the listKey
148      * @param pFieldId   the fieldId
149      * @param pSharedKey the shared listKey
150      */
151     void declareFieldIdMap(final MetisListKey pListKey,
152                            final MetisDataFieldId pFieldId,
153                            final MetisListKey pSharedKey) {
154         theInstanceMap.declareFieldIdMap(pListKey, pFieldId, pSharedKey);
155     }
156 
157     /**
158      * Declare dateId map.
159      *
160      * @param pListKey   the listKey
161      * @param pOwnerId   the ownerId
162      * @param pDateId    the dateId
163      * @param pAllowNull do we allow null value?
164      */
165     void declareDateIdMap(final MetisListKey pListKey,
166                           final MetisDataFieldId pOwnerId,
167                           final MetisDataFieldId pDateId,
168                           final boolean pAllowNull) {
169         theInstanceMap.declareDateIdMap(pListKey, pOwnerId, pDateId, pAllowNull);
170     }
171 
172     /**
173      * add item to map.
174      *
175      * @param pItem the item
176      */
177     void addItemToMaps(final PrometheusDataItem pItem) {
178         theInstanceMap.addItemToMaps(pItem);
179     }
180 
181     /**
182      * Is the key duplicate?
183      *
184      * @param pFieldId the fieldId
185      * @param pItem    the item
186      * @return true/false
187      */
188     boolean isKeyDuplicate(final MetisDataFieldId pFieldId,
189                            final PrometheusDataItem pItem) {
190         return theInstanceMap.isKeyDuplicate(pFieldId, pItem);
191     }
192 
193     /**
194      * Is the key available?
195      *
196      * @param pListKey the listKey
197      * @param pFieldId the fieldId
198      * @param pKey     the key
199      * @return true/false
200      */
201     boolean isKeyAvailable(final MetisListKey pListKey,
202                            final MetisDataFieldId pFieldId,
203                            final Object pKey) {
204         return theInstanceMap.isKeyAvailable(pListKey, pFieldId, pKey);
205     }
206 
207     /**
208      * Obtain the item for the key.
209      *
210      * @param pListKey the listKey
211      * @param pFieldId the fieldId
212      * @param pKey     the key
213      * @return the item
214      */
215     PrometheusDataItem getItemForKey(final MetisListKey pListKey,
216                                      final MetisDataFieldId pFieldId,
217                                      final Object pKey) {
218         return theInstanceMap.getItemForKey(pListKey, pFieldId, pKey);
219     }
220 
221     /**
222      * Reset the maps.
223      */
224     void resetMaps() {
225         theInstanceMap.resetMaps();
226     }
227 }