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.basic;
18  
19  import io.github.tonywasher.joceanus.metis.data.MetisDataFieldValue;
20  import io.github.tonywasher.joceanus.metis.data.MetisDataItem.MetisDataFieldId;
21  import io.github.tonywasher.joceanus.metis.field.MetisFieldSet;
22  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseSecurityInfo.MoneyWiseSecurityInfoList;
23  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseAccountInfoClass;
24  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseAccountInfoType.MoneyWiseAccountInfoTypeList;
25  import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
26  import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataInfoClass;
27  import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataInfoSet;
28  import io.github.tonywasher.joceanus.prometheus.views.PrometheusEditSet;
29  
30  import java.util.Arrays;
31  import java.util.Iterator;
32  import java.util.Map;
33  
34  /**
35   * SecurityInfoSet class.
36   *
37   * @author Tony Washer
38   */
39  public class MoneyWiseSecurityInfoSet
40          extends PrometheusDataInfoSet<MoneyWiseSecurityInfo> {
41      /**
42       * Report fields.
43       */
44      private static final MetisFieldSet<MoneyWiseSecurityInfoSet> FIELD_DEFS = MetisFieldSet.newFieldSet(MoneyWiseSecurityInfoSet.class);
45  
46      /**
47       * FieldSet map.
48       */
49      private static final Map<MetisDataFieldId, MoneyWiseAccountInfoClass> FIELDSET_MAP = FIELD_DEFS.buildFieldMap(MoneyWiseAccountInfoClass.class, MoneyWiseSecurityInfoSet::getFieldValue);
50  
51      /**
52       * Reverse FieldSet map.
53       */
54      private static final Map<MoneyWiseAccountInfoClass, MetisDataFieldId> REVERSE_FIELDMAP = MetisFieldSet.reverseFieldMap(FIELDSET_MAP, MoneyWiseAccountInfoClass.class);
55  
56      /**
57       * Constructor.
58       *
59       * @param pOwner    the Owner to which this Set belongs
60       * @param pTypeList the infoTypeList for the set
61       * @param pInfoList the InfoList for the set
62       */
63      protected MoneyWiseSecurityInfoSet(final MoneyWiseSecurity pOwner,
64                                         final MoneyWiseAccountInfoTypeList pTypeList,
65                                         final MoneyWiseSecurityInfoList pInfoList) {
66          /* Store the Owner and Info List */
67          super(pOwner, pTypeList, pInfoList);
68      }
69  
70      @Override
71      public MetisFieldSetDef getDataFieldSet() {
72          return FIELD_DEFS;
73      }
74  
75      @Override
76      public MoneyWiseSecurity getOwner() {
77          return (MoneyWiseSecurity) super.getOwner();
78      }
79  
80      /**
81       * Obtain fieldValue for infoSet.
82       *
83       * @param pFieldId the fieldId
84       * @return the value
85       */
86      public Object getFieldValue(final MetisDataFieldId pFieldId) {
87          /* Handle InfoSet fields */
88          final MoneyWiseAccountInfoClass myClass = getClassForField(pFieldId);
89          if (myClass != null) {
90              return getInfoSetValue(myClass);
91          }
92  
93          /* Pass onwards */
94          return null;
95      }
96  
97      /**
98       * Get an infoSet value.
99       *
100      * @param pInfoClass the class of info to get
101      * @return the value to set
102      */
103     private Object getInfoSetValue(final MoneyWiseAccountInfoClass pInfoClass) {
104         final Object myValue = switch (pInfoClass) {
105             case REGION ->
106                 /* Access region of object */
107                     getRegion(pInfoClass);
108             case UNDERLYINGSTOCK ->
109                 /* Access underlying Stock of object */
110                     getSecurity(pInfoClass);
111             default ->
112                 /* Access value of object */
113                     getField(pInfoClass);
114         };
115 
116         /* Return the value */
117         return myValue != null
118                 ? myValue
119                 : MetisDataFieldValue.SKIP;
120     }
121 
122     /**
123      * Obtain the class of the field if it is an infoSet field.
124      *
125      * @param pField the field
126      * @return the class
127      */
128     public static MoneyWiseAccountInfoClass getClassForField(final MetisDataFieldId pField) {
129         /* Look up field in map */
130         return FIELDSET_MAP.get(pField);
131     }
132 
133     /**
134      * Obtain the field for the infoSet class.
135      *
136      * @param pClass the class
137      * @return the field
138      */
139     public static MetisDataFieldId getFieldForClass(final MoneyWiseAccountInfoClass pClass) {
140         /* Look up field in map */
141         return REVERSE_FIELDMAP.get(pClass);
142     }
143 
144     @Override
145     public MetisDataFieldId getFieldForClass(final PrometheusDataInfoClass pClass) {
146         return getFieldForClass((MoneyWiseAccountInfoClass) pClass);
147     }
148 
149     @Override
150     public Iterator<PrometheusDataInfoClass> classIterator() {
151         final PrometheusDataInfoClass[] myValues = MoneyWiseAccountInfoClass.values();
152         return Arrays.stream(myValues).iterator();
153     }
154 
155     /**
156      * Clone the dataInfoSet.
157      *
158      * @param pSource the InfoSet to clone
159      */
160     protected void cloneDataInfoSet(final MoneyWiseSecurityInfoSet pSource) {
161         /* Clone the dataInfoSet */
162         cloneTheDataInfoSet(pSource);
163     }
164 
165     /**
166      * Resolve editSetLinks.
167      *
168      * @param pEditSet the editSet
169      * @throws OceanusException on error
170      */
171     void resolveEditSetLinks(final PrometheusEditSet pEditSet) throws OceanusException {
172         /* Loop through the items */
173         for (MoneyWiseSecurityInfo myInfo : this) {
174             myInfo.resolveEditSetLinks(pEditSet);
175         }
176     }
177 
178     /**
179      * Obtain the region for the infoClass.
180      *
181      * @param pInfoClass the Info Class
182      * @return the deposit
183      */
184     public MoneyWiseRegion getRegion(final MoneyWiseAccountInfoClass pInfoClass) {
185         /* Access existing entry */
186         final MoneyWiseSecurityInfo myValue = getInfo(pInfoClass);
187 
188         /* If we have no entry, return null */
189         if (myValue == null) {
190             return null;
191         }
192 
193         /* Return the region */
194         return myValue.getRegion();
195     }
196 
197     /**
198      * Obtain the security for the infoClass.
199      *
200      * @param pInfoClass the Info Class
201      * @return the security
202      */
203     public MoneyWiseSecurity getSecurity(final MoneyWiseAccountInfoClass pInfoClass) {
204         /* Access existing entry */
205         final MoneyWiseSecurityInfo myValue = getInfo(pInfoClass);
206 
207         /* If we have no entry, return null */
208         if (myValue == null) {
209             return null;
210         }
211 
212         /* Return the security */
213         return myValue.getSecurity();
214     }
215 }