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.MoneyWisePortfolioInfo.MoneyWisePortfolioInfoList;
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   * PortfolioInfoSet class.
36   *
37   * @author Tony Washer
38   */
39  public class MoneyWisePortfolioInfoSet
40          extends PrometheusDataInfoSet<MoneyWisePortfolioInfo> {
41      /**
42       * Report fields.
43       */
44      private static final MetisFieldSet<MoneyWisePortfolioInfoSet> FIELD_DEFS = MetisFieldSet.newFieldSet(MoneyWisePortfolioInfoSet.class);
45  
46      /**
47       * FieldSet map.
48       */
49      private static final Map<MetisDataFieldId, MoneyWiseAccountInfoClass> FIELDSET_MAP = FIELD_DEFS.buildFieldMap(MoneyWiseAccountInfoClass.class, MoneyWisePortfolioInfoSet::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 MoneyWisePortfolioInfoSet(final MoneyWisePortfolio pOwner,
64                                          final MoneyWiseAccountInfoTypeList pTypeList,
65                                          final MoneyWisePortfolioInfoList 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 MoneyWisePortfolio getOwner() {
77          return (MoneyWisePortfolio) 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         /* Return the value */
105         final Object myValue = getField(pInfoClass);
106         return myValue != null
107                 ? myValue
108                 : MetisDataFieldValue.SKIP;
109     }
110 
111     /**
112      * Obtain the class of the field if it is an infoSet field.
113      *
114      * @param pField the field
115      * @return the class
116      */
117     public static MoneyWiseAccountInfoClass getClassForField(final MetisDataFieldId pField) {
118         /* Look up field in map */
119         return FIELDSET_MAP.get(pField);
120     }
121 
122     /**
123      * Obtain the field for the infoSet class.
124      *
125      * @param pClass the class
126      * @return the field
127      */
128     public static MetisDataFieldId getFieldForClass(final MoneyWiseAccountInfoClass pClass) {
129         /* Look up field in map */
130         return REVERSE_FIELDMAP.get(pClass);
131     }
132 
133     @Override
134     public MetisDataFieldId getFieldForClass(final PrometheusDataInfoClass pClass) {
135         return getFieldForClass((MoneyWiseAccountInfoClass) pClass);
136     }
137 
138     @Override
139     public Iterator<PrometheusDataInfoClass> classIterator() {
140         final PrometheusDataInfoClass[] myValues = MoneyWiseAccountInfoClass.values();
141         return Arrays.stream(myValues).iterator();
142     }
143 
144     /**
145      * Clone the dataInfoSet.
146      *
147      * @param pSource the InfoSet to clone
148      */
149     protected void cloneDataInfoSet(final MoneyWisePortfolioInfoSet pSource) {
150         /* Clone the dataInfoSet */
151         cloneTheDataInfoSet(pSource);
152     }
153 
154     /**
155      * Resolve editSetLinks.
156      *
157      * @param pEditSet the editSet
158      * @throws OceanusException on error
159      */
160     void resolveEditSetLinks(final PrometheusEditSet pEditSet) throws OceanusException {
161         /* Loop through the items */
162         for (MoneyWisePortfolioInfo myInfo : this) {
163             myInfo.resolveEditSetLinks(pEditSet);
164         }
165     }
166 }