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