]> git.mxchange.org Git - jcustomer-core.git/blob - src/org/mxchange/jcustomercore/utils/CustomerUtils.java
Continued a bit:
[jcustomer-core.git] / src / org / mxchange / jcustomercore / utils / CustomerUtils.java
1 /*
2  * Copyright (C) 2016, 2017 Roland Häder
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.Random;
22 import org.apache.commons.lang3.StringUtils;
23
24 /**
25  * Customer utilities
26  * <p>
27  * @author Roland Häder<roland@mxchange.org>
28  */
29 public class CustomerUtils {
30
31         /**
32          * Random number generator
33          */
34         private static final Random RANDOM_NUMBER_GENERATOR;
35
36         /**
37          * Static initializer
38          */
39         static {
40                 // Init RNG
41                 RANDOM_NUMBER_GENERATOR = new SecureRandom();
42         }
43
44         /**
45          * Generates a random customer number with some dashes in it
46          * <p>
47          * @param totalLength Length of the number
48          * @param blockSize Block size
49          * @param separator Seperator
50          * <p>
51          * @return Generated customer number
52          */
53         public static String generateCustomerNumber (final int totalLength, final short blockSize, final char separator) {
54                 // All parameters must be set
55                 if (totalLength < 5) {
56                         // Total length is to short
57                         throw new IllegalArgumentException(MessageFormat.format("Total length of{0} characters is to short (5 minimum)", totalLength)); //NOI18N
58                 } else if (blockSize < 3) {
59                         // 3 charcters is minimum
60                         throw new IllegalArgumentException(MessageFormat.format("Block size of {0} characters is to short (3 minimum)", blockSize)); //NOI18N
61                 }
62
63                 // Init number
64                 StringBuilder customerNumber = new StringBuilder(totalLength);
65
66                 // Calculate total blockas
67                 long totalBlocks = Math.round(totalLength / (blockSize - 1) - 0.5) - 1;
68
69                 // Generate customer number
70                 for (int i = 0; i < totalBlocks; i++) {
71                         // Fill it up with leading zeros and append it + separator character
72                         customerNumber.append(genrateBlock(blockSize)).append(separator);
73                 }
74
75                 // Calculate remaining charcters
76                 long remain = totalLength - (blockSize + 1) * totalBlocks;
77
78                 // Generate new block and append it
79                 customerNumber.append(genrateBlock((short) remain));
80
81                 // Return finished number
82                 return customerNumber.toString();
83         }
84
85         /**
86          * Generates a block of numbers with leading zeros, if the random number is
87          * shorter than block size
88          * <p>
89          * @param blockSize Block size
90          * <p>
91          * @return Generated block
92          */
93         private static String genrateBlock (final short blockSize) {
94                 // Generate random number
95                 int num = RANDOM_NUMBER_GENERATOR.nextInt((int) Math.pow(10, blockSize));
96
97                 // Generate "block" and return it
98                 return StringUtils.leftPad(String.valueOf(num), blockSize, '0');
99         }
100
101         /**
102          * No constructors for utiltity classes
103          */
104         private CustomerUtils () {
105         }
106
107 }