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.ArrayList;
20  import java.util.Collections;
21  import java.util.Iterator;
22  import java.util.List;
23  
24  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseTransAsset;
25  
26  /**
27   * Class representing an account and its events.
28   */
29  public class MoneyWiseQIFAccountEvents
30          implements Comparable<MoneyWiseQIFAccountEvents> {
31      /**
32       * Account.
33       */
34      private final MoneyWiseQIFAccount theAccount;
35  
36      /**
37       * Events.
38       */
39      private final List<MoneyWiseQIFEventRecord<?>> theEvents;
40  
41      /**
42       * Constructor.
43       *
44       * @param pFile    the QIF file.
45       * @param pAccount the account.
46       */
47      protected MoneyWiseQIFAccountEvents(final MoneyWiseQIFFile pFile,
48                                          final MoneyWiseTransAsset pAccount) {
49          /* Store parameters */
50          theAccount = new MoneyWiseQIFAccount(pFile, pAccount);
51  
52          /* Create the list */
53          theEvents = new ArrayList<>();
54      }
55  
56      /**
57       * Constructor.
58       *
59       * @param pFile    the QIF file.
60       * @param pAccount the account name.
61       */
62      protected MoneyWiseQIFAccountEvents(final MoneyWiseQIFFile pFile,
63                                          final String pAccount) {
64          /* Store parameters */
65          theAccount = new MoneyWiseQIFAccount(pFile, pAccount);
66  
67          /* Create the list */
68          theEvents = new ArrayList<>();
69      }
70  
71      /**
72       * Constructor.
73       *
74       * @param pAccount the account.
75       */
76      protected MoneyWiseQIFAccountEvents(final MoneyWiseQIFAccount pAccount) {
77          /* Store parameters */
78          theAccount = pAccount;
79  
80          /* Create the list */
81          theEvents = new ArrayList<>();
82      }
83  
84      /**
85       * Obtain the account.
86       *
87       * @return the account
88       */
89      public MoneyWiseQIFAccount getAccount() {
90          return theAccount;
91      }
92  
93      /**
94       * Obtain the events.
95       *
96       * @return the events
97       */
98      public List<MoneyWiseQIFEventRecord<?>> getEvents() {
99          return theEvents;
100     }
101 
102     /**
103      * Event iterator.
104      *
105      * @return the iterator
106      */
107     public Iterator<MoneyWiseQIFEventRecord<?>> eventIterator() {
108         return theEvents.iterator();
109     }
110 
111     /**
112      * Add event.
113      *
114      * @param pEvent the event record set
115      */
116     protected void addEvent(final MoneyWiseQIFEventRecord<?> pEvent) {
117         /* Add the event */
118         theEvents.add(pEvent);
119     }
120 
121     /**
122      * Sort the events.
123      */
124     protected void sortEvents() {
125         Collections.sort(theEvents);
126     }
127 
128     @Override
129     public boolean equals(final Object pThat) {
130         /* Handle trivial cases */
131         if (this == pThat) {
132             return true;
133         }
134         if (pThat == null) {
135             return false;
136         }
137 
138         /* Check class */
139         if (!getClass().equals(pThat.getClass())) {
140             return false;
141         }
142 
143         /* Cast correctly */
144         final MoneyWiseQIFAccountEvents myEvents = (MoneyWiseQIFAccountEvents) pThat;
145 
146         /* Check account */
147         if (!theAccount.equals(myEvents.getAccount())) {
148             return false;
149         }
150 
151         /* Check events */
152         return theEvents.equals(myEvents.getEvents());
153     }
154 
155     @Override
156     public int hashCode() {
157         final int myResult = MoneyWiseQIFFile.HASH_BASE * theAccount.hashCode();
158         return myResult + theEvents.hashCode();
159     }
160 
161     @Override
162     public int compareTo(final MoneyWiseQIFAccountEvents pThat) {
163         return theAccount.compareTo(pThat.getAccount());
164     }
165 }