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