2 * Copyright (C) 2016 - 2020 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.customer;
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;
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 * Compares both customer instances. This method returns -1 if second
49 * @param customer1 Customer instance 1
50 * @param customer2 Customer instance 2
52 * @return Comparison value
54 public static int compare (final Customer customer1, final Customer customer2) {
55 // Check euqality, then at least first must be given
56 if (Objects.equals(customer1, customer2)) {
59 } else if (null == customer1) {
62 } else if (null == customer2) {
67 // Invoke compareTo() method
68 return customer1.compareTo(customer2);
72 * Copies all fields from source customer to target customer instance.
74 * @param targetCustomer Target customer instance 1
75 * @param sourceCustomer Source customer instance 2
77 * @throws NullPointerException If one parameter is null
79 public static void copyCustomerData (final Customer targetCustomer, final Customer sourceCustomer) {
80 // Check both parameter
81 if (null == targetCustomer) {
83 throw new NullPointerException("targetCustomer is null"); //NOI18N
84 } else if (null == sourceCustomer) {
86 throw new NullPointerException("sourceCustomer is null"); //NOI18N
90 targetCustomer.setCustomerAccountStatus(sourceCustomer.getCustomerAccountStatus());
91 targetCustomer.setCustomerContact(sourceCustomer.getCustomerContact());
92 targetCustomer.setCustomerId(sourceCustomer.getCustomerId());
93 targetCustomer.setCustomerLastLocked(sourceCustomer.getCustomerLastLocked());
94 targetCustomer.setCustomerLastLockedReason(sourceCustomer.getCustomerLastLockedReason());
95 targetCustomer.setCustomerNumber(sourceCustomer.getCustomerNumber());
99 * Generates a random customer number with some dashes in it
101 * @param totalLength Length of the number
102 * @param blockSize Block size
103 * @param separator Separator
105 * @return Generated customer number
107 public static String generateCustomerNumber (final int totalLength, final short blockSize, final char separator) {
108 // All parameters must be set
109 if (totalLength < 5) {
110 // Total length is to short
111 throw new IllegalArgumentException(MessageFormat.format("Total length of{0} characters is to short (5 minimum)", totalLength)); //NOI18N
112 } else if (blockSize < 3) {
113 // 3 charcters is minimum
114 throw new IllegalArgumentException(MessageFormat.format("Block size of {0} characters is to short (3 minimum)", blockSize)); //NOI18N
118 StringBuilder customerNumber = new StringBuilder(totalLength);
120 // Calculate total blockas
121 long totalBlocks = Math.round(totalLength / (blockSize - 1) - 0.5) - 1;
123 // Generate customer number
124 for (int i = 0; i < totalBlocks; i++) {
125 // Fill it up with leading zeros and append it + separator character
126 customerNumber.append(genrateBlock(blockSize)).append(separator);
129 // Calculate remaining charcters
130 long remain = totalLength - (blockSize + 1) * totalBlocks;
132 // Generate new block and append it
133 customerNumber.append(genrateBlock((short) remain));
135 // Return finished number
136 return customerNumber.toString();
140 * Generates a block of numbers with leading zeros, if the random number is
141 * shorter than block size
143 * @param blockSize Block size
145 * @return Generated block
147 private static String genrateBlock (final short blockSize) {
148 // Generate random number
149 int num = RANDOM_NUMBER_GENERATOR.nextInt((int) Math.pow(10, blockSize));
151 // Generate "block" and return it
152 return StringUtils.leftPad(String.valueOf(num), blockSize, '0');
156 * No constructors for utility classes
158 private Customers () {