2 * Copyright (C) 2017 - 2022 Free Software Foundation
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.
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.
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/>.
17 package org.mxchange.jfinancials.model.receipt;
19 import java.io.Serializable;
20 import java.text.MessageFormat;
21 import java.util.Objects;
24 * A utilities class for receipts
26 * @author Roland Häder<roland@mxchange.org>
28 public class Receipts implements Serializable {
33 private static final long serialVersionUID = 2_867_938_676_165_401L;
36 * Compares both receipt instances. This method returns -1 if second
39 * @param receipt1 BillableReceipt instance 1
40 * @param receipt2 BillableReceipt instance 2
42 * @return Comparison value
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)) {
49 } else if (null == receipt1) {
52 } else if (null == receipt2) {
57 // Invoke compareTo() method
58 return receipt1.compareTo(receipt2);
62 * Copies all fields from source receipt to target receipt.
64 * @param sourceReceipt Source receipt instance
65 * @param targetReceipt Target Receipt instance
67 public static void copyReceiptData (final BillableReceipt sourceReceipt, final BillableReceipt targetReceipt) {
68 // Both should not be null
69 if (null == sourceReceipt) {
71 throw new NullPointerException("sourceReceipt is null"); //NOI18N
72 } else if (null == targetReceipt) {
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
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.setReceiptQrCode(sourceReceipt.getReceiptQrCode());
89 targetReceipt.setReceiptRegisterNumber(sourceReceipt.getReceiptRegisterNumber());
90 targetReceipt.setReceiptSellerEmployee(sourceReceipt.getReceiptSellerEmployee());
91 targetReceipt.setReceiptSequenceNumber(sourceReceipt.getReceiptSequenceNumber());
92 targetReceipt.setReceiptTransactionNumber(sourceReceipt.getReceiptTransactionNumber());
93 targetReceipt.setReceiptUser(sourceReceipt.getReceiptUser());
97 * Checks whether both receipts are the same, means not true entity equality
98 * but if the receipt in general terms does already exist. A pre-check on
99 * entity equality is however performed to avoid below much longer code.
101 * @param receipt1 BillableReceipt instance 1
102 * @param receipt2 BillableReceipt instance 2
104 * @return Whether both receipts are the same (not instances)
106 public static boolean isSameReceipt (final BillableReceipt receipt1, final BillableReceipt receipt2) {
107 // Pre-compare both as entities (same id)
108 if (Objects.equals(receipt1, receipt2)) {
109 // Same entity (with id number)
113 // Validate parameter
114 if (null == receipt1) {
116 throw new NullPointerException("receipt1 is null"); //NOI18N
117 } else if ((receipt1.getReceiptId() instanceof Long) && (receipt1.getReceiptId() < 1)) {
119 throw new IllegalArgumentException(MessageFormat.format("receipt1.receiptId={0} is not valid.", receipt1.getReceiptId())); //NOI18N
120 } else if (receipt1.getReceiptBranchOffice() == null) {
122 throw new NullPointerException("receipt1.receiptBranchOffice is null"); //NOI18N
123 } else if (receipt1.getReceiptBranchOffice().getBranchId() == null) {
125 throw new NullPointerException("receipt1.receiptBranchOffice.branchId is null"); //NOI18N
126 } else if (receipt1.getReceiptBranchOffice().getBranchId() < 1) {
128 throw new NullPointerException(MessageFormat.format("receipt1.receiptBranchOffice.branchId={0} is not valid", receipt1.getReceiptBranchOffice().getBranchId())); //NOI18N
129 } else if ((receipt1.getReceiptNumber() != null) && (receipt1.getReceiptNumber().isEmpty())) {
131 throw new IllegalArgumentException("receipt1.receiptNumber is empty."); //NOI18N
132 } else if (receipt1.getReceiptPaymentType() == null) {
134 throw new NullPointerException("receipt1.receiptPaymentType is null"); //NOI18N
135 } else if (null == receipt2) {
137 throw new NullPointerException("receipt2 is null"); //NOI18N
138 } else if ((receipt2.getReceiptId() instanceof Long) && (receipt2.getReceiptId() < 1)) {
140 throw new IllegalArgumentException(MessageFormat.format("receipt2.receiptId={0} is not valid.", receipt2.getReceiptId())); //NOI18N
141 } else if (receipt2.getReceiptBranchOffice() == null) {
143 throw new NullPointerException("receipt2.receiptBranchOffice is null"); //NOI18N
144 } else if (receipt2.getReceiptBranchOffice().getBranchId() == null) {
146 throw new NullPointerException("receipt2.receiptBranchOffice.branchId is null"); //NOI18N
147 } else if (receipt2.getReceiptBranchOffice().getBranchId() < 1) {
149 throw new NullPointerException(MessageFormat.format("receipt2.receiptBranchOffice.branchId={0} is not valid", receipt2.getReceiptBranchOffice().getBranchId())); //NOI18N
150 } else if ((receipt2.getReceiptNumber() != null) && (receipt2.getReceiptNumber().isEmpty())) {
152 throw new IllegalArgumentException("receipt2.receiptNumber is empty."); //NOI18N
153 } else if (receipt2.getReceiptPaymentType() == null) {
155 throw new NullPointerException("receipt2.receiptPaymentType is null"); //NOI18N
158 // Now check all individually
159 if (!Objects.equals(receipt1.getReceiptBranchOffice(), receipt2.getReceiptBranchOffice())) {
160 // Other branch offices
162 } else if (!Objects.equals(receipt1.getReceiptNumber(), receipt2.getReceiptNumber())) {
163 // Other receipt number
165 } else if (!Objects.equals(receipt1.getReceiptIssued(), receipt2.getReceiptIssued())) {
168 } else if (!Objects.equals(receipt1.getReceiptUser(), receipt2.getReceiptUser())) {
169 // Other user (unlikely to happen
173 // Maybe same receipt!
178 * Private default constructor
180 private Receipts () {
181 // Utilities don't have instances