2 * Copyright (C) 2016 - 2022 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.model.utils;
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;
29 * @author Roland Häder<roland@mxchange.org>
31 public class CustomerUtils {
34 * Random number generator
36 private static final Random RANDOM_NUMBER_GENERATOR;
43 RANDOM_NUMBER_GENERATOR = new SecureRandom();
47 * Compares both customer instances. This method returns -1 if second
50 * @param customer1 Customer instance 1
51 * @param customer2 Customer instance 2
53 * @return Comparison value
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)) {
60 } else if (null == customer1) {
63 } else if (null == customer2) {
68 // Invoke compareTo() method
69 return customer1.compareTo(customer2);
73 * Copies all fields from source customer to target customer instance.
75 * @param targetCustomer Target customer instance 1
76 * @param sourceCustomer Source customer instance 2
78 * @throws NullPointerException If one parameter is null
80 public static void copyCustomerData (final Customer targetCustomer, final Customer sourceCustomer) {
81 // Check both parameter
82 if (null == targetCustomer) {
84 throw new NullPointerException("targetCustomer is null"); //NOI18N
85 } else if (null == sourceCustomer) {
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
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());
103 * Generates a random customer number with some dashes in it
105 * @param totalLength Length of the number
106 * @param blockSize Block size
107 * @param separator Separator
109 * @return Generated customer number
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
122 StringBuilder customerNumber = new StringBuilder(totalLength);
124 // Calculate total blockas
125 long totalBlocks = Math.round(totalLength / (blockSize - 1) - 0.5) - 1;
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);
133 // Calculate remaining charcters
134 long remain = totalLength - (blockSize + 1) * totalBlocks;
136 // Generate new block and append it
137 customerNumber.append(genrateBlock((short) remain));
139 // Return finished number
140 return customerNumber.toString();
144 * Generates a block of numbers with leading zeros, if the random number is
145 * shorter than block size
147 * @param blockSize Block size
149 * @return Generated block
151 private static String genrateBlock (final short blockSize) {
152 // Generate random number
153 int num = RANDOM_NUMBER_GENERATOR.nextInt((int) Math.pow(10, blockSize));
155 // Generate "block" and return it
156 return StringUtils.leftPad(String.valueOf(num), blockSize, '0');
160 * No constructors for utility classes
162 private CustomerUtils () {