2 * Copyright (C) 2016 - 2018 Free Software Foundation
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.
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.
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/>.
17 package org.mxchange.jcustomercore.utils;
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;
28 * @author Roland Häder<roland@mxchange.org>
30 public class Customers {
33 * Random number generator
35 private static final Random RANDOM_NUMBER_GENERATOR;
42 RANDOM_NUMBER_GENERATOR = new SecureRandom();
46 * Copies all fields from source customer to target customer instance.
48 * @param targetCustomer Target customer instance 1
49 * @param sourceCustomer Source customer instance 2
51 * @throws NullPointerException If one parameter is null
53 public static void copyAll (final Customer targetCustomer, final Customer sourceCustomer) {
54 // Check both parameter
55 if (null == targetCustomer) {
57 throw new NullPointerException("targetCustomer is null"); //NOI18N
58 } else if (null == sourceCustomer) {
60 throw new NullPointerException("sourceCustomer is null"); //NOI18N
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());
74 * Generates a random customer number with some dashes in it
76 * @param totalLength Length of the number
77 * @param blockSize Block size
78 * @param separator Separator
80 * @return Generated customer number
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
93 StringBuilder customerNumber = new StringBuilder(totalLength);
95 // Calculate total blockas
96 long totalBlocks = Math.round(totalLength / (blockSize - 1) - 0.5) - 1;
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);
104 // Calculate remaining charcters
105 long remain = totalLength - (blockSize + 1) * totalBlocks;
107 // Generate new block and append it
108 customerNumber.append(genrateBlock((short) remain));
110 // Return finished number
111 return customerNumber.toString();
115 * Generates a block of numbers with leading zeros, if the random number is
116 * shorter than block size
118 * @param blockSize Block size
120 * @return Generated block
122 private static String genrateBlock (final short blockSize) {
123 // Generate random number
124 int num = RANDOM_NUMBER_GENERATOR.nextInt((int) Math.pow(10, blockSize));
126 // Generate "block" and return it
127 return StringUtils.leftPad(String.valueOf(num), blockSize, '0');
131 * No constructors for utility classes
133 private Customers () {