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