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.date.OceanusDate;
20  import io.github.tonywasher.joceanus.oceanus.date.OceanusDateRange;
21  import io.github.tonywasher.joceanus.oceanus.decimal.OceanusMoney;
22  import io.github.tonywasher.joceanus.oceanus.format.OceanusDataFormatter;
23  import io.github.tonywasher.joceanus.metis.data.MetisDataItem.MetisDataList;
24  import io.github.tonywasher.joceanus.metis.data.MetisDataItem.MetisDataObjectFormat;
25  import io.github.tonywasher.joceanus.metis.field.MetisFieldItem;
26  import io.github.tonywasher.joceanus.metis.field.MetisFieldSet;
27  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseBasicDataType;
28  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseBasicResource;
29  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseTransaction;
30  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseStaticResource;
31  
32  import java.util.ArrayList;
33  import java.util.List;
34  import java.util.Objects;
35  
36  /**
37   * Chargeable Gains Slice record.
38   */
39  public final class MoneyWiseChargeableGainSlice
40          implements MetisFieldItem {
41      /**
42       * Local Report fields.
43       */
44      private static final MetisFieldSet<MoneyWiseChargeableGainSlice> FIELD_DEFS = MetisFieldSet.newFieldSet(MoneyWiseChargeableGainSlice.class);
45  
46      /*
47       * Declare Fields.
48       */
49      static {
50          FIELD_DEFS.declareLocalField(MoneyWiseBasicResource.MONEYWISEDATA_FIELD_DATE, MoneyWiseChargeableGainSlice::getDate);
51          FIELD_DEFS.declareLocalField(MoneyWiseTaxResource.CHARGEABLEGAIN_GAIN, MoneyWiseChargeableGainSlice::getGain);
52          FIELD_DEFS.declareLocalField(MoneyWiseStaticResource.TRANSINFO_QUALYEARS, MoneyWiseChargeableGainSlice::getYears);
53          FIELD_DEFS.declareLocalField(MoneyWiseTaxResource.CHARGEABLEGAIN_SLICE, MoneyWiseChargeableGainSlice::getSlice);
54          FIELD_DEFS.declareLocalField(MoneyWiseBasicDataType.TRANSACTION, MoneyWiseChargeableGainSlice::getTransaction);
55      }
56  
57      /**
58       * The Date.
59       */
60      private final OceanusDate theDate;
61  
62      /**
63       * The Gain.
64       */
65      private final OceanusMoney theGain;
66  
67      /**
68       * The Years.
69       */
70      private final Integer theYears;
71  
72      /**
73       * The Slice.
74       */
75      private final OceanusMoney theSlice;
76  
77      /**
78       * The Transaction.
79       */
80      private final MoneyWiseTransaction theTrans;
81  
82      /**
83       * Constructor.
84       *
85       * @param pTrans the transaction
86       * @param pGain  the gain
87       */
88      private MoneyWiseChargeableGainSlice(final MoneyWiseTransaction pTrans,
89                                           final OceanusMoney pGain) {
90          /* Store the parameters */
91          theDate = pTrans.getDate();
92          theGain = pGain;
93          theTrans = pTrans;
94          theYears = Objects.requireNonNull(pTrans.getYears());
95  
96          /* Calculate slice */
97          theSlice = new OceanusMoney(pGain);
98          theSlice.divide(theYears);
99      }
100 
101     /**
102      * Constructor.
103      *
104      * @param pTrans the transaction
105      * @param pGain  the gain
106      * @param pSlice the slice
107      * @param pYears the years
108      */
109     private MoneyWiseChargeableGainSlice(final MoneyWiseTransaction pTrans,
110                                          final OceanusMoney pGain,
111                                          final OceanusMoney pSlice,
112                                          final Integer pYears) {
113         /* Store the parameters */
114         theTrans = pTrans;
115         theDate = theTrans.getDate();
116         theGain = pGain;
117         theSlice = pSlice;
118         theYears = pYears;
119     }
120 
121     /**
122      * Obtain the date.
123      *
124      * @return the date
125      */
126     public OceanusDate getDate() {
127         return theDate;
128     }
129 
130     /**
131      * Obtain the gain.
132      *
133      * @return the gain
134      */
135     public OceanusMoney getGain() {
136         return theGain;
137     }
138 
139     /**
140      * Obtain the years.
141      *
142      * @return the years
143      */
144     public Integer getYears() {
145         return theYears;
146     }
147 
148     /**
149      * Obtain the slice.
150      *
151      * @return the slice
152      */
153     public OceanusMoney getSlice() {
154         return theSlice;
155     }
156 
157     /**
158      * Obtain the transaction.
159      *
160      * @return the transaction
161      */
162     public MoneyWiseTransaction getTransaction() {
163         return theTrans;
164     }
165 
166     @Override
167     public String formatObject(final OceanusDataFormatter pFormatter) {
168         return FIELD_DEFS.getName();
169     }
170 
171     @Override
172     public MetisFieldSet<MoneyWiseChargeableGainSlice> getDataFieldSet() {
173         return FIELD_DEFS;
174     }
175 
176     /**
177      * List of Gains Slices.
178      */
179     public static class MoneyWiseChargeableGainSliceList
180             implements MetisDataObjectFormat, MetisDataList<MoneyWiseChargeableGainSlice> {
181         /**
182          * Gains slices.
183          */
184         private final List<MoneyWiseChargeableGainSlice> theSlices;
185 
186         /**
187          * Constructor.
188          */
189         public MoneyWiseChargeableGainSliceList() {
190             theSlices = new ArrayList<>();
191         }
192 
193         /**
194          * Constructor.
195          *
196          * @param pSource the source list.
197          * @param pRange  the range of events to copy
198          */
199         public MoneyWiseChargeableGainSliceList(final MoneyWiseChargeableGainSliceList pSource,
200                                                 final OceanusDateRange pRange) {
201             /* Call standard constructor */
202             this();
203 
204             /* Loop through the source */
205             for (MoneyWiseChargeableGainSlice mySlice : pSource.theSlices) {
206                 /* Check the range */
207                 final int iDiff = pRange.compareToDate(mySlice.getDate());
208 
209                 /* If we are past the range, break the loop */
210                 if (iDiff < 0) {
211                     break;
212                 }
213 
214                 /* If we are within the range */
215                 if (iDiff == 0) {
216                     /* Add to the list */
217                     theSlices.add(mySlice);
218                 }
219             }
220         }
221 
222         @Override
223         public String formatObject(final OceanusDataFormatter pFormatter) {
224             return MoneyWiseChargeableGainSliceList.class.getSimpleName();
225         }
226 
227         /**
228          * Add Chargeable Transaction to List.
229          *
230          * @param pTrans the base transaction
231          * @param pGains the gains
232          */
233         public void addTransaction(final MoneyWiseTransaction pTrans,
234                                    final OceanusMoney pGains) {
235             /* Create the chargeable event */
236             final MoneyWiseChargeableGainSlice mySlice = new MoneyWiseChargeableGainSlice(pTrans, pGains);
237 
238             /* Add it to the list */
239             theSlices.add(mySlice);
240         }
241 
242         /**
243          * Add Chargeable Transaction to List.
244          *
245          * @param pTrans the base transaction
246          * @param pGains the gains
247          * @param pSlice the slice
248          * @param pYears the years
249          */
250         public void addTransaction(final MoneyWiseTransaction pTrans,
251                                    final OceanusMoney pGains,
252                                    final OceanusMoney pSlice,
253                                    final Integer pYears) {
254             /* Create the chargeable event */
255             final MoneyWiseChargeableGainSlice mySlice = new MoneyWiseChargeableGainSlice(pTrans, pGains, pSlice, pYears);
256 
257             /* Add it to the list */
258             theSlices.add(mySlice);
259         }
260 
261         @Override
262         public List<MoneyWiseChargeableGainSlice> getUnderlyingList() {
263             return theSlices;
264         }
265 
266         @Override
267         public boolean isEmpty() {
268             return theSlices.isEmpty();
269         }
270     }
271 }