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.moneywise.exc.MoneyWiseDataException;
20  import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
21  import io.github.tonywasher.joceanus.oceanus.resource.OceanusBundleId;
22  
23  /**
24   * Enumeration of CashCategory Type Classes.
25   */
26  public enum MoneyWiseCashCategoryClass
27          implements MoneyWiseCategoryInterface {
28      /**
29       * Cash Account.
30       * <p>
31       * This is a cash account and represents cash that is held by the client outside of any
32       * institution.
33       */
34      CASH(1, 1),
35  
36      /**
37       * AutoExpense Cash Account.
38       * <p>
39       * This is a cash account and represents cash that is held by the client outside of any
40       * institution.
41       */
42      AUTOEXPENSE(2, 2),
43  
44      /**
45       * Parent Category.
46       * <p>
47       * This is used as a sub-total bucket and is used purely for reporting purposes.
48       */
49      PARENT(3, 0);
50  
51      /**
52       * The String name.
53       */
54      private String theName;
55  
56      /**
57       * Class Id.
58       */
59      private final int theId;
60  
61      /**
62       * Class Order.
63       */
64      private final int theOrder;
65  
66      /**
67       * Constructor.
68       *
69       * @param uId    the Id
70       * @param uOrder the default order.
71       */
72      MoneyWiseCashCategoryClass(final int uId,
73                                 final int uOrder) {
74          theId = uId;
75          theOrder = uOrder;
76      }
77  
78      @Override
79      public int getClassId() {
80          return theId;
81      }
82  
83      @Override
84      public int getOrder() {
85          return theOrder;
86      }
87  
88      @Override
89      public String toString() {
90          /* If we have not yet loaded the name */
91          if (theName == null) {
92              /* Load the name */
93              theName = bundleIdForCategoryClass(this).getValue();
94          }
95  
96          /* return the name */
97          return theName;
98      }
99  
100     /**
101      * get value from id.
102      *
103      * @param id the id value
104      * @return the corresponding enum object
105      * @throws OceanusException on error
106      */
107     public static MoneyWiseCashCategoryClass fromId(final int id) throws OceanusException {
108         for (MoneyWiseCashCategoryClass myClass : values()) {
109             if (myClass.getClassId() == id) {
110                 return myClass;
111             }
112         }
113         throw new MoneyWiseDataException("Invalid ClassId for " + MoneyWiseStaticDataType.CASHTYPE.toString() + ":" + id);
114     }
115 
116     /**
117      * Determine whether the CashCategoryType is a parent category.
118      *
119      * @return <code>true</code> if the cash category type is a parent category, <code>false</code>
120      * otherwise.
121      */
122     public boolean isParentCategory() {
123         return this == MoneyWiseCashCategoryClass.PARENT;
124     }
125 
126     @Override
127     public boolean isTotals() {
128         return false;
129     }
130 
131     /**
132      * Obtain the resource bundleId for the category class.
133      *
134      * @param pClass the category class
135      * @return the resource bundleId
136      */
137     private static OceanusBundleId bundleIdForCategoryClass(final MoneyWiseCashCategoryClass pClass) {
138         /* Create the map and return it */
139         return switch (pClass) {
140             case CASH -> MoneyWiseStaticResource.CASHTYPE_CASH;
141             case AUTOEXPENSE -> MoneyWiseStaticResource.CASHTYPE_AUTO;
142             case PARENT -> MoneyWiseStaticResource.CATEGORYTYPE_PARENT;
143             default -> throw new IllegalArgumentException();
144         };
145     }
146 }