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