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