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