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