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.Iterator;
20  import java.util.List;
21  
22  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseTransTag;
23  import io.github.tonywasher.joceanus.moneywise.quicken.definitions.MoneyWiseQClassLineType;
24  import io.github.tonywasher.joceanus.moneywise.quicken.file.MoneyWiseQIFLine.MoneyWiseQIFStringLine;
25  
26  /**
27   * Class representing a QIF Class record.
28   */
29  public class MoneyWiseQIFClass
30          extends MoneyWiseQIFRecord<MoneyWiseQClassLineType>
31          implements Comparable<MoneyWiseQIFClass> {
32      /**
33       * Item type.
34       */
35      protected static final String QIF_ITEM = "Class";
36  
37      /**
38       * The Class Name.
39       */
40      private final String theName;
41  
42      /**
43       * The Class Description.
44       */
45      private final String theDesc;
46  
47      /**
48       * Constructor.
49       *
50       * @param pFile the QIF File
51       * @param pTag  the Event Tag
52       */
53      public MoneyWiseQIFClass(final MoneyWiseQIFFile pFile,
54                               final MoneyWiseTransTag pTag) {
55          /* Call super-constructor */
56          super(pFile, MoneyWiseQClassLineType.class);
57  
58          /* Store data */
59          theName = pTag.getName();
60          theDesc = pTag.getDesc();
61  
62          /* Build lines */
63          addLine(new MoneyWiseQIFClassNameLine(theName));
64          if (theDesc != null) {
65              addLine(new MoneyWiseQIFClassDescLine(theDesc));
66          }
67      }
68  
69      /**
70       * Constructor.
71       *
72       * @param pFile  the QIF File
73       * @param pLines the data lines
74       */
75      protected MoneyWiseQIFClass(final MoneyWiseQIFFile pFile,
76                                  final List<String> pLines) {
77          /* Call super-constructor */
78          super(pFile, MoneyWiseQClassLineType.class);
79  
80          /* Determine details */
81          String myName = null;
82          String myDesc = null;
83  
84          /* Loop through the lines */
85          final Iterator<String> myIterator = pLines.iterator();
86          while (myIterator.hasNext()) {
87              final String myLine = myIterator.next();
88  
89              /* Determine the category */
90              final MoneyWiseQClassLineType myType = MoneyWiseQClassLineType.parseLine(myLine);
91              if (myType != null) {
92                  /* Access data */
93                  final String myData = myLine.substring(myType.getSymbol().length());
94  
95                  /* Switch on line type */
96                  switch (myType) {
97                      case NAME:
98                          addLine(new MoneyWiseQIFClassNameLine(myData));
99                          myName = myData;
100                         break;
101                     case DESCRIPTION:
102                         addLine(new MoneyWiseQIFClassDescLine(myData));
103                         myDesc = myData;
104                         break;
105                     default:
106                         throw new IllegalArgumentException("Unexpected QClassLineType: " + myLine);
107                 }
108             }
109         }
110 
111         /* Build details */
112         theName = myName;
113         theDesc = myDesc;
114     }
115 
116     @Override
117     public String toString() {
118         return getName();
119     }
120 
121     /**
122      * Obtain the Name.
123      *
124      * @return the Name
125      */
126     public String getName() {
127         return theName;
128     }
129 
130     /**
131      * Obtain the Description.
132      *
133      * @return the description
134      */
135     public String getDesc() {
136         return theDesc;
137     }
138 
139     @Override
140     public int compareTo(final MoneyWiseQIFClass pThat) {
141         return theName.compareTo(pThat.getName());
142     }
143 
144     /**
145      * The Class Name line.
146      */
147     public class MoneyWiseQIFClassNameLine
148             extends MoneyWiseQIFStringLine<MoneyWiseQClassLineType> {
149         /**
150          * Constructor.
151          *
152          * @param pName the Name
153          */
154         protected MoneyWiseQIFClassNameLine(final String pName) {
155             /* Call super-constructor */
156             super(pName);
157         }
158 
159         @Override
160         public MoneyWiseQClassLineType getLineType() {
161             return MoneyWiseQClassLineType.NAME;
162         }
163 
164         /**
165          * Obtain name.
166          *
167          * @return the name
168          */
169         public String getName() {
170             return getValue();
171         }
172     }
173 
174     /**
175      * The Class Description line.
176      */
177     public class MoneyWiseQIFClassDescLine
178             extends MoneyWiseQIFStringLine<MoneyWiseQClassLineType> {
179         /**
180          * Constructor.
181          *
182          * @param pDesc the Description
183          */
184         protected MoneyWiseQIFClassDescLine(final String pDesc) {
185             /* Call super-constructor */
186             super(pDesc);
187         }
188 
189         @Override
190         public MoneyWiseQClassLineType getLineType() {
191             return MoneyWiseQClassLineType.DESCRIPTION;
192         }
193 
194         /**
195          * Obtain description.
196          *
197          * @return the description
198          */
199         public String getDescription() {
200             return getValue();
201         }
202     }
203 }