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.atlas.data.analysis.analyse;
18  
19  import io.github.tonywasher.joceanus.oceanus.date.OceanusDate;
20  import io.github.tonywasher.joceanus.oceanus.date.OceanusDateRange;
21  import io.github.tonywasher.joceanus.oceanus.event.OceanusEventManager;
22  import io.github.tonywasher.joceanus.oceanus.event.OceanusEventRegistrar;
23  import io.github.tonywasher.joceanus.oceanus.event.OceanusEventRegistrar.OceanusEventProvider;
24  import io.github.tonywasher.joceanus.oceanus.format.OceanusDataFormatter;
25  import io.github.tonywasher.joceanus.metis.data.MetisDataItem.MetisDataMap;
26  import io.github.tonywasher.joceanus.metis.field.MetisFieldItem;
27  import io.github.tonywasher.joceanus.metis.field.MetisFieldSet;
28  import io.github.tonywasher.joceanus.moneywise.atlas.data.analysis.buckets.MoneyWiseXAnalysis;
29  import io.github.tonywasher.joceanus.moneywise.atlas.data.analysis.buckets.MoneyWiseXAnalysisBucketResource;
30  import io.github.tonywasher.joceanus.prometheus.views.PrometheusDataEvent;
31  
32  import java.util.HashMap;
33  import java.util.Map;
34  
35  /**
36   * Analysis manager.
37   */
38  public class MoneyWiseXAnalysisManager
39          implements OceanusEventProvider<PrometheusDataEvent>, MetisFieldItem, MetisDataMap<OceanusDateRange, MoneyWiseXAnalysis> {
40      /**
41       * Local Report fields.
42       */
43      private static final MetisFieldSet<MoneyWiseXAnalysisManager> FIELD_DEFS = MetisFieldSet.newFieldSet(MoneyWiseXAnalysisManager.class);
44  
45      /*
46       * Declare Fields.
47       */
48      static {
49          FIELD_DEFS.declareLocalField(MoneyWiseXAnalysisBucketResource.ANALYSIS_NAME, MoneyWiseXAnalysisManager::getAnalysis);
50      }
51  
52      /**
53       * The Event Manager.
54       */
55      private final OceanusEventManager<PrometheusDataEvent> theEventManager;
56  
57      /**
58       * The analysis map.
59       */
60      private final Map<OceanusDateRange, MoneyWiseXAnalysis> theAnalysisMap;
61  
62      /**
63       * The base analysis.
64       */
65      private MoneyWiseXAnalysis theAnalysis;
66  
67      /**
68       * Constructor.
69       *
70       * @param pAnalysis the new analysis
71       */
72      public MoneyWiseXAnalysisManager(final MoneyWiseXAnalysis pAnalysis) {
73          /* Store the parameters */
74          theAnalysis = pAnalysis;
75  
76          /* Create the event manager */
77          theEventManager = new OceanusEventManager<>();
78  
79          /* Create the analysis map */
80          theAnalysisMap = new HashMap<>();
81      }
82  
83      @Override
84      public MetisFieldSet<MoneyWiseXAnalysisManager> getDataFieldSet() {
85          return FIELD_DEFS;
86      }
87  
88      @Override
89      public OceanusEventRegistrar<PrometheusDataEvent> getEventRegistrar() {
90          return theEventManager.getEventRegistrar();
91      }
92  
93      @Override
94      public String formatObject(final OceanusDataFormatter pFormatter) {
95          return getClass().getSimpleName();
96      }
97  
98      @Override
99      public Map<OceanusDateRange, MoneyWiseXAnalysis> getUnderlyingMap() {
100         return theAnalysisMap;
101     }
102 
103     /**
104      * Is the analysis manager idle?
105      *
106      * @return true/false
107      */
108     public boolean isIdle() {
109         return theAnalysis.isIdle();
110     }
111 
112     /**
113      * Set analysis.
114      *
115      * @param pAnalysis the analysis
116      */
117     public void setAnalysis(final MoneyWiseXAnalysis pAnalysis) {
118         /* Record analysis and clear the map */
119         theAnalysis = pAnalysis;
120         theAnalysisMap.clear();
121 
122         /* Report to listeners */
123         theEventManager.fireEvent(PrometheusDataEvent.DATACHANGED);
124     }
125 
126     /**
127      * Obtain the base analysis.
128      *
129      * @return the base analysis
130      */
131     public MoneyWiseXAnalysis getAnalysis() {
132         return theAnalysis;
133     }
134 
135     /**
136      * Do we have a foreign currency?
137      *
138      * @return true/false
139      */
140     public Boolean haveForeignCurrency() {
141         return theAnalysis.haveForeignCurrency();
142     }
143 
144     /**
145      * Do we have active securities?
146      *
147      * @return true/false
148      */
149     public Boolean haveActiveSecurities() {
150         return theAnalysis.haveActiveSecurities();
151     }
152 
153     /**
154      * Obtain an analysis for a date.
155      *
156      * @param pDate the date for the analysis.
157      * @return the analysis
158      */
159     public MoneyWiseXAnalysis getDatedAnalysis(final OceanusDate pDate) {
160         /* Create the new Range */
161         final OceanusDateRange myRange = new OceanusDateRange(null, pDate);
162 
163         /* Look for the existing analysis */
164         return theAnalysisMap.computeIfAbsent(myRange, r -> {
165             /* Create the new event analysis */
166             final MoneyWiseXAnalysis myAnalysis = new MoneyWiseXAnalysis(theAnalysis, pDate);
167             myAnalysis.produceTotals();
168 
169             /* Check the totals */
170             myAnalysis.checkTotals();
171 
172             /* Put it into the map */
173             return myAnalysis;
174         });
175     }
176 
177     /**
178      * Obtain an analysis for a range.
179      *
180      * @param pRange the date range for the analysis.
181      * @return the analysis
182      */
183     public MoneyWiseXAnalysis getRangedAnalysis(final OceanusDateRange pRange) {
184         /* Look for the existing analysis */
185         return theAnalysisMap.computeIfAbsent(pRange, r -> {
186             /* Create the new event analysis */
187             final MoneyWiseXAnalysis myAnalysis = new MoneyWiseXAnalysis(theAnalysis, r);
188             myAnalysis.produceTotals();
189 
190             /* Check the totals */
191             myAnalysis.checkTotals();
192 
193             /* Put it into the map */
194             return myAnalysis;
195         });
196     }
197 
198     /**
199      * Analyse the base analysis.
200      */
201     public void analyseBase() {
202         /* Produce totals for the base analysis */
203         theAnalysis.produceTotals();
204     }
205 }