View Javadoc
1   /*
2    * MoneyWise: Finance Application
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.moneywise.data.validate;
18  
19  import io.github.tonywasher.joceanus.oceanus.date.OceanusDate;
20  import io.github.tonywasher.joceanus.oceanus.date.OceanusDateRange;
21  import io.github.tonywasher.joceanus.oceanus.decimal.OceanusRatio;
22  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseBasicResource;
23  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseExchangeRate;
24  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseExchangeRate.MoneyWiseExchangeRateBaseList;
25  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseExchangeRate.MoneyWiseExchangeRateDataMap;
26  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseCurrency;
27  import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataItem;
28  import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataValidator;
29  
30  /**
31   * Validator for region.
32   */
33  public class MoneyWiseValidateXchangeRate
34          implements PrometheusDataValidator {
35  
36      @Override
37      public void validate(final PrometheusDataItem pRate) {
38          final MoneyWiseExchangeRate myRate = (MoneyWiseExchangeRate) pRate;
39          final MoneyWiseExchangeRateBaseList<? extends MoneyWiseExchangeRate> myList = myRate.getList();
40          final MoneyWiseCurrency myFrom = myRate.getFromCurrency();
41          final MoneyWiseCurrency myTo = myRate.getToCurrency();
42          final OceanusDate myDate = myRate.getDate();
43          final OceanusRatio myXchgRate = myRate.getExchangeRate();
44          final OceanusDateRange myRange = myRate.getDataSet().getDateRange();
45  
46          /* Date must be non-null */
47          if (myDate == null) {
48              pRate.addError(PrometheusDataItem.ERROR_MISSING, MoneyWiseBasicResource.MONEYWISEDATA_FIELD_DATE);
49  
50              /* else date is non-null */
51          } else {
52              /* Date must be unique for this currency */
53              final MoneyWiseExchangeRateDataMap myMap = myList.getDataMap();
54              if (!myMap.validRateCount(myRate)) {
55                  pRate.addError(PrometheusDataItem.ERROR_DUPLICATE, MoneyWiseBasicResource.MONEYWISEDATA_FIELD_DATE);
56              }
57  
58              /* The date must be in-range */
59              if (myRange.compareToDate(myDate) != 0) {
60                  pRate.addError(PrometheusDataItem.ERROR_RANGE, MoneyWiseBasicResource.MONEYWISEDATA_FIELD_DATE);
61              }
62          }
63  
64          /* FromCurrency must be non-null and enabled */
65          if (myFrom == null) {
66              pRate.addError(PrometheusDataItem.ERROR_MISSING, MoneyWiseBasicResource.XCHGRATE_FROM);
67          } else if (!myFrom.getEnabled()) {
68              pRate.addError(PrometheusDataItem.ERROR_DISABLED, MoneyWiseBasicResource.XCHGRATE_FROM);
69          }
70  
71          /* ToCurrency must be non-null and enabled */
72          if (myTo == null) {
73              pRate.addError(PrometheusDataItem.ERROR_MISSING, MoneyWiseBasicResource.XCHGRATE_TO);
74          } else if (!myTo.getEnabled()) {
75              pRate.addError(PrometheusDataItem.ERROR_DISABLED, MoneyWiseBasicResource.XCHGRATE_TO);
76          }
77  
78          /* Check currency combination */
79          if (myFrom != null && myTo != null) {
80              /* Must be different */
81              if (myFrom.equals(myTo)) {
82                  pRate.addError(MoneyWiseExchangeRate.ERROR_CIRCLE, MoneyWiseBasicResource.XCHGRATE_TO);
83              }
84  
85              /* From currency must be the reporting currency */
86              final MoneyWiseCurrency myDefault = myRate.getDataSet().getReportingCurrency();
87              if (!myFrom.equals(myDefault)) {
88                  pRate.addError(MoneyWiseExchangeRate.ERROR_DEF, MoneyWiseBasicResource.XCHGRATE_FROM);
89              }
90          }
91  
92          /* Rate must be non-null and positive non-zero */
93          if (myXchgRate == null) {
94              pRate.addError(PrometheusDataItem.ERROR_MISSING, MoneyWiseBasicResource.XCHGRATE_RATE);
95          } else if (!myXchgRate.isNonZero()) {
96              pRate.addError(PrometheusDataItem.ERROR_ZERO, MoneyWiseBasicResource.XCHGRATE_RATE);
97          } else if (!myXchgRate.isPositive()) {
98              pRate.addError(PrometheusDataItem.ERROR_NEGATIVE, MoneyWiseBasicResource.XCHGRATE_RATE);
99          }
100 
101         /* Set validation flag */
102         if (!pRate.hasErrors()) {
103             pRate.setValidEdit();
104         }
105     }
106 }