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.tax.uk;
18  
19  import io.github.tonywasher.joceanus.metis.field.MetisFieldItem;
20  import io.github.tonywasher.joceanus.metis.field.MetisFieldSet;
21  import io.github.tonywasher.joceanus.moneywise.tax.MoneyWiseTaxBandSet;
22  import io.github.tonywasher.joceanus.moneywise.tax.MoneyWiseTaxBandSet.MoneyWiseTaxBand;
23  import io.github.tonywasher.joceanus.moneywise.tax.MoneyWiseTaxResource;
24  import io.github.tonywasher.joceanus.moneywise.tax.uk.MoneyWiseUKTax.MoneyWiseUKTaxBandsCtl;
25  import io.github.tonywasher.joceanus.oceanus.decimal.OceanusRate;
26  import io.github.tonywasher.joceanus.oceanus.format.OceanusDataFormatter;
27  
28  import java.util.Iterator;
29  
30  /**
31   * UK TaxBands.
32   */
33  public class MoneyWiseUKTaxBands
34          implements MetisFieldItem, MoneyWiseUKTaxBandsCtl {
35      /**
36       * Local Report fields.
37       */
38      private static final MetisFieldSet<MoneyWiseUKTaxBands> FIELD_DEFS = MetisFieldSet.newFieldSet(MoneyWiseUKTaxBands.class);
39  
40      /*
41       * Declare Fields.
42       */
43      static {
44          FIELD_DEFS.declareLocalField(MoneyWiseTaxResource.TAXBANDS_STANDARD, MoneyWiseUKTaxBands::getStandardSet);
45          FIELD_DEFS.declareLocalField(MoneyWiseTaxResource.TAXBANDS_HASLOTAXBAND, MoneyWiseUKTaxBands::hasLoTaxBand);
46          FIELD_DEFS.declareLocalField(MoneyWiseTaxResource.TAXBANDS_LOSAVINGS, MoneyWiseUKTaxBands::getLoSavings);
47      }
48  
49      /**
50       * Standard TaxBandSet.
51       */
52      private final MoneyWiseTaxBandSet theStandard;
53  
54      /**
55       * Low TaxBand present.
56       */
57      private final boolean hasLoTaxBand;
58  
59      /**
60       * Low Savings TaxBands.
61       */
62      private final MoneyWiseTaxBand theLoSavings;
63  
64      /**
65       * Constructor.
66       *
67       * @param pStandard the standard taxBands
68       */
69      protected MoneyWiseUKTaxBands(final MoneyWiseTaxBandSet pStandard) {
70          this(pStandard, Boolean.FALSE);
71      }
72  
73      /**
74       * Constructor.
75       *
76       * @param pStandard     the standard taxBands
77       * @param pHasLoTaxBand do we have a low tax band?
78       */
79      protected MoneyWiseUKTaxBands(final MoneyWiseTaxBandSet pStandard,
80                                    final Boolean pHasLoTaxBand) {
81          this(pStandard, pHasLoTaxBand, null);
82      }
83  
84      /**
85       * Constructor.
86       *
87       * @param pStandard  the standard taxBands
88       * @param pLoSavings the loSavings taxBand
89       */
90      protected MoneyWiseUKTaxBands(final MoneyWiseTaxBandSet pStandard,
91                                    final MoneyWiseTaxBand pLoSavings) {
92          this(pStandard, Boolean.FALSE, pLoSavings);
93      }
94  
95      /**
96       * Constructor.
97       *
98       * @param pStandard     the standard taxBands
99       * @param pHasLoTaxBand do we have a low tax band?
100      * @param pLoSavings    the loSavings taxBand
101      */
102     private MoneyWiseUKTaxBands(final MoneyWiseTaxBandSet pStandard,
103                                 final Boolean pHasLoTaxBand,
104                                 final MoneyWiseTaxBand pLoSavings) {
105         theStandard = pStandard;
106         hasLoTaxBand = pHasLoTaxBand;
107         theLoSavings = pLoSavings;
108     }
109 
110     /**
111      * Constructor.
112      *
113      * @param pSource the source taxBands
114      */
115     protected MoneyWiseUKTaxBands(final MoneyWiseUKTaxBands pSource) {
116         theStandard = new MoneyWiseTaxBandSet(pSource.getStandardSet());
117         hasLoTaxBand = pSource.hasLoTaxBand;
118         final MoneyWiseTaxBand mySavings = pSource.getLoSavings();
119         theLoSavings = mySavings == null
120                 ? null
121                 : new MoneyWiseTaxBand(mySavings);
122     }
123 
124     /**
125      * Obtain the standard taxBands.
126      *
127      * @return the taxBands
128      */
129     public MoneyWiseTaxBandSet getStandardSet() {
130         return theStandard;
131     }
132 
133     /**
134      * Do we have a Low taxBand?
135      *
136      * @return true/false
137      */
138     public Boolean hasLoTaxBand() {
139         return hasLoTaxBand;
140     }
141 
142     /**
143      * Obtain the low savings taxBand.
144      *
145      * @return the taxBands
146      */
147     public MoneyWiseTaxBand getLoSavings() {
148         return theLoSavings;
149     }
150 
151     @Override
152     public OceanusRate getBasicTaxRate() {
153         final Iterator<MoneyWiseTaxBand> myIterator = theStandard.iterator();
154         if (hasLoTaxBand && myIterator.hasNext()) {
155             myIterator.next();
156         }
157         return myIterator.hasNext()
158                 ? myIterator.next().getRate()
159                 : null;
160     }
161 
162     @Override
163     public String formatObject(final OceanusDataFormatter pFormatter) {
164         return FIELD_DEFS.getName();
165     }
166 
167     @Override
168     public MetisFieldSet<MoneyWiseUKTaxBands> getDataFieldSet() {
169         return FIELD_DEFS;
170     }
171 }