]> git.mxchange.org Git - jcustomer-core.git/blob - src/org/mxchange/jcustomercore/utils/Customers.java
20ed6fd0d5204e2dc75f22e900de131a7804935a
[jcustomer-core.git] / src / org / mxchange / jcustomercore / utils / Customers.java
1 /*
2  * Copyright (C) 2016 - 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 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.utils;
18
19 import java.security.SecureRandom;
20 import java.text.MessageFormat;
21 import java.util.Random;
22 import org.apache.commons.lang3.StringUtils;
23 import org.mxchange.jcustomercore.model.customer.Customer;
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          * Copies all fields from source customer to target customer instance.
47          * <p>
48          * @param targetCustomer Target customer instance 1
49          * @param sourceCustomer Source customer instance 2
50          * <p>
51          * @throws NullPointerException If one parameter is null
52          */
53         public static void copyAll (final Customer targetCustomer, final Customer sourceCustomer) {
54                 // Check both parameter
55                 if (null == targetCustomer) {
56                         // Throw NPE
57                         throw new NullPointerException("targetCustomer is null"); //NOI18N
58                 } else if (null == sourceCustomer) {
59                         // Throw NPE
60                         throw new NullPointerException("sourceCustomer is null"); //NOI18N
61                 }
62
63                 // Copy all fields
64                 targetCustomer.setCustomerAccountStatus(sourceCustomer.getCustomerAccountStatus());
65                 targetCustomer.setCustomerContact(sourceCustomer.getCustomerContact());
66                 targetCustomer.setCustomerCreated(sourceCustomer.getCustomerCreated());
67                 targetCustomer.setCustomerId(sourceCustomer.getCustomerId());
68                 targetCustomer.setCustomerLastLocked(sourceCustomer.getCustomerLastLocked());
69                 targetCustomer.setCustomerLastLockedReason(sourceCustomer.getCustomerLastLockedReason());
70                 targetCustomer.setCustomerNumber(sourceCustomer.getCustomerNumber());
71         }
72
73         /**
74          * Generates a random customer number with some dashes in it
75          * <p>
76          * @param totalLength Length of the number
77          * @param blockSize   Block size
78          * @param separator   Separator
79          * <p>
80          * @return Generated customer number
81          */
82         public static String generateCustomerNumber (final int totalLength, final short blockSize, final char separator) {
83                 // All parameters must be set
84                 if (totalLength < 5) {
85                         // Total length is to short
86                         throw new IllegalArgumentException(MessageFormat.format("Total length of{0} characters is to short (5 minimum)", totalLength)); //NOI18N
87                 } else if (blockSize < 3) {
88                         // 3 charcters is minimum
89                         throw new IllegalArgumentException(MessageFormat.format("Block size of {0} characters is to short (3 minimum)", blockSize)); //NOI18N
90                 }
91
92                 // Init number
93                 StringBuilder customerNumber = new StringBuilder(totalLength);
94
95                 // Calculate total blockas
96                 long totalBlocks = Math.round(totalLength / (blockSize - 1) - 0.5) - 1;
97
98                 // Generate customer number
99                 for (int i = 0; i < totalBlocks; i++) {
100                         // Fill it up with leading zeros and append it + separator character
101                         customerNumber.append(genrateBlock(blockSize)).append(separator);
102                 }
103
104                 // Calculate remaining charcters
105                 long remain = totalLength - (blockSize + 1) * totalBlocks;
106
107                 // Generate new block and append it
108                 customerNumber.append(genrateBlock((short) remain));
109
110                 // Return finished number
111                 return customerNumber.toString();
112         }
113
114         /**
115          * Generates a block of numbers with leading zeros, if the random number is
116          * shorter than block size
117          * <p>
118          * @param blockSize Block size
119          * <p>
120          * @return Generated block
121          */
122         private static String genrateBlock (final short blockSize) {
123                 // Generate random number
124                 int num = RANDOM_NUMBER_GENERATOR.nextInt((int) Math.pow(10, blockSize));
125
126                 // Generate "block" and return it
127                 return StringUtils.leftPad(String.valueOf(num), blockSize, '0');
128         }
129
130         /**
131          * No constructors for utility classes
132          */
133         private Customers () {
134         }
135
136 }