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.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 Customers {
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 euqality, 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 copyAll (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
91 targetCustomer.setCustomerAccountStatus(sourceCustomer.getCustomerAccountStatus());
92 targetCustomer.setCustomerContact(sourceCustomer.getCustomerContact());
93 targetCustomer.setCustomerCreated(sourceCustomer.getCustomerCreated());
94 targetCustomer.setCustomerId(sourceCustomer.getCustomerId());
95 targetCustomer.setCustomerLastLocked(sourceCustomer.getCustomerLastLocked());
96 targetCustomer.setCustomerLastLockedReason(sourceCustomer.getCustomerLastLockedReason());
97 targetCustomer.setCustomerNumber(sourceCustomer.getCustomerNumber());
101 * Generates a random customer number with some dashes in it
103 * @param totalLength Length of the number
104 * @param blockSize Block size
105 * @param separator Separator
107 * @return Generated customer number
109 public static String generateCustomerNumber (final int totalLength, final short blockSize, final char separator) {
110 // All parameters must be set
111 if (totalLength < 5) {
112 // Total length is to short
113 throw new IllegalArgumentException(MessageFormat.format("Total length of{0} characters is to short (5 minimum)", totalLength)); //NOI18N
114 } else if (blockSize < 3) {
115 // 3 charcters is minimum
116 throw new IllegalArgumentException(MessageFormat.format("Block size of {0} characters is to short (3 minimum)", blockSize)); //NOI18N
120 StringBuilder customerNumber = new StringBuilder(totalLength);
122 // Calculate total blockas
123 long totalBlocks = Math.round(totalLength / (blockSize - 1) - 0.5) - 1;
125 // Generate customer number
126 for (int i = 0; i < totalBlocks; i++) {
127 // Fill it up with leading zeros and append it + separator character
128 customerNumber.append(genrateBlock(blockSize)).append(separator);
131 // Calculate remaining charcters
132 long remain = totalLength - (blockSize + 1) * totalBlocks;
134 // Generate new block and append it
135 customerNumber.append(genrateBlock((short) remain));
137 // Return finished number
138 return customerNumber.toString();
142 * Generates a block of numbers with leading zeros, if the random number is
143 * shorter than block size
145 * @param blockSize Block size
147 * @return Generated block
149 private static String genrateBlock (final short blockSize) {
150 // Generate random number
151 int num = RANDOM_NUMBER_GENERATOR.nextInt((int) Math.pow(10, blockSize));
153 // Generate "block" and return it
154 return StringUtils.leftPad(String.valueOf(num), blockSize, '0');
158 * No constructors for utility classes
160 private Customers () {