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.data.statics;
18  
19  import io.github.tonywasher.joceanus.metis.data.MetisDataItem.MetisDataFieldId;
20  import io.github.tonywasher.joceanus.metis.data.MetisDataType;
21  import io.github.tonywasher.joceanus.moneywise.exc.MoneyWiseDataException;
22  import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
23  import io.github.tonywasher.joceanus.oceanus.resource.OceanusBundleId;
24  import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataInfoClass;
25  
26  /**
27   * Enumeration of TransactionInfo Classes..
28   */
29  public enum MoneyWiseTransInfoClass
30          implements PrometheusDataInfoClass, MetisDataFieldId {
31      /**
32       * Tax Credit.
33       */
34      TAXCREDIT(1, 0, MetisDataType.MONEY),
35  
36      /**
37       * Employer National Insurance.
38       */
39      EMPLOYERNATINS(2, 1, MetisDataType.MONEY),
40  
41      /**
42       * Employee National Insurance.
43       */
44      EMPLOYEENATINS(3, 2, MetisDataType.MONEY),
45  
46      /**
47       * Deemed Benefit.
48       */
49      DEEMEDBENEFIT(4, 3, MetisDataType.MONEY),
50  
51      /**
52       * QualifyingYears.
53       */
54      QUALIFYYEARS(5, 4, MetisDataType.INTEGER),
55  
56      /**
57       * Account Delta Units.
58       */
59      ACCOUNTDELTAUNITS(6, 5, MetisDataType.UNITS),
60  
61      /**
62       * Partner Delta Units.
63       */
64      PARTNERDELTAUNITS(7, 6, MetisDataType.UNITS),
65  
66      /**
67       * Dilution.
68       */
69      DILUTION(8, 7, MetisDataType.RATIO),
70  
71      /**
72       * Reference.
73       */
74      REFERENCE(9, 8, MetisDataType.STRING),
75  
76      /**
77       * Withheld.
78       */
79      WITHHELD(10, 9, MetisDataType.MONEY),
80  
81      /**
82       * Partner Amount.
83       */
84      PARTNERAMOUNT(11, 10, MetisDataType.MONEY),
85  
86      /**
87       * ThirdParty Amount.
88       */
89      RETURNEDCASH(12, 11, MetisDataType.MONEY),
90  
91      /**
92       * ReturnedCashAccount.
93       */
94      RETURNEDCASHACCOUNT(13, 12, MetisDataType.LINKPAIR),
95  
96      /**
97       * Comments.
98       */
99      COMMENTS(14, 13, MetisDataType.STRING),
100 
101     /**
102      * Price.
103      */
104     PRICE(15, 14, MetisDataType.PRICE),
105 
106     /**
107      * XchangeRate.
108      */
109     XCHANGERATE(16, 15, MetisDataType.RATIO),
110 
111     /**
112      * Commission.
113      */
114     COMMISSION(17, 16, MetisDataType.MONEY),
115 
116     /**
117      * TransactionTag.
118      */
119     TRANSTAG(18, 17, MetisDataType.LINKSET);
120 
121     /**
122      * Data length.
123      */
124     private static final int DATA_LEN = 20;
125 
126     /**
127      * Comment length.
128      */
129     private static final int COMMENT_LEN = 50;
130 
131     /**
132      * The String name.
133      */
134     private String theName;
135 
136     /**
137      * Class Id.
138      */
139     private final int theId;
140 
141     /**
142      * Class Order.
143      */
144     private final int theOrder;
145 
146     /**
147      * Data Type.
148      */
149     private final MetisDataType theDataType;
150 
151     /**
152      * Constructor.
153      *
154      * @param uId       the id
155      * @param uOrder    the default order
156      * @param pDataType the data type
157      */
158     MoneyWiseTransInfoClass(final int uId,
159                             final int uOrder,
160                             final MetisDataType pDataType) {
161         theId = uId;
162         theOrder = uOrder;
163         theDataType = pDataType;
164     }
165 
166     @Override
167     public int getClassId() {
168         return theId;
169     }
170 
171     @Override
172     public int getOrder() {
173         return theOrder;
174     }
175 
176     @Override
177     public MetisDataType getDataType() {
178         return theDataType;
179     }
180 
181     @Override
182     public boolean isLink() {
183         return switch (theDataType) {
184             case LINK, LINKPAIR, LINKSET -> true;
185             default -> false;
186         };
187     }
188 
189     @Override
190     public boolean isLinkSet() {
191         return theDataType == MetisDataType.LINKSET;
192     }
193 
194     @Override
195     public String toString() {
196         /* If we have not yet loaded the name */
197         if (theName == null) {
198             /* Load the name */
199             theName = bundleIdForInfoClass(this).getValue();
200         }
201 
202         /* return the name */
203         return theName;
204     }
205 
206     /**
207      * get value from id.
208      *
209      * @param id the id value
210      * @return the corresponding enum object
211      * @throws OceanusException on error
212      */
213     public static MoneyWiseTransInfoClass fromId(final int id) throws OceanusException {
214         for (MoneyWiseTransInfoClass myClass : values()) {
215             if (myClass.getClassId() == id) {
216                 return myClass;
217             }
218         }
219         throw new MoneyWiseDataException("Invalid ClassId for " + MoneyWiseStaticDataType.TRANSINFOTYPE.toString() + ":" + id);
220     }
221 
222     /**
223      * Obtain maximum length for infoType.
224      *
225      * @return the maximum length
226      */
227     public int getMaximumLength() {
228         return switch (this) {
229             case REFERENCE -> DATA_LEN;
230             case COMMENTS -> COMMENT_LEN;
231             default -> 0;
232         };
233     }
234 
235     @Override
236     public String getId() {
237         return toString();
238     }
239 
240     /**
241      * Obtain the resource bundleId for the info class.
242      *
243      * @param pClass the info class
244      * @return the resource bundleId
245      */
246     private static OceanusBundleId bundleIdForInfoClass(final MoneyWiseTransInfoClass pClass) {
247         /* Create the map and return it */
248         return switch (pClass) {
249             case TAXCREDIT -> MoneyWiseStaticResource.TRANSINFO_TAXCREDIT;
250             case EMPLOYERNATINS -> MoneyWiseStaticResource.TRANSTYPE_EMPLOYERNATINS;
251             case EMPLOYEENATINS -> MoneyWiseStaticResource.TRANSTYPE_EMPLOYEENATINS;
252             case DEEMEDBENEFIT -> MoneyWiseStaticResource.TRANSINFO_BENEFIT;
253             case WITHHELD -> MoneyWiseStaticResource.TRANSTYPE_WITHHELD;
254             case ACCOUNTDELTAUNITS -> MoneyWiseStaticResource.TRANSINFO_ACCOUNTDELTAUNITS;
255             case PARTNERDELTAUNITS -> MoneyWiseStaticResource.TRANSINFO_PARTNERDELTAUNITS;
256             case PARTNERAMOUNT -> MoneyWiseStaticResource.TRANSINFO_PARTNERAMOUNT;
257             case RETURNEDCASH -> MoneyWiseStaticResource.TRANSINFO_RETURNEDCASH;
258             case DILUTION -> MoneyWiseStaticResource.TRANSINFO_DILUTION;
259             case QUALIFYYEARS -> MoneyWiseStaticResource.TRANSINFO_QUALYEARS;
260             case REFERENCE -> MoneyWiseStaticResource.TRANSINFO_REFERENCE;
261             case COMMENTS -> MoneyWiseStaticResource.TRANSINFO_COMMENTS;
262             case RETURNEDCASHACCOUNT -> MoneyWiseStaticResource.TRANSINFO_RETURNEDCASHACCOUNT;
263             case PRICE -> MoneyWiseStaticResource.TRANSINFO_PRICE;
264             case XCHANGERATE -> MoneyWiseStaticResource.TRANSINFO_XCHANGERATE;
265             case COMMISSION -> MoneyWiseStaticResource.TRANSINFO_COMMISSION;
266             case TRANSTAG -> MoneyWiseStaticResource.TRANSINFO_TRANSTAG;
267             default -> throw new IllegalArgumentException();
268         };
269     }
270 }