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