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.utils;
19 import java.io.Serializable;
20 import java.text.MessageFormat;
21 import java.util.Objects;
22 import org.mxchange.jfinancials.model.receipt_item.BillableReceiptItem;
25 * A utilities class for receipt items
27 * @author Roland Häder<roland@mxchange.org>
29 public class ReceiptItemUtils implements Serializable {
34 private static final long serialVersionUID = 2_867_938_676_165_402L;
37 * Copies all fields from source entity to target entity. An exception might
38 * be thrown when both instances are the same.
40 * @param sourceReceiptItem Source receipt item instance
41 * @param targetReceiptItem Target receipt item instance
43 public static void copyReceiptItemData (final BillableReceiptItem sourceReceiptItem, final BillableReceiptItem targetReceiptItem) {
45 if (null == sourceReceiptItem) {
47 throw new NullPointerException("sourceReceiptItem is null"); //NOI18N
48 } else if (null == targetReceiptItem) {
50 throw new NullPointerException("targetReceiptItem is null"); //NOI18N
51 } else if (Objects.equals(sourceReceiptItem, targetReceiptItem)) {
53 throw new IllegalArgumentException("sourceReceiptItem and targetReceiptItem are the same."); //NOI18N
57 targetReceiptItem.setItemBrandName(sourceReceiptItem.getItemBrandName());
58 targetReceiptItem.setItemCouponNumber(sourceReceiptItem.getItemCouponNumber());
59 targetReceiptItem.setItemGrossPrice(sourceReceiptItem.getItemGrossPrice());
60 targetReceiptItem.setItemId(sourceReceiptItem.getItemId());
61 targetReceiptItem.setItemIsDiscount(sourceReceiptItem.getItemIsDiscount());
62 targetReceiptItem.setItemIsRefund(sourceReceiptItem.getItemIsRefund());
63 targetReceiptItem.setItemManufacturer(sourceReceiptItem.getItemManufacturer());
64 targetReceiptItem.setItemNetPrice(sourceReceiptItem.getItemNetPrice());
65 targetReceiptItem.setItemNumber(sourceReceiptItem.getItemNumber());
66 targetReceiptItem.setItemProduct(sourceReceiptItem.getItemProduct());
67 targetReceiptItem.setItemProductQuantity(sourceReceiptItem.getItemProductQuantity());
68 targetReceiptItem.setItemReceipt(sourceReceiptItem.getItemReceipt());
69 targetReceiptItem.setItemTaxRate(sourceReceiptItem.getItemTaxRate());
73 * Checks if both receipt items are the same by assigned receipt, product
76 * @param receiptItem1 Receipt item 1
77 * @param receiptItem2 Receipt item 2
79 * @return Whether both are equal
81 public static boolean isSameReceiptItem (final BillableReceiptItem receiptItem1, final BillableReceiptItem receiptItem2) {
82 // Pre-compare both as entities (same id)
83 if (Objects.equals(receiptItem1, receiptItem2)) {
84 // Same entity (with id number)
89 if (null == receiptItem1) {
91 throw new NullPointerException("receiptItem1 is null"); //NOI18N
92 } else if ((receiptItem1.getItemId() instanceof Long) && (receiptItem1.getItemId() < 1)) {
94 throw new IllegalArgumentException(MessageFormat.format("receiptItem1.itemId={0} is not valid.", receiptItem1.getItemId())); //NOI18N
95 } else if (receiptItem1.getItemReceipt() == null) {
97 throw new NullPointerException("receiptItem1.itemReceipt is null"); //NOI18N
98 } else if (receiptItem1.getItemReceipt().getReceiptId() == null) {
100 throw new NullPointerException("receiptItem1.itemReceipt.receiptId is null"); //NOI18N
101 } else if (receiptItem1.getItemReceipt().getReceiptId() < 1) {
103 throw new NullPointerException(MessageFormat.format("receiptItem1.itemReceipt.receiptId={0} is not valid", receiptItem1.getItemReceipt().getReceiptId())); //NOI18N
104 } else if (receiptItem1.getItemProduct() == null) {
106 throw new NullPointerException("receiptItem1.itemProduct is null."); //NOI18N
107 } else if (receiptItem1.getItemProduct().getProductId() == null) {
109 throw new NullPointerException("receiptItem1.itemProduct.productId is null."); //NOI18N
110 } else if (receiptItem1.getItemProduct().getProductId() < 1) {
112 throw new IllegalArgumentException(MessageFormat.format("receiptItem1.itemProduct.productId={0} is invalid.", receiptItem1.getItemProduct().getProductId())); //NOI18N
113 } else if (null == receiptItem2) {
115 throw new NullPointerException("receiptItem2 is null"); //NOI18N
116 } else if ((receiptItem2.getItemId() instanceof Long) && (receiptItem2.getItemId() < 1)) {
118 throw new IllegalArgumentException(MessageFormat.format("receiptItem2.itemId={0} is not valid.", receiptItem2.getItemId())); //NOI18N
119 } else if (receiptItem2.getItemReceipt() == null) {
121 throw new NullPointerException("receiptItem2.itemReceipt is null"); //NOI18N
122 } else if (receiptItem2.getItemReceipt().getReceiptId() == null) {
124 throw new NullPointerException("receiptItem2.itemReceipt.receiptId is null"); //NOI18N
125 } else if (receiptItem2.getItemReceipt().getReceiptId() < 1) {
127 throw new NullPointerException(MessageFormat.format("receiptItem2.itemReceipt.receiptId={0} is not valid", receiptItem2.getItemReceipt().getReceiptId())); //NOI18N
128 } else if (receiptItem2.getItemProduct() == null) {
130 throw new NullPointerException("receiptItem2.itemProduct is null."); //NOI18N
131 } else if (receiptItem2.getItemProduct().getProductId() == null) {
133 throw new NullPointerException("receiptItem2.itemProduct.productId is null."); //NOI18N
134 } else if (receiptItem2.getItemProduct().getProductId() < 1) {
136 throw new IllegalArgumentException(MessageFormat.format("receiptItem2.itemProduct.productId={0} is invalid.", receiptItem2.getItemProduct().getProductId())); //NOI18N
137 } else if (null == receiptItem2) {
139 throw new NullPointerException("receiptItem2 is null"); //NOI18N
142 // Now check all individually
143 if (!Objects.equals(receiptItem1.getItemReceipt(), receiptItem2.getItemReceipt())) {
144 // Other item receipt
146 } else if (!Objects.equals(receiptItem1.getItemProduct(), receiptItem2.getItemProduct())) {
147 // Other item product
149 } else if (!Objects.equals(receiptItem1.getItemProductQuantity(), receiptItem2.getItemProductQuantity())) {
150 // Other item quantity
152 } else if (!Objects.equals(receiptItem1.getItemNumber(), receiptItem2.getItemNumber())) {
157 // Maybe same receipt!
162 * Private default constructor
164 private ReceiptItemUtils () {
165 // Utilities don't have instances