]> git.mxchange.org Git - jfinancials-core.git/blob - src/org/mxchange/jfinancials/model/receipt/Receipts.java
a2f6d8a317f78d5951f292616b8215ca269d3184
[jfinancials-core.git] / src / org / mxchange / jfinancials / model / receipt / Receipts.java
1 /*
2  * Copyright (C) 2017, 2018 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 euqality, 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          * Checks whether both receipts are the same, means not true entity equality
63          * but if the receipt in general terms does already exist. A pre-check on
64          * entity equality is however performed to avoid below much longer code.
65          * <p>
66          * @param receipt1 BillableReceipt instance 1
67          * @param receipt2 BillableReceipt instance 2
68          * <p>
69          * @return Whether both receipts are the same (not instances)
70          */
71         public static boolean isSameReceipt (final BillableReceipt receipt1, final BillableReceipt receipt2) {
72                 // Pre-compare both as entities (same id)
73                 if (Objects.equals(receipt1, receipt2)) {
74                         // Same entity (with id number)
75                         return true;
76                 }
77
78                 // Validate parameter
79                 if (null == receipt1) {
80                         // Throw NPE
81                         throw new NullPointerException("receipt1 is null");
82                 } else if ((receipt1.getReceiptId() instanceof Long) && (receipt1.getReceiptId() < 1)) {
83                         // Throw IAE
84                         throw new IllegalArgumentException(MessageFormat.format("receipt1.receiptId={0} is not valid.", receipt1.getReceiptId()));
85                 } else if (receipt1.getReceiptBranchOffice() == null) {
86                         // Throw NPE
87                         throw new NullPointerException("receipt1.receiptBranchOffice is null");
88                 } else if (receipt1.getReceiptBranchOffice().getBranchId() == null) {
89                         // Throw NPE
90                         throw new NullPointerException("receipt1.receiptBranchOffice.branchId is null");
91                 } else if (receipt1.getReceiptBranchOffice().getBranchId() < 1) {
92                         // Throw NPE
93                         throw new NullPointerException(MessageFormat.format("receipt1.receiptBranchOffice.branchId={0} is not valid", receipt1.getReceiptBranchOffice().getBranchId()));
94                 } else if ((receipt1.getReceiptNumber() != null) && (receipt1.getReceiptNumber().isEmpty())) {
95                         // Throw IAE
96                         throw new IllegalArgumentException("receipt1.receiptNumber is empty.");
97                 } else if (receipt1.getReceiptPaymentType() == null) {
98                         // Throw NPE
99                         throw new NullPointerException("receipt1.receiptPaymentType is null");
100                 } else if (null == receipt2) {
101                         // Throw NPE
102                         throw new NullPointerException("receipt2 is null");
103                 } else if ((receipt2.getReceiptId() instanceof Long) && (receipt2.getReceiptId() < 1)) {
104                         // Throw IAE
105                         throw new IllegalArgumentException(MessageFormat.format("receipt2.receiptId={0} is not valid.", receipt2.getReceiptId()));
106                 } else if (receipt2.getReceiptBranchOffice() == null) {
107                         // Throw NPE
108                         throw new NullPointerException("receipt2.receiptBranchOffice is null");
109                 } else if (receipt2.getReceiptBranchOffice().getBranchId() == null) {
110                         // Throw NPE
111                         throw new NullPointerException("receipt2.receiptBranchOffice.branchId is null");
112                 } else if (receipt2.getReceiptBranchOffice().getBranchId() < 1) {
113                         // Throw NPE
114                         throw new NullPointerException(MessageFormat.format("receipt2.receiptBranchOffice.branchId={0} is not valid", receipt2.getReceiptBranchOffice().getBranchId()));
115                 } else if ((receipt2.getReceiptNumber() != null) && (receipt2.getReceiptNumber().isEmpty())) {
116                         // Throw IAE
117                         throw new IllegalArgumentException("receipt2.receiptNumber is empty.");
118                 } else if (receipt2.getReceiptPaymentType() == null) {
119                         // Throw NPE
120                         throw new NullPointerException("receipt2.receiptPaymentType is null");
121                 }
122
123                 // Now check all individually
124                 if (!Objects.equals(receipt1.getReceiptBranchOffice(), receipt2.getReceiptBranchOffice())) {
125                         // Other branch offices
126                         return false;
127                 } else if (!Objects.equals(receipt1.getReceiptNumber(), receipt2.getReceiptNumber())) {
128                         // Other receipt number
129                         return false;
130                 } else if (!Objects.equals(receipt1.getReceiptUser(), receipt2.getReceiptUser())) {
131                         // Other user (unlikely to happen
132                         return false;
133                 }
134
135                 // Maybe same receipt!
136                 return true;
137         }
138
139         /**
140          * Private default constructor
141          */
142         private Receipts () {
143                 // Utilities don't have instances
144         }
145
146 }