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.atlas.data.analysis.buckets;
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.metis.data.MetisDataDifference;
22  import io.github.tonywasher.joceanus.metis.field.MetisFieldSet;
23  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseBasicDataType;
24  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseLoan;
25  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseLoanCategory;
26  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseLoanCategoryClass;
27  
28  import java.util.Iterator;
29  
30  /**
31   * The Loan Bucket class.
32   */
33  public final class MoneyWiseXAnalysisLoanBucket
34          extends MoneyWiseXAnalysisAccountBucket<MoneyWiseLoan> {
35      /**
36       * Local Report fields.
37       */
38      private static final MetisFieldSet<MoneyWiseXAnalysisLoanBucket> FIELD_DEFS = MetisFieldSet.newFieldSet(MoneyWiseXAnalysisLoanBucket.class);
39  
40      /*
41       * Declare Fields.
42       */
43      static {
44          FIELD_DEFS.declareLocalField(MoneyWiseBasicDataType.LOANCATEGORY, MoneyWiseXAnalysisLoanBucket::getCategory);
45          FIELD_DEFS.declareLocalField(MoneyWiseXAnalysisBucketResource.LOAN_CREDITCARD, MoneyWiseXAnalysisLoanBucket::isCreditCard);
46      }
47  
48      /**
49       * The loan category.
50       */
51      private final MoneyWiseLoanCategory theCategory;
52  
53      /**
54       * Is this a creditCard?
55       */
56      private final Boolean isCreditCard;
57  
58      /**
59       * Constructor.
60       *
61       * @param pAnalysis the analysis
62       * @param pLoan     the loan
63       */
64      private MoneyWiseXAnalysisLoanBucket(final MoneyWiseXAnalysis pAnalysis,
65                                           final MoneyWiseLoan pLoan) {
66          /* Call super-constructor */
67          super(pAnalysis, pLoan);
68  
69          /* Obtain category */
70          theCategory = pLoan.getCategory();
71  
72          /* Determine whether this is a credit card */
73          isCreditCard = theCategory.isCategoryClass(MoneyWiseLoanCategoryClass.CREDITCARD);
74      }
75  
76      /**
77       * Constructor.
78       *
79       * @param pAnalysis the analysis
80       * @param pBase     the underlying bucket
81       */
82      private MoneyWiseXAnalysisLoanBucket(final MoneyWiseXAnalysis pAnalysis,
83                                           final MoneyWiseXAnalysisLoanBucket pBase) {
84          /* Call super-constructor */
85          super(pAnalysis, pBase);
86  
87          /* Copy details from base */
88          theCategory = pBase.getCategory();
89          isCreditCard = pBase.isCreditCard();
90      }
91  
92      /**
93       * Constructor.
94       *
95       * @param pAnalysis the analysis
96       * @param pBase     the underlying bucket
97       * @param pDate     the date for the bucket
98       */
99      private MoneyWiseXAnalysisLoanBucket(final MoneyWiseXAnalysis pAnalysis,
100                                          final MoneyWiseXAnalysisLoanBucket pBase,
101                                          final OceanusDate pDate) {
102         /* Call super-constructor */
103         super(pAnalysis, pBase, pDate);
104 
105         /* Copy details from base */
106         theCategory = pBase.getCategory();
107         isCreditCard = pBase.isCreditCard();
108     }
109 
110     /**
111      * Constructor.
112      *
113      * @param pAnalysis the analysis
114      * @param pBase     the underlying bucket
115      * @param pRange    the range for the bucket
116      */
117     private MoneyWiseXAnalysisLoanBucket(final MoneyWiseXAnalysis pAnalysis,
118                                          final MoneyWiseXAnalysisLoanBucket pBase,
119                                          final OceanusDateRange pRange) {
120         /* Call super-constructor */
121         super(pAnalysis, pBase, pRange);
122 
123         /* Copy details from base */
124         theCategory = pBase.getCategory();
125         isCreditCard = pBase.isCreditCard();
126     }
127 
128     @Override
129     public MetisFieldSet<MoneyWiseXAnalysisLoanBucket> getDataFieldSet() {
130         return FIELD_DEFS;
131     }
132 
133     /**
134      * Obtain the loan category.
135      *
136      * @return the loan category
137      */
138     public MoneyWiseLoanCategory getCategory() {
139         return theCategory;
140     }
141 
142     /**
143      * Is this a creditCard?
144      *
145      * @return true/false
146      */
147     public Boolean isCreditCard() {
148         return isCreditCard;
149     }
150 
151     @Override
152     public Long getBucketId() {
153         return getAccount().getExternalId();
154     }
155 
156     /**
157      * LoanBucket list class.
158      */
159     public static final class MoneyWiseXAnalysisLoanBucketList
160             extends MoneyWiseXAnalysisAccountBucketList<MoneyWiseXAnalysisLoanBucket, MoneyWiseLoan> {
161         /**
162          * Local Report fields.
163          */
164         private static final MetisFieldSet<MoneyWiseXAnalysisLoanBucketList> FIELD_DEFS = MetisFieldSet.newFieldSet(MoneyWiseXAnalysisLoanBucketList.class);
165 
166         /**
167          * Construct a top-level List.
168          *
169          * @param pAnalysis the analysis
170          */
171         MoneyWiseXAnalysisLoanBucketList(final MoneyWiseXAnalysis pAnalysis) {
172             /* Initialise class */
173             super(pAnalysis);
174         }
175 
176         /**
177          * Construct a dated List.
178          *
179          * @param pAnalysis the analysis
180          * @param pBase     the base list
181          * @param pDate     the Date
182          */
183         MoneyWiseXAnalysisLoanBucketList(final MoneyWiseXAnalysis pAnalysis,
184                                          final MoneyWiseXAnalysisLoanBucketList pBase,
185                                          final OceanusDate pDate) {
186             /* Initialise class */
187             this(pAnalysis);
188 
189             /* Construct list from base */
190             constructFromBase(pBase, pDate);
191         }
192 
193         /**
194          * Construct a ranged List.
195          *
196          * @param pAnalysis the analysis
197          * @param pBase     the base list
198          * @param pRange    the Date Range
199          */
200         MoneyWiseXAnalysisLoanBucketList(final MoneyWiseXAnalysis pAnalysis,
201                                          final MoneyWiseXAnalysisLoanBucketList pBase,
202                                          final OceanusDateRange pRange) {
203             /* Initialise class */
204             this(pAnalysis);
205 
206             /* Construct list from base */
207             constructFromBase(pBase, pRange);
208         }
209 
210         @Override
211         public MetisFieldSet<MoneyWiseXAnalysisLoanBucketList> getDataFieldSet() {
212             return FIELD_DEFS;
213         }
214 
215         /**
216          * Obtain the matching LoanBucket.
217          *
218          * @param pLoan the loan
219          * @return the matching bucket
220          */
221         public MoneyWiseXAnalysisLoanBucket getMatchingLoan(final MoneyWiseLoan pLoan) {
222             /* Return the matching loan if it exists else an orphan bucket */
223             final MoneyWiseXAnalysisLoanBucket myLoan = findItemById(pLoan.getIndexedId());
224             return myLoan != null
225                     ? myLoan
226                     : new MoneyWiseXAnalysisLoanBucket(getAnalysis(), pLoan);
227         }
228 
229         /**
230          * Obtain the default Cash.
231          *
232          * @return the bucket
233          */
234         public MoneyWiseXAnalysisLoanBucket getDefaultLoan() {
235             /* Return the first loan in the list if it exists */
236             return isEmpty()
237                     ? null
238                     : getUnderlyingList().get(0);
239         }
240 
241         /**
242          * Obtain the default Loan for the category.
243          *
244          * @param pCategory the category
245          * @return the bucket
246          */
247         public MoneyWiseXAnalysisLoanBucket getDefaultLoan(final MoneyWiseLoanCategory pCategory) {
248             /* If there is a category */
249             if (pCategory != null) {
250                 /* Loop through the available account values */
251                 final Iterator<MoneyWiseXAnalysisLoanBucket> myIterator = iterator();
252                 while (myIterator.hasNext()) {
253                     final MoneyWiseXAnalysisLoanBucket myBucket = myIterator.next();
254 
255                     /* Return if correct category */
256                     if (MetisDataDifference.isEqual(pCategory, myBucket.getCategory())) {
257                         return myBucket;
258                     }
259                 }
260             }
261 
262             /* No default loan */
263             return null;
264         }
265 
266         @Override
267         protected MoneyWiseXAnalysisLoanBucket newBucket(final MoneyWiseLoan pLoan) {
268             return new MoneyWiseXAnalysisLoanBucket(getAnalysis(), pLoan);
269         }
270 
271         @Override
272         protected MoneyWiseXAnalysisLoanBucket newBucket(final MoneyWiseXAnalysisLoanBucket pBase) {
273             return new MoneyWiseXAnalysisLoanBucket(getAnalysis(), pBase);
274         }
275 
276         @Override
277         protected MoneyWiseXAnalysisLoanBucket newBucket(final MoneyWiseXAnalysisLoanBucket pBase,
278                                                          final OceanusDate pDate) {
279             return new MoneyWiseXAnalysisLoanBucket(getAnalysis(), pBase, pDate);
280         }
281 
282         @Override
283         protected MoneyWiseXAnalysisLoanBucket newBucket(final MoneyWiseXAnalysisLoanBucket pBase,
284                                                          final OceanusDateRange pRange) {
285             return new MoneyWiseXAnalysisLoanBucket(getAnalysis(), pBase, pRange);
286         }
287     }
288 }