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.MoneyWiseTransCategory;
20  import io.github.tonywasher.joceanus.oceanus.format.OceanusDataFormatter;
21  
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.Iterator;
25  import java.util.List;
26  import java.util.Objects;
27  
28  /**
29   * Parent category registration.
30   *
31   * @author Tony Washer
32   */
33  public class MoneyWiseQIFParentCategory
34          implements Comparable<MoneyWiseQIFParentCategory> {
35      /**
36       * Self definition.
37       */
38      private final MoneyWiseQIFEventCategory theSelf;
39  
40      /**
41       * Children.
42       */
43      private final List<MoneyWiseQIFEventCategory> theChildren;
44  
45      /**
46       * Constructor.
47       *
48       * @param pParent the parent category
49       */
50      protected MoneyWiseQIFParentCategory(final MoneyWiseTransCategory pParent) {
51          this(new MoneyWiseQIFEventCategory(pParent));
52      }
53  
54      /**
55       * Constructor.
56       *
57       * @param pParent the parent category
58       */
59      protected MoneyWiseQIFParentCategory(final MoneyWiseQIFEventCategory pParent) {
60          /* Record self definition */
61          theSelf = pParent;
62  
63          /* Create child list */
64          theChildren = new ArrayList<>();
65      }
66  
67      @Override
68      public String toString() {
69          return theSelf.toString();
70      }
71  
72      /**
73       * Obtain number of children.
74       *
75       * @return the number of children
76       */
77      protected int numChildren() {
78          return theChildren.size();
79      }
80  
81      /**
82       * Obtain the security.
83       *
84       * @return the security
85       */
86      public MoneyWiseQIFEventCategory getParent() {
87          return theSelf;
88      }
89  
90      /**
91       * Obtain the children.
92       *
93       * @return the children
94       */
95      public List<MoneyWiseQIFEventCategory> getChildren() {
96          return theChildren;
97      }
98  
99      /**
100      * Register child.
101      *
102      * @param pChild the child
103      */
104     protected void registerChild(final MoneyWiseQIFEventCategory pChild) {
105         /* Add the child */
106         theChildren.add(pChild);
107     }
108 
109     /**
110      * Sort the children.
111      */
112     protected void sortChildren() {
113         Collections.sort(theChildren);
114     }
115 
116     /**
117      * Format record.
118      *
119      * @param pFormatter the data formatter
120      * @param pBuilder   the string builder
121      */
122     public void formatRecord(final OceanusDataFormatter pFormatter,
123                              final StringBuilder pBuilder) {
124         /* Format own record */
125         theSelf.formatRecord(pFormatter, pBuilder);
126 
127         /* Loop through the children */
128         final Iterator<MoneyWiseQIFEventCategory> myIterator = theChildren.iterator();
129         while (myIterator.hasNext()) {
130             final MoneyWiseQIFEventCategory myCategory = myIterator.next();
131 
132             /* Format the child */
133             myCategory.formatRecord(pFormatter, pBuilder);
134         }
135     }
136 
137     @Override
138     public boolean equals(final Object pThat) {
139         /* Handle trivial cases */
140         if (this == pThat) {
141             return true;
142         }
143         if (pThat == null) {
144             return false;
145         }
146 
147         /* Check class */
148         if (!getClass().equals(pThat.getClass())) {
149             return false;
150         }
151 
152         /* Cast correctly */
153         final MoneyWiseQIFParentCategory myParent = (MoneyWiseQIFParentCategory) pThat;
154 
155         /* Check parent */
156         if (!theSelf.equals(myParent.getParent())) {
157             return false;
158         }
159 
160         /* Check children */
161         return theChildren.equals(myParent.getChildren());
162     }
163 
164     @Override
165     public int hashCode() {
166         return Objects.hash(theSelf, theChildren);
167     }
168 
169     @Override
170     public int compareTo(final MoneyWiseQIFParentCategory pThat) {
171         return theSelf.compareTo(pThat.getParent());
172     }
173 }