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  import io.github.tonywasher.joceanus.prometheus.data.PrometheusStaticDataClass;
23  
24  /**
25   * Enumeration of Portfolio Type Classes.
26   */
27  public enum MoneyWisePortfolioClass
28          implements PrometheusStaticDataClass {
29      /**
30       * Standard.
31       * <p>
32       * This is a standard portfolio.
33       */
34      STANDARD(1, 0),
35  
36      /**
37       * ISA.
38       * <p>
39       * This is a TaxFree portfolio.
40       */
41      TAXFREE(2, 1),
42  
43      /**
44       * Pension.
45       * <p>
46       * This is a Pension. and is singular
47       */
48      PENSION(3, 2),
49  
50      /**
51       * SIPP.
52       * <p>
53       * This is a SIPP Portfolio.
54       */
55      SIPP(4, 3);
56  
57      /**
58       * The String name.
59       */
60      private String theName;
61  
62      /**
63       * Class Id.
64       */
65      private final int theId;
66  
67      /**
68       * Class Order.
69       */
70      private final int theOrder;
71  
72      /**
73       * Constructor.
74       *
75       * @param uId    the Id
76       * @param uOrder the default order.
77       */
78      MoneyWisePortfolioClass(final int uId,
79                              final int uOrder) {
80          theId = uId;
81          theOrder = uOrder;
82      }
83  
84      @Override
85      public int getClassId() {
86          return theId;
87      }
88  
89      @Override
90      public int getOrder() {
91          return theOrder;
92      }
93  
94      @Override
95      public String toString() {
96          /* If we have not yet loaded the name */
97          if (theName == null) {
98              /* Load the name */
99              theName = bundleIdForPortfolioClass(this).getValue();
100         }
101 
102         /* return the name */
103         return theName;
104     }
105 
106     /**
107      * get value from id.
108      *
109      * @param id the id value
110      * @return the corresponding enum object
111      * @throws OceanusException on error
112      */
113     public static MoneyWisePortfolioClass fromId(final int id) throws OceanusException {
114         for (MoneyWisePortfolioClass myClass : values()) {
115             if (myClass.getClassId() == id) {
116                 return myClass;
117             }
118         }
119         throw new MoneyWiseDataException("Invalid ClassId for " + MoneyWiseStaticDataType.PORTFOLIOTYPE.toString() + ":" + id);
120     }
121 
122     /**
123      * Determine whether the PortfolioType is tax free.
124      *
125      * @return <code>true</code> if the PortfolioTtype is tax free, <code>false</code> otherwise.
126      */
127     public boolean isTaxFree() {
128         return this != STANDARD;
129     }
130 
131     /**
132      * Determine whether the PortfolioType is a pension.
133      *
134      * @return <code>true</code> if the PortfolioType is pension, <code>false</code> otherwise.
135      */
136     public boolean isPension() {
137         return switch (this) {
138             case PENSION, SIPP -> true;
139             default -> false;
140         };
141     }
142 
143     /**
144      * Determine whether the PortfolioType owns securities.
145      *
146      * @return <code>true</code> if the PortfolioType owns securities, <code>false</code> otherwise.
147      */
148     public boolean holdsSecurities() {
149         return switch (this) {
150             case STANDARD, TAXFREE, SIPP -> true;
151             default -> false;
152         };
153     }
154 
155     /**
156      * Determine whether the PortfolioType can hold pension securities.
157      *
158      * @return <code>true</code> if the PortfolioType owns pensions, <code>false</code> otherwise.
159      */
160     public boolean holdsPensions() {
161         return this == PENSION;
162     }
163 
164     /**
165      * Is this a singular portfolio?.
166      *
167      * @return <code>true</code> if the PortfolioType is singular, <code>false</code> otherwise.
168      */
169     public boolean isSingular() {
170         return holdsPensions();
171     }
172 
173     /**
174      * Obtain the resource bundleId for the portfolio class.
175      *
176      * @param pClass the category class
177      * @return the resource bundleId
178      */
179     private static OceanusBundleId bundleIdForPortfolioClass(final MoneyWisePortfolioClass pClass) {
180         /* Create the map and return it */
181         return switch (pClass) {
182             case STANDARD -> MoneyWiseStaticResource.PORTFOLIOTYPE_STANDARD;
183             case TAXFREE -> MoneyWiseStaticResource.PORTFOLIOTYPE_TAXFREE;
184             case PENSION -> MoneyWiseStaticResource.PORTFOLIOTYPE_PENSION;
185             case SIPP -> MoneyWiseStaticResource.PORTFOLIOTYPE_SIPP;
186             default -> throw new IllegalArgumentException();
187         };
188     }
189 }