]> git.mxchange.org Git - jfinancials-core.git/blob - src/org/mxchange/jfinancials/model/utils/ReceiptUtils.java
704aa3288f6e16a3cb4a922155a129dea1175d7b
[jfinancials-core.git] / src / org / mxchange / jfinancials / model / utils / ReceiptUtils.java
1 /*
2  * Copyright (C) 2017 - 2024 Free Software Foundation
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Affero General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Affero General Public License for more details.
13  *
14  * You should have received a copy of the GNU Affero General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.jfinancials.model.utils;
18
19 import java.io.Serializable;
20 import java.text.MessageFormat;
21 import java.util.Objects;
22 import org.mxchange.jfinancials.model.receipt.BillableReceipt;
23
24 /**
25  * A utilities class for receipts
26  *
27  * @author Roland Häder<roland@mxchange.org>
28  */
29 public class ReceiptUtils implements Serializable {
30
31         /**
32          * Serial number
33          */
34         private static final long serialVersionUID = 2_867_938_676_165_401L;
35
36         /**
37          * Compares both receipt instances. This method returns -1 if second
38          * instance is null.
39          * <p>
40          * @param receipt1 BillableReceipt instance 1
41          * @param receipt2 BillableReceipt instance 2
42          * <p>
43          * @return Comparison value
44          */
45         public static int compare (final BillableReceipt receipt1, final BillableReceipt receipt2) {
46                 // Check equality, then at least first must be given
47                 if (Objects.equals(receipt1, receipt2)) {
48                         // Both are same
49                         return 0;
50                 } else if (null == receipt1) {
51                         // First is null
52                         return -1;
53                 } else if (null == receipt2) {
54                         // Second is null
55                         return 1;
56                 }
57
58                 // Invoke compareTo() method
59                 return receipt1.compareTo(receipt2);
60         }
61
62         /**
63          * Copies all fields from source receipt to target receipt.
64          * <p>
65          * @param sourceReceipt Source receipt instance
66          * @param targetReceipt Target Receipt instance
67          */
68         public static void copyReceiptData (final BillableReceipt sourceReceipt, final BillableReceipt targetReceipt) {
69                 // Both should not be null
70                 if (null == sourceReceipt) {
71                         // Throw NPE
72                         throw new NullPointerException("sourceReceipt is null"); //NOI18N
73                 } else if (null == targetReceipt) {
74                         // Throw NPE
75                         throw new NullPointerException("targetReceipt is null"); //NOI18N
76                 } else if (Objects.equals(sourceReceipt, targetReceipt)) {
77                         // Is exactly the same!
78                         throw new IllegalArgumentException("sourcerReceipt and targetReceipt are the same."); //NOI18N
79                 }
80
81                 // Copy all fields
82                 targetReceipt.setReceiptBarCodeNumber(sourceReceipt.getReceiptBarCodeNumber());
83                 targetReceipt.setReceiptBonusCard(sourceReceipt.getReceiptBonusCard());
84                 targetReceipt.setReceiptBranchOffice(sourceReceipt.getReceiptBranchOffice());
85                 targetReceipt.setReceiptId(sourceReceipt.getReceiptId());
86                 targetReceipt.setReceiptIssued(sourceReceipt.getReceiptIssued());
87                 targetReceipt.setReceiptNumber(sourceReceipt.getReceiptNumber());
88                 targetReceipt.setReceiptPaymentType(sourceReceipt.getReceiptPaymentType());
89                 targetReceipt.setReceiptQrCode(sourceReceipt.getReceiptQrCode());
90                 targetReceipt.setReceiptRegisterNumber(sourceReceipt.getReceiptRegisterNumber());
91                 targetReceipt.setReceiptSellerEmployee(sourceReceipt.getReceiptSellerEmployee());
92                 targetReceipt.setReceiptSequenceNumber(sourceReceipt.getReceiptSequenceNumber());
93                 targetReceipt.setReceiptTransactionNumber(sourceReceipt.getReceiptTransactionNumber());
94                 targetReceipt.setReceiptUser(sourceReceipt.getReceiptUser());
95         }
96
97         /**
98          * Checks whether both receipts are the same, means not true entity equality
99          * but if the receipt in general terms does already exist. A pre-check on
100          * entity equality is however performed to avoid below much longer code.
101          * <p>
102          * @param receipt1 BillableReceipt instance 1
103          * @param receipt2 BillableReceipt instance 2
104          * <p>
105          * @return Whether both receipts are the same (not instances)
106          */
107         public static boolean isSameReceipt (final BillableReceipt receipt1, final BillableReceipt receipt2) {
108                 // Pre-compare both as entities (same id)
109                 if (Objects.equals(receipt1, receipt2)) {
110                         // Same entity (with id number)
111                         return true;
112                 }
113
114                 // Validate parameter
115                 if (null == receipt1) {
116                         // Throw NPE
117                         throw new NullPointerException("receipt1 is null"); //NOI18N
118                 } else if ((receipt1.getReceiptId() instanceof Long) && (receipt1.getReceiptId() < 1)) {
119                         // Throw IAE
120                         throw new IllegalArgumentException(MessageFormat.format("receipt1.receiptId={0} is not valid.", receipt1.getReceiptId())); //NOI18N
121                 } else if (receipt1.getReceiptBranchOffice() == null) {
122                         // Throw NPE
123                         throw new NullPointerException("receipt1.receiptBranchOffice is null"); //NOI18N
124                 } else if (receipt1.getReceiptBranchOffice().getBranchId() == null) {
125                         // Throw NPE
126                         throw new NullPointerException("receipt1.receiptBranchOffice.branchId is null"); //NOI18N
127                 } else if (receipt1.getReceiptBranchOffice().getBranchId() < 1) {
128                         // Throw NPE
129                         throw new NullPointerException(MessageFormat.format("receipt1.receiptBranchOffice.branchId={0} is not valid", receipt1.getReceiptBranchOffice().getBranchId())); //NOI18N
130                 } else if ((receipt1.getReceiptNumber() != null) && (receipt1.getReceiptNumber().isEmpty())) {
131                         // Throw IAE
132                         throw new IllegalArgumentException("receipt1.receiptNumber is empty."); //NOI18N
133                 } else if (receipt1.getReceiptPaymentType() == null) {
134                         // Throw NPE
135                         throw new NullPointerException("receipt1.receiptPaymentType is null"); //NOI18N
136                 } else if (null == receipt2) {
137                         // Throw NPE
138                         throw new NullPointerException("receipt2 is null"); //NOI18N
139                 } else if ((receipt2.getReceiptId() instanceof Long) && (receipt2.getReceiptId() < 1)) {
140                         // Throw IAE
141                         throw new IllegalArgumentException(MessageFormat.format("receipt2.receiptId={0} is not valid.", receipt2.getReceiptId())); //NOI18N
142                 } else if (receipt2.getReceiptBranchOffice() == null) {
143                         // Throw NPE
144                         throw new NullPointerException("receipt2.receiptBranchOffice is null"); //NOI18N
145                 } else if (receipt2.getReceiptBranchOffice().getBranchId() == null) {
146                         // Throw NPE
147                         throw new NullPointerException("receipt2.receiptBranchOffice.branchId is null"); //NOI18N
148                 } else if (receipt2.getReceiptBranchOffice().getBranchId() < 1) {
149                         // Throw NPE
150                         throw new NullPointerException(MessageFormat.format("receipt2.receiptBranchOffice.branchId={0} is not valid", receipt2.getReceiptBranchOffice().getBranchId())); //NOI18N
151                 } else if ((receipt2.getReceiptNumber() != null) && (receipt2.getReceiptNumber().isEmpty())) {
152                         // Throw IAE
153                         throw new IllegalArgumentException("receipt2.receiptNumber is empty."); //NOI18N
154                 } else if (receipt2.getReceiptPaymentType() == null) {
155                         // Throw NPE
156                         throw new NullPointerException("receipt2.receiptPaymentType is null"); //NOI18N
157                 }
158
159                 // Now check all individually
160                 if (!Objects.equals(receipt1.getReceiptBranchOffice(), receipt2.getReceiptBranchOffice())) {
161                         // Other branch offices
162                         return false;
163                 } else if (!Objects.equals(receipt1.getReceiptNumber(), receipt2.getReceiptNumber())) {
164                         // Other receipt number
165                         return false;
166                 } else if (!Objects.equals(receipt1.getReceiptIssued(), receipt2.getReceiptIssued())) {
167                         // Other issue date
168                         return false;
169                 } else if (!Objects.equals(receipt1.getReceiptUser(), receipt2.getReceiptUser())) {
170                         // Other user (unlikely to happen
171                         return false;
172                 }
173
174                 // Maybe same receipt!
175                 return true;
176         }
177
178         /**
179          * Private default constructor
180          */
181         private ReceiptUtils () {
182                 // Utilities don't have instances
183         }
184
185 }