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