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.validate;
18  
19  import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataItem;
20  import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataResource;
21  import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataValidator;
22  import io.github.tonywasher.joceanus.prometheus.data.PrometheusStaticDataItem;
23  import io.github.tonywasher.joceanus.prometheus.data.PrometheusStaticDataItem.PrometheusStaticDataMap;
24  import io.github.tonywasher.joceanus.prometheus.data.PrometheusStaticDataItem.PrometheusStaticList;
25  
26  /**
27   * Validator for static.
28   */
29  public class PrometheusValidateStatic
30          implements PrometheusDataValidator {
31      /**
32       * Constructor.
33       */
34      public PrometheusValidateStatic() {
35      }
36  
37      @Override
38      public void validate(final PrometheusDataItem pStatic) {
39          final PrometheusStaticDataItem myStatic = (PrometheusStaticDataItem) pStatic;
40          final PrometheusStaticList<?> myList = myStatic.getList();
41          final String myName = myStatic.getName();
42          final String myDesc = myStatic.getDesc();
43          final PrometheusStaticDataMap<?> myMap = myList.getDataMap();
44  
45          /* Name must be non-null */
46          if (myName == null) {
47              pStatic.addError(PrometheusDataItem.ERROR_MISSING, PrometheusDataResource.DATAITEM_FIELD_NAME);
48  
49              /* Else check the name */
50          } else {
51              /* The name must not be too long */
52              if (PrometheusDataItem.byteLength(myName) > PrometheusDataItem.NAMELEN) {
53                  pStatic.addError(PrometheusDataItem.ERROR_LENGTH, PrometheusDataResource.DATAITEM_FIELD_NAME);
54              }
55  
56              /* The name must only contain valid characters */
57              if (!PrometheusDataItem.validString(myName, null)) {
58                  pStatic.addError(PrometheusStaticDataItem.ERROR_BADNAME, PrometheusDataResource.DATAITEM_FIELD_NAME);
59              }
60  
61              /* Check that the name is unique */
62              if (!myMap.validNameCount(myName)) {
63                  pStatic.addError(PrometheusDataItem.ERROR_DUPLICATE, PrometheusDataResource.DATAITEM_FIELD_NAME);
64              }
65          }
66  
67          /* Check description length */
68          if (myDesc != null
69                  && PrometheusDataItem.byteLength(myDesc) > PrometheusDataItem.DESCLEN) {
70              pStatic.addError(PrometheusDataItem.ERROR_LENGTH, PrometheusDataResource.DATAITEM_FIELD_DESC);
71          }
72  
73          /* The order must not be negative */
74          if (myStatic.getOrder() < 0) {
75              pStatic.addError(PrometheusDataItem.ERROR_NEGATIVE, PrometheusDataResource.STATICDATA_SORT);
76          }
77  
78          /* Cannot have duplicate order */
79          if (!myMap.validOrderCount(myStatic.getOrder())) {
80              pStatic.addError(PrometheusDataItem.ERROR_DUPLICATE, PrometheusDataResource.STATICDATA_SORT);
81          }
82  
83          /* Set validation flag */
84          if (!pStatic.hasErrors()) {
85              pStatic.setValidEdit();
86          }
87      }
88  }