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