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.oceanus.base.OceanusException;
20  import io.github.tonywasher.joceanus.metis.data.MetisDataFieldValue;
21  import io.github.tonywasher.joceanus.metis.data.MetisDataItem.MetisDataFieldId;
22  import io.github.tonywasher.joceanus.metis.field.MetisFieldSet;
23  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseSecurityInfo.MoneyWiseSecurityInfoList;
24  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseAccountInfoClass;
25  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseAccountInfoType.MoneyWiseAccountInfoTypeList;
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;
105 
106         switch (pInfoClass) {
107             case REGION:
108                 /* Access region of object */
109                 myValue = getRegion(pInfoClass);
110                 break;
111             case UNDERLYINGSTOCK:
112                 /* Access underlying Stock of object */
113                 myValue = getSecurity(pInfoClass);
114                 break;
115             default:
116                 /* Access value of object */
117                 myValue = getField(pInfoClass);
118                 break;
119         }
120 
121         /* Return the value */
122         return myValue != null
123                 ? myValue
124                 : MetisDataFieldValue.SKIP;
125     }
126 
127     /**
128      * Obtain the class of the field if it is an infoSet field.
129      *
130      * @param pField the field
131      * @return the class
132      */
133     public static MoneyWiseAccountInfoClass getClassForField(final MetisDataFieldId pField) {
134         /* Look up field in map */
135         return FIELDSET_MAP.get(pField);
136     }
137 
138     /**
139      * Obtain the field for the infoSet class.
140      *
141      * @param pClass the class
142      * @return the field
143      */
144     public static MetisDataFieldId getFieldForClass(final MoneyWiseAccountInfoClass pClass) {
145         /* Look up field in map */
146         return REVERSE_FIELDMAP.get(pClass);
147     }
148 
149     @Override
150     public MetisDataFieldId getFieldForClass(final PrometheusDataInfoClass pClass) {
151         return getFieldForClass((MoneyWiseAccountInfoClass) pClass);
152     }
153 
154     @Override
155     public Iterator<PrometheusDataInfoClass> classIterator() {
156         final PrometheusDataInfoClass[] myValues = MoneyWiseAccountInfoClass.values();
157         return Arrays.stream(myValues).iterator();
158     }
159 
160     /**
161      * Clone the dataInfoSet.
162      *
163      * @param pSource the InfoSet to clone
164      */
165     protected void cloneDataInfoSet(final MoneyWiseSecurityInfoSet pSource) {
166         /* Clone the dataInfoSet */
167         cloneTheDataInfoSet(pSource);
168     }
169 
170     /**
171      * Resolve editSetLinks.
172      *
173      * @param pEditSet the editSet
174      * @throws OceanusException on error
175      */
176     void resolveEditSetLinks(final PrometheusEditSet pEditSet) throws OceanusException {
177         /* Loop through the items */
178         for (MoneyWiseSecurityInfo myInfo : this) {
179             myInfo.resolveEditSetLinks(pEditSet);
180         }
181     }
182 
183     /**
184      * Obtain the region for the infoClass.
185      *
186      * @param pInfoClass the Info Class
187      * @return the deposit
188      */
189     public MoneyWiseRegion getRegion(final MoneyWiseAccountInfoClass pInfoClass) {
190         /* Access existing entry */
191         final MoneyWiseSecurityInfo myValue = getInfo(pInfoClass);
192 
193         /* If we have no entry, return null */
194         if (myValue == null) {
195             return null;
196         }
197 
198         /* Return the region */
199         return myValue.getRegion();
200     }
201 
202     /**
203      * Obtain the security for the infoClass.
204      *
205      * @param pInfoClass the Info Class
206      * @return the security
207      */
208     public MoneyWiseSecurity getSecurity(final MoneyWiseAccountInfoClass pInfoClass) {
209         /* Access existing entry */
210         final MoneyWiseSecurityInfo myValue = getInfo(pInfoClass);
211 
212         /* If we have no entry, return null */
213         if (myValue == null) {
214             return null;
215         }
216 
217         /* Return the security */
218         return myValue.getSecurity();
219     }
220 }