]> git.mxchange.org Git - jcustomer-core.git/blob - src/org/mxchange/jcustomercore/utils/Customers.java
Continued:
[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.Objects;
22 import java.util.Random;
23 import org.apache.commons.lang3.StringUtils;
24 import org.mxchange.jcustomercore.model.customer.Customer;
25
26 /**
27  * Customer utilities
28  * <p>
29  * @author Roland Häder<roland@mxchange.org>
30  */
31 public class Customers {
32
33         /**
34          * Random number generator
35          */
36         private static final Random RANDOM_NUMBER_GENERATOR;
37
38         /**
39          * Static initializer
40          */
41         static {
42                 // Init RNG
43                 RANDOM_NUMBER_GENERATOR = new SecureRandom();
44         }
45
46         /**
47          * Compares both customer instances. This method returns -1 if second
48          * instance is null.
49          * <p>
50          * @param customer1 Customer instance 1
51          * @param customer2 Customer instance 2
52          * <p>
53          * @return Comparison value
54          */
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)) {
58                         // Both are same
59                         return 0;
60                 } else if (null == customer1) {
61                         // First is null
62                         return -1;
63                 } else if (null == customer2) {
64                         // Second is null
65                         return 1;
66                 }
67
68                 // Invoke compareTo() method
69                 return customer1.compareTo(customer2);
70         }
71
72         /**
73          * Copies all fields from source customer to target customer instance.
74          * <p>
75          * @param targetCustomer Target customer instance 1
76          * @param sourceCustomer Source customer instance 2
77          * <p>
78          * @throws NullPointerException If one parameter is null
79          */
80         public static void copyAll (final Customer targetCustomer, final Customer sourceCustomer) {
81                 // Check both parameter
82                 if (null == targetCustomer) {
83                         // Throw NPE
84                         throw new NullPointerException("targetCustomer is null"); //NOI18N
85                 } else if (null == sourceCustomer) {
86                         // Throw NPE
87                         throw new NullPointerException("sourceCustomer is null"); //NOI18N
88                 }
89
90                 // Copy all fields
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());
98         }
99
100         /**
101          * Generates a random customer number with some dashes in it
102          * <p>
103          * @param totalLength Length of the number
104          * @param blockSize   Block size
105          * @param separator   Separator
106          * <p>
107          * @return Generated customer number
108          */
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
117                 }
118
119                 // Init number
120                 StringBuilder customerNumber = new StringBuilder(totalLength);
121
122                 // Calculate total blockas
123                 long totalBlocks = Math.round(totalLength / (blockSize - 1) - 0.5) - 1;
124
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);
129                 }
130
131                 // Calculate remaining charcters
132                 long remain = totalLength - (blockSize + 1) * totalBlocks;
133
134                 // Generate new block and append it
135                 customerNumber.append(genrateBlock((short) remain));
136
137                 // Return finished number
138                 return customerNumber.toString();
139         }
140
141         /**
142          * Generates a block of numbers with leading zeros, if the random number is
143          * shorter than block size
144          * <p>
145          * @param blockSize Block size
146          * <p>
147          * @return Generated block
148          */
149         private static String genrateBlock (final short blockSize) {
150                 // Generate random number
151                 int num = RANDOM_NUMBER_GENERATOR.nextInt((int) Math.pow(10, blockSize));
152
153                 // Generate "block" and return it
154                 return StringUtils.leftPad(String.valueOf(num), blockSize, '0');
155         }
156
157         /**
158          * No constructors for utility classes
159          */
160         private Customers () {
161         }
162
163 }