]> git.mxchange.org Git - jcustomer-core.git/blob - src/org/mxchange/jcustomercore/model/customer/Customers.java
d9da59653676f5e98d67ff0fb0013812db46cb19
[jcustomer-core.git] / src / org / mxchange / jcustomercore / model / customer / Customers.java
1 /*
2  * Copyright (C) 2016 - 2022 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 General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (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 General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.jcustomercore.model.customer;
18
19 import java.security.SecureRandom;
20 import java.text.MessageFormat;
21 import java.util.Objects;
22 import java.util.Random;
23 import org.apache.commons.lang3.StringUtils;
24
25 /**
26  * Customer utilities
27  * <p>
28  * @author Roland Häder<roland@mxchange.org>
29  */
30 public class Customers {
31
32         /**
33          * Random number generator
34          */
35         private static final Random RANDOM_NUMBER_GENERATOR;
36
37         /**
38          * Static initializer
39          */
40         static {
41                 // Init RNG
42                 RANDOM_NUMBER_GENERATOR = new SecureRandom();
43         }
44
45         /**
46          * Compares both customer instances. This method returns -1 if second
47          * instance is null.
48          * <p>
49          * @param customer1 Customer instance 1
50          * @param customer2 Customer instance 2
51          * <p>
52          * @return Comparison value
53          */
54         public static int compare (final Customer customer1, final Customer customer2) {
55                 // Check equality, then at least first must be given
56                 if (Objects.equals(customer1, customer2)) {
57                         // Both are same
58                         return 0;
59                 } else if (null == customer1) {
60                         // First is null
61                         return -1;
62                 } else if (null == customer2) {
63                         // Second is null
64                         return 1;
65                 }
66
67                 // Invoke compareTo() method
68                 return customer1.compareTo(customer2);
69         }
70
71         /**
72          * Copies all fields from source customer to target customer instance.
73          * <p>
74          * @param targetCustomer Target customer instance 1
75          * @param sourceCustomer Source customer instance 2
76          * <p>
77          * @throws NullPointerException If one parameter is null
78          */
79         public static void copyCustomerData (final Customer targetCustomer, final Customer sourceCustomer) {
80                 // Check both parameter
81                 if (null == targetCustomer) {
82                         // Throw NPE
83                         throw new NullPointerException("targetCustomer is null"); //NOI18N
84                 } else if (null == sourceCustomer) {
85                         // Throw NPE
86                         throw new NullPointerException("sourceCustomer is null"); //NOI18N
87                 } else if (Objects.equals(sourceCustomer, targetCustomer)) {
88                         // Is exactly the same!
89                         throw new IllegalArgumentException("sourcerCustomer and targetCustomer are the same."); //NOI18N
90                 }
91
92                 // Copy all fields
93                 targetCustomer.setCustomerAccountStatus(sourceCustomer.getCustomerAccountStatus());
94                 targetCustomer.setCustomerContact(sourceCustomer.getCustomerContact());
95                 targetCustomer.setCustomerId(sourceCustomer.getCustomerId());
96                 targetCustomer.setCustomerLastLocked(sourceCustomer.getCustomerLastLocked());
97                 targetCustomer.setCustomerLastLockedReason(sourceCustomer.getCustomerLastLockedReason());
98                 targetCustomer.setCustomerNumber(sourceCustomer.getCustomerNumber());
99         }
100
101         /**
102          * Generates a random customer number with some dashes in it
103          * <p>
104          * @param totalLength Length of the number
105          * @param blockSize   Block size
106          * @param separator   Separator
107          * <p>
108          * @return Generated customer number
109          */
110         public static String generateCustomerNumber (final int totalLength, final short blockSize, final char separator) {
111                 // All parameters must be set
112                 if (totalLength < 5) {
113                         // Total length is to short
114                         throw new IllegalArgumentException(MessageFormat.format("Total length of{0} characters is to short (5 minimum)", totalLength)); //NOI18N
115                 } else if (blockSize < 3) {
116                         // 3 charcters is minimum
117                         throw new IllegalArgumentException(MessageFormat.format("Block size of {0} characters is to short (3 minimum)", blockSize)); //NOI18N
118                 }
119
120                 // Init number
121                 StringBuilder customerNumber = new StringBuilder(totalLength);
122
123                 // Calculate total blockas
124                 long totalBlocks = Math.round(totalLength / (blockSize - 1) - 0.5) - 1;
125
126                 // Generate customer number
127                 for (int i = 0; i < totalBlocks; i++) {
128                         // Fill it up with leading zeros and append it + separator character
129                         customerNumber.append(genrateBlock(blockSize)).append(separator);
130                 }
131
132                 // Calculate remaining charcters
133                 long remain = totalLength - (blockSize + 1) * totalBlocks;
134
135                 // Generate new block and append it
136                 customerNumber.append(genrateBlock((short) remain));
137
138                 // Return finished number
139                 return customerNumber.toString();
140         }
141
142         /**
143          * Generates a block of numbers with leading zeros, if the random number is
144          * shorter than block size
145          * <p>
146          * @param blockSize Block size
147          * <p>
148          * @return Generated block
149          */
150         private static String genrateBlock (final short blockSize) {
151                 // Generate random number
152                 int num = RANDOM_NUMBER_GENERATOR.nextInt((int) Math.pow(10, blockSize));
153
154                 // Generate "block" and return it
155                 return StringUtils.leftPad(String.valueOf(num), blockSize, '0');
156         }
157
158         /**
159          * No constructors for utility classes
160          */
161         private Customers () {
162         }
163
164 }