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