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.MoneyWiseDepositRate;
21  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseDepositRate.MoneyWiseDepositRateDataMap;
22  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseDepositRate.MoneyWiseDepositRateList;
23  import io.github.tonywasher.joceanus.oceanus.date.OceanusDate;
24  import io.github.tonywasher.joceanus.oceanus.decimal.OceanusRate;
25  import io.github.tonywasher.joceanus.prometheus.data.PrometheusData.PrometheusDataItemCtl;
26  import io.github.tonywasher.joceanus.prometheus.data.PrometheusData.PrometheusDataValidator;
27  import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataItem;
28  
29  /**
30   * Validator for DepositRate.
31   */
32  public class MoneyWiseValidateDepositRate
33          implements PrometheusDataValidator {
34  
35      @Override
36      public void validate(final PrometheusDataItemCtl pRate) {
37          final MoneyWiseDepositRate myRate = (MoneyWiseDepositRate) pRate;
38          final MoneyWiseDepositRateList myList = myRate.getList();
39          final OceanusDate myDate = myRate.getEndDate();
40          final OceanusRate myDepRate = myRate.getRate();
41          final OceanusRate myBonus = myRate.getBonus();
42  
43          /* Count instances of this date for the account */
44          final MoneyWiseDepositRateDataMap myMap = myList.getDataMap();
45          if (!myMap.validRateCount(myRate)) {
46              /* Each date must be unique for deposit (even null) */
47              myRate.addError(myDate == null
48                      ? MoneyWiseDepositRate.ERROR_NULLDATE
49                      : PrometheusDataItem.ERROR_DUPLICATE, MoneyWiseBasicResource.DEPOSITRATE_ENDDATE);
50          }
51  
52          /* The Rate must be non-zero and greater than zero */
53          if (myDepRate == null) {
54              myRate.addError(PrometheusDataItem.ERROR_MISSING, MoneyWiseBasicResource.MONEYWISEDATA_FIELD_RATE);
55          } else if (!myDepRate.isPositive()) {
56              myRate.addError(PrometheusDataItem.ERROR_NEGATIVE, MoneyWiseBasicResource.MONEYWISEDATA_FIELD_RATE);
57          }
58  
59          /* The bonus rate must be non-zero if it exists */
60          if (myBonus != null) {
61              if (myBonus.isZero()) {
62                  myRate.addError(PrometheusDataItem.ERROR_ZERO, MoneyWiseBasicResource.DEPOSITRATE_BONUS);
63              } else if (!myBonus.isPositive()) {
64                  myRate.addError(PrometheusDataItem.ERROR_NEGATIVE, MoneyWiseBasicResource.DEPOSITRATE_BONUS);
65              }
66          }
67  
68          /* Set validation flag */
69          if (!myRate.hasErrors()) {
70              myRate.setValidEdit();
71          }
72      }
73  }