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.Random;
22 import org.apache.commons.lang3.StringUtils;
27 * @author Roland Häder<roland@mxchange.org>
29 public class CustomerUtils {
32 * Random number generator
34 private static final Random RANDOM_NUMBER_GENERATOR;
41 RANDOM_NUMBER_GENERATOR = new SecureRandom();
45 * Generates a random customer number with some dashes in it
47 * @param totalLength Length of the number
48 * @param blockSize Block size
49 * @param separator Seperator
51 * @return Generated customer number
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
64 StringBuilder customerNumber = new StringBuilder(totalLength);
66 // Calculate total blockas
67 long totalBlocks = Math.round(totalLength / (blockSize - 1) - 0.5) - 1;
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);
75 // Calculate remaining charcters
76 long remain = totalLength - (blockSize + 1) * totalBlocks;
78 // Generate new block and append it
79 customerNumber.append(genrateBlock((short) remain));
81 // Return finished number
82 return customerNumber.toString();
86 * Generates a block of numbers with leading zeros, if the random number is
87 * shorter than block size
89 * @param blockSize Block size
91 * @return Generated block
93 private static String genrateBlock (final short blockSize) {
94 // Generate random number
95 int num = RANDOM_NUMBER_GENERATOR.nextInt((int) Math.pow(10, blockSize));
97 // Generate "block" and return it
98 return StringUtils.leftPad(String.valueOf(num), blockSize, '0');
102 * No constructors for utiltity classes
104 private CustomerUtils () {