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.field.MetisFieldItem;
21  import io.github.tonywasher.joceanus.metis.field.MetisFieldSet;
22  import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataItem;
23  import io.github.tonywasher.joceanus.prometheus.maps.PrometheusMapsInstanceElement.PrometheusMapsInstanceElementItem;
24  import io.github.tonywasher.joceanus.prometheus.maps.PrometheusMapsInstanceElement.PrometheusMapsInstanceElementList;
25  
26  import java.util.LinkedHashMap;
27  import java.util.Map;
28  
29  /**
30   * InstanceMaps Base.
31   */
32  public abstract class PrometheusMapsBaseInstance
33          implements MetisFieldItem {
34      /**
35       * Report fields.
36       */
37      private static final MetisFieldSet<PrometheusMapsBaseInstance> FIELD_DEFS
38              = MetisFieldSet.newFieldSet(PrometheusMapsBaseInstance.class);
39  
40      /*
41       * Declare Fields.
42       */
43      static {
44          FIELD_DEFS.declareLocalField(PrometheusMapsResource.MAPS_INSTANCEMAP, PrometheusMapsBaseInstance::getMap);
45      }
46  
47      /**
48       * The map.
49       */
50      private final Map<Object, PrometheusMapsInstanceElement> theMap;
51  
52      /**
53       * Constructor.
54       */
55      PrometheusMapsBaseInstance() {
56          theMap = new LinkedHashMap<>();
57      }
58  
59      @Override
60      public MetisFieldSetDef getDataFieldSet() {
61          return FIELD_DEFS;
62      }
63  
64      @Override
65      public String formatObject(final OceanusDataFormatter pFormatter) {
66          return PrometheusMapsBaseInstance.class.getSimpleName();
67      }
68  
69      /**
70       * Obtain the map.
71       *
72       * @return the map
73       */
74      private Map<Object, PrometheusMapsInstanceElement> getMap() {
75          return theMap;
76      }
77  
78      /**
79       * add item to map.
80       *
81       * @param pKey  the key for the item
82       * @param pItem the item
83       */
84      void addItemToMap(final Object pKey,
85                        final PrometheusDataItem pItem) {
86          final PrometheusMapsInstanceElement myCurr = theMap.get(pKey);
87          if (myCurr != null) {
88              theMap.put(pKey, new PrometheusMapsInstanceElementList(myCurr, pItem));
89          } else {
90              theMap.put(pKey, new PrometheusMapsInstanceElementItem(pItem));
91          }
92      }
93  
94      /**
95       * Is the key duplicate?
96       *
97       * @param pKey the key
98       * @return true/false
99       */
100     boolean isKeyDuplicate(final Object pKey) {
101         return theMap.get(pKey) instanceof PrometheusMapsInstanceElementList;
102     }
103 
104     /**
105      * Is the key available?
106      *
107      * @param pKey the key
108      * @return true/false
109      */
110     boolean isKeyAvailable(final Object pKey) {
111         return theMap.get(pKey) == null;
112     }
113 
114     /**
115      * Obtain the item for the key.
116      *
117      * @param pKey the key
118      * @return the item
119      */
120     PrometheusDataItem getItemForKey(final Object pKey) {
121         final PrometheusMapsInstanceElement myElement = theMap.get(pKey);
122         if (myElement instanceof PrometheusMapsInstanceElementItem myItem) {
123             return myItem.getItem();
124         }
125         if (myElement instanceof PrometheusMapsInstanceElementList) {
126             return myElement.getList().get(0);
127         }
128         return null;
129     }
130 
131     /**
132      * Reset Maps.
133      */
134     void resetMap() {
135         theMap.clear();
136     }
137 }