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