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.oceanus.format.OceanusDataFormatter;
20  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseSecurity;
21  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseSecurityPrice;
22  
23  import java.util.ArrayList;
24  import java.util.Collections;
25  import java.util.List;
26  
27  /**
28   * Security Price List.
29   *
30   * @author Tony Washer
31   */
32  public class MoneyWiseQIFSecurityPrices
33          implements Comparable<MoneyWiseQIFSecurityPrices> {
34      /**
35       * The QIF File.
36       */
37      private final MoneyWiseQIFFile theFile;
38  
39      /**
40       * The Security.
41       */
42      private final MoneyWiseQIFSecurity theSecurity;
43  
44      /**
45       * The Price List.
46       */
47      private final List<MoneyWiseQIFPrice> thePrices;
48  
49      /**
50       * Constructor.
51       *
52       * @param pFile     the QIF File
53       * @param pSecurity the security.
54       */
55      protected MoneyWiseQIFSecurityPrices(final MoneyWiseQIFFile pFile,
56                                           final MoneyWiseSecurity pSecurity) {
57          this(pFile, new MoneyWiseQIFSecurity(pFile, pSecurity));
58      }
59  
60      /**
61       * Constructor.
62       *
63       * @param pFile     the QIF File
64       * @param pSecurity the security.
65       */
66      protected MoneyWiseQIFSecurityPrices(final MoneyWiseQIFFile pFile,
67                                           final MoneyWiseQIFSecurity pSecurity) {
68          /* Store parameters */
69          theFile = pFile;
70          theSecurity = pSecurity;
71  
72          /* Create the list */
73          thePrices = new ArrayList<>();
74      }
75  
76      /**
77       * Obtain the security.
78       *
79       * @return the security
80       */
81      public MoneyWiseQIFSecurity getSecurity() {
82          return theSecurity;
83      }
84  
85      /**
86       * Obtain the prices.
87       *
88       * @return the prices
89       */
90      public List<MoneyWiseQIFPrice> getPrices() {
91          return thePrices;
92      }
93  
94      /**
95       * Add price.
96       *
97       * @param pPrice the price to add
98       */
99      protected void addPrice(final MoneyWiseSecurityPrice pPrice) {
100         /* Allocate price */
101         final MoneyWiseQIFPrice myPrice = new MoneyWiseQIFPrice(theFile, theSecurity, pPrice);
102 
103         /* Add to the list */
104         thePrices.add(myPrice);
105     }
106 
107     /**
108      * Add price.
109      *
110      * @param pPrice the price to add
111      */
112     protected void addPrice(final MoneyWiseQIFPrice pPrice) {
113         /* Add to the list */
114         thePrices.add(pPrice);
115     }
116 
117     /**
118      * Sort the prices.
119      */
120     protected void sortPrices() {
121         Collections.sort(thePrices);
122     }
123 
124     /**
125      * Format prices.
126      *
127      * @param pFormatter the formatter
128      * @param pBuilder   the string builder
129      */
130     protected void formatPrices(final OceanusDataFormatter pFormatter,
131                                 final StringBuilder pBuilder) {
132         /* Loop through the prices */
133         for (MoneyWiseQIFPrice myPrice : thePrices) {
134             /* Format Item Type header */
135             MoneyWiseQIFRecord.formatItemType(MoneyWiseQIFPrice.QIF_ITEM, pBuilder);
136 
137             /* Format the record */
138             myPrice.formatRecord(pFormatter, pBuilder);
139         }
140     }
141 
142     @Override
143     public boolean equals(final Object pThat) {
144         /* Handle trivial cases */
145         if (this == pThat) {
146             return true;
147         }
148         if (pThat == null) {
149             return false;
150         }
151 
152         /* Check class */
153         if (!getClass().equals(pThat.getClass())) {
154             return false;
155         }
156 
157         /* Cast correctly */
158         final MoneyWiseQIFSecurityPrices myPrices = (MoneyWiseQIFSecurityPrices) pThat;
159 
160         /* Check security */
161         if (!theSecurity.equals(myPrices.getSecurity())) {
162             return false;
163         }
164 
165         /* Check prices */
166         return thePrices.equals(myPrices.getPrices());
167     }
168 
169     @Override
170     public int hashCode() {
171         final int myResult = MoneyWiseQIFFile.HASH_BASE * theSecurity.hashCode();
172         return myResult + thePrices.hashCode();
173     }
174 
175     @Override
176     public int compareTo(final MoneyWiseQIFSecurityPrices pThat) {
177         return theSecurity.compareTo(pThat.getSecurity());
178     }
179 }