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