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;
18  
19  import io.github.tonywasher.joceanus.oceanus.decimal.OceanusMoney;
20  import io.github.tonywasher.joceanus.oceanus.decimal.OceanusRate;
21  import io.github.tonywasher.joceanus.oceanus.format.OceanusDataFormatter;
22  import io.github.tonywasher.joceanus.metis.data.MetisDataItem.MetisDataList;
23  import io.github.tonywasher.joceanus.metis.data.MetisDataItem.MetisDataObjectFormat;
24  import io.github.tonywasher.joceanus.metis.field.MetisFieldItem;
25  import io.github.tonywasher.joceanus.metis.field.MetisFieldSet;
26  import io.github.tonywasher.joceanus.moneywise.tax.MoneyWiseTaxBandSet.MoneyWiseTaxBand;
27  
28  import java.util.ArrayList;
29  import java.util.Collections;
30  import java.util.Iterator;
31  import java.util.List;
32  
33  /**
34   * Set of taxBands.
35   */
36  public class MoneyWiseTaxBandSet
37          implements MetisDataList<MoneyWiseTaxBand>, MetisDataObjectFormat, Iterable<MoneyWiseTaxBand> {
38      /**
39       * List of Tax Bands.
40       */
41      private final List<MoneyWiseTaxBand> theTaxBands;
42  
43      /**
44       * Constructor.
45       */
46      public MoneyWiseTaxBandSet() {
47          theTaxBands = new ArrayList<>();
48      }
49  
50      /**
51       * Constructor.
52       *
53       * @param pBands the set of tax bands
54       */
55      public MoneyWiseTaxBandSet(final MoneyWiseTaxBand... pBands) {
56          this();
57          Collections.addAll(theTaxBands, pBands);
58      }
59  
60      /**
61       * Constructor.
62       *
63       * @param pSource the source to clone
64       */
65      public MoneyWiseTaxBandSet(final MoneyWiseTaxBandSet pSource) {
66          this();
67          for (MoneyWiseTaxBand myBand : pSource.theTaxBands) {
68              theTaxBands.add(new MoneyWiseTaxBand(myBand));
69          }
70      }
71  
72      @Override
73      public Iterator<MoneyWiseTaxBand> iterator() {
74          return theTaxBands.iterator();
75      }
76  
77      @Override
78      public List<MoneyWiseTaxBand> getUnderlyingList() {
79          return theTaxBands;
80      }
81  
82      @Override
83      public String formatObject(final OceanusDataFormatter pFormatter) {
84          return theTaxBands.toString();
85      }
86  
87      /**
88       * Add a tax band.
89       *
90       * @param pBand the tax band
91       */
92      public void addTaxBand(final MoneyWiseTaxBand pBand) {
93          theTaxBands.add(pBand);
94      }
95  
96      /**
97       * Obtain a zero amount.
98       *
99       * @return the zero amount
100      */
101     public OceanusMoney getZeroAmount() {
102         OceanusMoney myAmount = theTaxBands.get(0).getAmount();
103         myAmount = new OceanusMoney(myAmount);
104         myAmount.setZero();
105         return myAmount;
106     }
107 
108     /**
109      * Are there multiple taxBands?
110      *
111      * @return true/false
112      */
113     public boolean multipleBands() {
114         return theTaxBands.size() > 1;
115     }
116 
117     /**
118      * MoneyWiseTaxBand class.
119      */
120     public static class MoneyWiseTaxBand
121             implements MetisFieldItem {
122         /**
123          * Local Report fields.
124          */
125         private static final MetisFieldSet<MoneyWiseTaxBand> FIELD_DEFS = MetisFieldSet.newFieldSet(MoneyWiseTaxBand.class);
126 
127         /*
128          * Declare Fields.
129          */
130         static {
131             FIELD_DEFS.declareLocalField(MoneyWiseTaxResource.TAXBANDS_RATE, MoneyWiseTaxBand::getAmount);
132             FIELD_DEFS.declareLocalField(MoneyWiseTaxResource.TAXBANDS_AMOUNT, MoneyWiseTaxBand::getRate);
133         }
134 
135         /**
136          * Amount.
137          */
138         private final OceanusMoney theAmount;
139 
140         /**
141          * Rate.
142          */
143         private final OceanusRate theRate;
144 
145         /**
146          * Constructor.
147          *
148          * @param pAmount the amount
149          * @param pRate   the rate
150          */
151         public MoneyWiseTaxBand(final OceanusMoney pAmount,
152                                 final OceanusRate pRate) {
153             theAmount = pAmount == null
154                     ? null
155                     : new OceanusMoney(pAmount);
156             theRate = pRate;
157         }
158 
159         /**
160          * Constructor.
161          *
162          * @param pRate the rate
163          */
164         public MoneyWiseTaxBand(final OceanusRate pRate) {
165             this(null, pRate);
166         }
167 
168         /**
169          * Constructor.
170          *
171          * @param pSource the source band
172          */
173         public MoneyWiseTaxBand(final MoneyWiseTaxBand pSource) {
174             final OceanusMoney myAmount = pSource.getAmount();
175             theAmount = myAmount == null
176                     ? null
177                     : new OceanusMoney(myAmount);
178             theRate = pSource.getRate();
179         }
180 
181         /**
182          * Obtain the amount.
183          *
184          * @return the amount
185          */
186         public OceanusMoney getAmount() {
187             return theAmount;
188         }
189 
190         /**
191          * Obtain the rate.
192          *
193          * @return the rate
194          */
195         public OceanusRate getRate() {
196             return theRate;
197         }
198 
199         @Override
200         public String formatObject(final OceanusDataFormatter pFormatter) {
201             return toString();
202         }
203 
204         @Override
205         public String toString() {
206             final StringBuilder myBuilder = new StringBuilder();
207             if (theAmount != null) {
208                 myBuilder.append(theAmount);
209                 myBuilder.append('@');
210             }
211             myBuilder.append(theRate);
212             return myBuilder.toString();
213         }
214 
215         @Override
216         public MetisFieldSet<MoneyWiseTaxBand> getDataFieldSet() {
217             return FIELD_DEFS;
218         }
219     }
220 }