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.quicken.file;
18  
19  import java.util.EnumMap;
20  import java.util.Iterator;
21  import java.util.List;
22  import java.util.Map;
23  import java.util.Map.Entry;
24  
25  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseSecurity;
26  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseSecurityClass;
27  import io.github.tonywasher.joceanus.moneywise.quicken.definitions.MoneyWiseQSecurityLineType;
28  import io.github.tonywasher.joceanus.moneywise.quicken.file.MoneyWiseQIFLine.MoneyWiseQIFStringLine;
29  
30  /**
31   * Class representing a QIF Security record.
32   */
33  public class MoneyWiseQIFSecurity
34          extends MoneyWiseQIFRecord<MoneyWiseQSecurityLineType>
35          implements Comparable<MoneyWiseQIFSecurity> {
36      /**
37       * Item type.
38       */
39      protected static final String QIF_ITEM = "Security";
40  
41      /**
42       * Category Map.
43       */
44      protected static final Map<MoneyWiseSecurityClass, String> QIF_ACTCATMAP = createClassMap();
45  
46      /**
47       * The Security.
48       */
49      private final String theName;
50  
51      /**
52       * The Symbol.
53       */
54      private final String theSymbol;
55  
56      /**
57       * The SecurityTypeClass.
58       */
59      private final MoneyWiseSecurityClass theClass;
60  
61      /**
62       * Constructor.
63       *
64       * @param pFile     the QIF File
65       * @param pSecurity the Security
66       */
67      public MoneyWiseQIFSecurity(final MoneyWiseQIFFile pFile,
68                                  final MoneyWiseSecurity pSecurity) {
69          /* Call super-constructor */
70          super(pFile, MoneyWiseQSecurityLineType.class);
71  
72          /* Store data */
73          theName = pSecurity.getName();
74          theSymbol = pSecurity.getSymbol();
75          theClass = pSecurity.getCategoryClass();
76  
77          /* Build lines */
78          addLine(new MoneyWiseQIFSecurityNameLine(theName));
79          addLine(new MoneyWiseQIFSecuritySymbolLine(theSymbol));
80          addLine(new MoneyWiseQIFSecurityTypeLine(theClass));
81      }
82  
83      /**
84       * Constructor.
85       *
86       * @param pFile  the QIF File
87       * @param pLines the data lines
88       */
89      protected MoneyWiseQIFSecurity(final MoneyWiseQIFFile pFile,
90                                     final List<String> pLines) {
91          /* Call super-constructor */
92          super(pFile, MoneyWiseQSecurityLineType.class);
93  
94          /* Determine details */
95          String myName = null;
96          String mySymbol = null;
97          MoneyWiseSecurityClass myClass = null;
98  
99          /* Loop through the lines */
100         final Iterator<String> myIterator = pLines.iterator();
101         while (myIterator.hasNext()) {
102             final String myLine = myIterator.next();
103 
104             /* Determine the category */
105             final MoneyWiseQSecurityLineType myType = MoneyWiseQSecurityLineType.parseLine(myLine);
106             if (myType != null) {
107                 /* Access data */
108                 final String myData = myLine.substring(myType.getSymbol().length());
109 
110                 /* Switch on line type */
111                 switch (myType) {
112                     case NAME:
113                         addLine(new MoneyWiseQIFSecurityNameLine(myData));
114                         myName = myData;
115                         break;
116                     case SYMBOL:
117                         addLine(new MoneyWiseQIFSecuritySymbolLine(myData));
118                         mySymbol = myData;
119                         break;
120                     case SECTYPE:
121                         final MoneyWiseQIFSecurityTypeLine myQLine = new MoneyWiseQIFSecurityTypeLine(myData);
122                         addLine(myQLine);
123                         myClass = myQLine.getSecurityClass();
124                         break;
125                     default:
126                         break;
127                 }
128             }
129         }
130 
131         /* Build details */
132         theName = myName;
133         theSymbol = mySymbol;
134         theClass = myClass;
135     }
136 
137     @Override
138     public String toString() {
139         return getName();
140     }
141 
142     /**
143      * Obtain the Name.
144      *
145      * @return the Name
146      */
147     public String getName() {
148         return theName;
149     }
150 
151     /**
152      * Obtain the symbol.
153      *
154      * @return the Name
155      */
156     public String getSymbol() {
157         return theSymbol;
158     }
159 
160     /**
161      * Create the CategoryClass to type map.
162      *
163      * @return the map
164      */
165     private static Map<MoneyWiseSecurityClass, String> createClassMap() {
166         /* Create the map */
167         final Map<MoneyWiseSecurityClass, String> myMap = new EnumMap<>(MoneyWiseSecurityClass.class);
168 
169         /* Add the entries */
170         myMap.put(MoneyWiseSecurityClass.SHARES, "Share");
171         myMap.put(MoneyWiseSecurityClass.GROWTHUNITTRUST, "Unit/Inv. Trust");
172         myMap.put(MoneyWiseSecurityClass.INCOMEUNITTRUST, "Unit/Inv. Trust");
173         myMap.put(MoneyWiseSecurityClass.LIFEBOND, "Bond");
174         myMap.put(MoneyWiseSecurityClass.ASSET, "Asset");
175         myMap.put(MoneyWiseSecurityClass.ENDOWMENT, "Trust");
176         myMap.put(MoneyWiseSecurityClass.VEHICLE, "Vehicle");
177         myMap.put(MoneyWiseSecurityClass.PROPERTY, "Real Estate");
178 
179         /* Return the map */
180         return myMap;
181     }
182 
183     @Override
184     public int compareTo(final MoneyWiseQIFSecurity pThat) {
185         return theName.compareTo(pThat.getName());
186     }
187 
188     /**
189      * The Security Name line.
190      */
191     public class MoneyWiseQIFSecurityNameLine
192             extends MoneyWiseQIFStringLine<MoneyWiseQSecurityLineType> {
193         /**
194          * Constructor.
195          *
196          * @param pName the Name
197          */
198         protected MoneyWiseQIFSecurityNameLine(final String pName) {
199             /* Call super-constructor */
200             super(pName);
201         }
202 
203         @Override
204         public MoneyWiseQSecurityLineType getLineType() {
205             return MoneyWiseQSecurityLineType.NAME;
206         }
207 
208         /**
209          * Obtain name.
210          *
211          * @return the name
212          */
213         public String getName() {
214             return getValue();
215         }
216     }
217 
218     /**
219      * The Security Symbol line.
220      */
221     public class MoneyWiseQIFSecuritySymbolLine
222             extends MoneyWiseQIFStringLine<MoneyWiseQSecurityLineType> {
223         /**
224          * Constructor.
225          *
226          * @param pSymbol the Symbol
227          */
228         protected MoneyWiseQIFSecuritySymbolLine(final String pSymbol) {
229             /* Call super-constructor */
230             super(pSymbol);
231         }
232 
233         @Override
234         public MoneyWiseQSecurityLineType getLineType() {
235             return MoneyWiseQSecurityLineType.SYMBOL;
236         }
237 
238         /**
239          * Obtain symbol.
240          *
241          * @return the symbol
242          */
243         public String getSymbol() {
244             return getValue();
245         }
246     }
247 
248     /**
249      * The Security Type line.
250      */
251     public class MoneyWiseQIFSecurityTypeLine
252             extends MoneyWiseQIFStringLine<MoneyWiseQSecurityLineType> {
253         /**
254          * The Security Type Class.
255          */
256         private final MoneyWiseSecurityClass theClass;
257 
258         /**
259          * Constructor.
260          *
261          * @param pClass the Security Class
262          */
263         protected MoneyWiseQIFSecurityTypeLine(final MoneyWiseSecurityClass pClass) {
264             /* Call super-constructor */
265             super(QIF_ACTCATMAP.get(pClass));
266 
267             /* Record the class */
268             theClass = pClass;
269         }
270 
271         /**
272          * Constructor.
273          *
274          * @param pType the Security Type
275          */
276         protected MoneyWiseQIFSecurityTypeLine(final String pType) {
277             /* Call super-constructor */
278             super(pType);
279 
280             /* Loop through the map entries */
281             MoneyWiseSecurityClass myClass = null;
282             final Iterator<Entry<MoneyWiseSecurityClass, String>> myIterator = QIF_ACTCATMAP.entrySet().iterator();
283             while (myIterator.hasNext()) {
284                 final Entry<MoneyWiseSecurityClass, String> myEntry = myIterator.next();
285 
286                 /* If we have a match */
287                 if (pType.equals(myEntry.getValue())) {
288                     myClass = myEntry.getKey();
289                     break;
290                 }
291             }
292 
293             /* Store the class */
294             theClass = myClass;
295         }
296 
297         @Override
298         public MoneyWiseQSecurityLineType getLineType() {
299             return MoneyWiseQSecurityLineType.SECTYPE;
300         }
301 
302         @Override
303         public String toString() {
304             return theClass.toString();
305         }
306 
307         /**
308          * Obtain security class.
309          *
310          * @return the security class
311          */
312         public MoneyWiseSecurityClass getSecurityClass() {
313             return theClass;
314         }
315     }
316 }