1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.github.tonywasher.joceanus.moneywise.quicken.file;
18
19 import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseSecurityPrice;
20 import io.github.tonywasher.joceanus.moneywise.quicken.definitions.MoneyWiseQIFType;
21 import io.github.tonywasher.joceanus.oceanus.date.OceanusDate;
22 import io.github.tonywasher.joceanus.oceanus.decimal.OceanusPrice;
23 import io.github.tonywasher.joceanus.oceanus.format.OceanusDataFormatter;
24
25 import java.util.ArrayList;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.Objects;
29
30
31
32
33 public class MoneyWiseQIFPrice
34 implements Comparable<MoneyWiseQIFPrice> {
35
36
37
38 protected static final String QIF_ITEM = "Prices";
39
40
41
42
43 private static final String QIF_QUOTE = "\"";
44
45
46
47
48 private static final String QIF_COMMA = ",";
49
50
51
52
53 private final MoneyWiseQIFType theFileType;
54
55
56
57
58 private final MoneyWiseQIFSecurity theSecurity;
59
60
61
62
63 private final OceanusDate theDate;
64
65
66
67
68 private final OceanusPrice thePrice;
69
70
71
72
73 private final List<MoneyWiseQIFPrice> thePrices;
74
75
76
77
78
79
80
81
82 protected MoneyWiseQIFPrice(final MoneyWiseQIFRegister pRegister,
83 final MoneyWiseQIFSecurity pSecurity,
84 final MoneyWiseSecurityPrice pPrice) {
85
86 theFileType = pRegister.getFileType();
87 theSecurity = pSecurity;
88 theDate = pPrice.getDate();
89 thePrice = pPrice.getPrice();
90 thePrices = null;
91 }
92
93
94
95
96
97
98
99
100 private MoneyWiseQIFPrice(final MoneyWiseQIFRegister pRegister,
101 final OceanusDataFormatter pFormatter,
102 final String pLine) {
103
104 final String[] myParts = pLine.split(QIF_COMMA);
105
106
107 for (int i = 0; i < myParts.length; i++) {
108 final String myStr = myParts[i];
109 if (myStr.startsWith(QIF_QUOTE)
110 && myStr.endsWith(QIF_QUOTE)) {
111 myParts[i] = myStr.substring(1, myStr.length() - 1);
112 }
113 }
114
115
116 theFileType = pRegister.getFileType();
117 theSecurity = pRegister.getSecurityBySymbol(myParts[0]);
118 theDate = pFormatter.getDateFormatter().parseDateBase(myParts[2], MoneyWiseQIFConstants.QIF_BASEYEAR);
119 thePrice = pFormatter.getDecimalParser().parsePriceValue(myParts[1]);
120 thePrices = null;
121 }
122
123
124
125
126
127
128
129
130 protected MoneyWiseQIFPrice(final MoneyWiseQIFRegister pRegister,
131 final OceanusDataFormatter pFormatter,
132 final List<String> pLines) {
133
134 thePrices = new ArrayList<>();
135
136
137 MoneyWiseQIFSecurity mySecurity = null;
138 for (String myLine : pLines) {
139
140 final MoneyWiseQIFPrice myPrice = new MoneyWiseQIFPrice(pRegister, pFormatter, myLine);
141 mySecurity = myPrice.getSecurity();
142 thePrices.add(myPrice);
143 }
144
145
146 theFileType = pRegister.getFileType();
147 theSecurity = mySecurity;
148 theDate = null;
149 thePrice = null;
150 }
151
152
153
154
155
156
157 public MoneyWiseQIFSecurity getSecurity() {
158 return theSecurity;
159 }
160
161
162
163
164
165
166 public OceanusDate getDate() {
167 return theDate;
168 }
169
170
171
172
173
174
175 public OceanusPrice getPrice() {
176 return thePrice;
177 }
178
179
180
181
182
183
184 public Iterator<MoneyWiseQIFPrice> priceIterator() {
185 return thePrices.iterator();
186 }
187
188
189
190
191
192
193
194 protected void formatRecord(final OceanusDataFormatter pFormatter,
195 final StringBuilder pBuilder) {
196
197 pBuilder.append(QIF_QUOTE);
198 pBuilder.append(theSecurity.getSymbol());
199 pBuilder.append(QIF_QUOTE);
200 pBuilder.append(QIF_COMMA);
201
202
203 if (theFileType.escapePrices()) {
204 pBuilder.append(QIF_QUOTE);
205 }
206 pBuilder.append(thePrice.toString());
207 if (theFileType.escapePrices()) {
208 pBuilder.append(QIF_QUOTE);
209 }
210 pBuilder.append(QIF_COMMA);
211
212
213 pBuilder.append(QIF_QUOTE);
214 pBuilder.append(pFormatter.formatObject(theDate));
215 pBuilder.append(QIF_QUOTE);
216 pBuilder.append(MoneyWiseQIFRecord.QIF_EOL);
217
218
219 pBuilder.append(MoneyWiseQIFRecord.QIF_EOI);
220 pBuilder.append(MoneyWiseQIFRecord.QIF_EOL);
221 }
222
223 @Override
224 public boolean equals(final Object pThat) {
225
226 if (this == pThat) {
227 return true;
228 }
229 if (pThat == null) {
230 return false;
231 }
232
233
234 if (!getClass().equals(pThat.getClass())) {
235 return false;
236 }
237
238
239 final MoneyWiseQIFPrice myLine = (MoneyWiseQIFPrice) pThat;
240
241
242 if (!getSecurity().equals(myLine.getSecurity())) {
243 return false;
244 }
245
246
247 if (!getPrice().equals(myLine.getPrice())) {
248 return false;
249 }
250
251
252 return theDate.equals(myLine.getDate());
253 }
254
255 @Override
256 public int hashCode() {
257 return Objects.hash(theSecurity, thePrice, theDate);
258 }
259
260 @Override
261 public int compareTo(final MoneyWiseQIFPrice pThat) {
262 return theDate.compareTo(pThat.getDate());
263 }
264 }