-/*
- * Copyright (C) 2016 - 2018 Free Software Foundation
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-package org.mxchange.jcustomercore.utils;
-
-import java.security.SecureRandom;
-import java.text.MessageFormat;
-import java.util.Objects;
-import java.util.Random;
-import org.apache.commons.lang3.StringUtils;
-import org.mxchange.jcustomercore.model.customer.Customer;
-
-/**
- * Customer utilities
- * <p>
- * @author Roland Häder<roland@mxchange.org>
- */
-public class Customers {
-
- /**
- * Random number generator
- */
- private static final Random RANDOM_NUMBER_GENERATOR;
-
- /**
- * Static initializer
- */
- static {
- // Init RNG
- RANDOM_NUMBER_GENERATOR = new SecureRandom();
- }
-
- /**
- * Compares both customer instances. This method returns -1 if second
- * instance is null.
- * <p>
- * @param customer1 Customer instance 1
- * @param customer2 Customer instance 2
- * <p>
- * @return Comparison value
- */
- public static int compare (final Customer customer1, final Customer customer2) {
- // Check euqality, then at least first must be given
- if (Objects.equals(customer1, customer2)) {
- // Both are same
- return 0;
- } else if (null == customer1) {
- // First is null
- return -1;
- } else if (null == customer2) {
- // Second is null
- return 1;
- }
-
- // Invoke compareTo() method
- return customer1.compareTo(customer2);
- }
-
- /**
- * Copies all fields from source customer to target customer instance.
- * <p>
- * @param targetCustomer Target customer instance 1
- * @param sourceCustomer Source customer instance 2
- * <p>
- * @throws NullPointerException If one parameter is null
- */
- public static void copyAll (final Customer targetCustomer, final Customer sourceCustomer) {
- // Check both parameter
- if (null == targetCustomer) {
- // Throw NPE
- throw new NullPointerException("targetCustomer is null"); //NOI18N
- } else if (null == sourceCustomer) {
- // Throw NPE
- throw new NullPointerException("sourceCustomer is null"); //NOI18N
- }
-
- // Copy all fields
- targetCustomer.setCustomerAccountStatus(sourceCustomer.getCustomerAccountStatus());
- targetCustomer.setCustomerContact(sourceCustomer.getCustomerContact());
- targetCustomer.setCustomerCreated(sourceCustomer.getCustomerCreated());
- targetCustomer.setCustomerId(sourceCustomer.getCustomerId());
- targetCustomer.setCustomerLastLocked(sourceCustomer.getCustomerLastLocked());
- targetCustomer.setCustomerLastLockedReason(sourceCustomer.getCustomerLastLockedReason());
- targetCustomer.setCustomerNumber(sourceCustomer.getCustomerNumber());
- }
-
- /**
- * Generates a random customer number with some dashes in it
- * <p>
- * @param totalLength Length of the number
- * @param blockSize Block size
- * @param separator Separator
- * <p>
- * @return Generated customer number
- */
- public static String generateCustomerNumber (final int totalLength, final short blockSize, final char separator) {
- // All parameters must be set
- if (totalLength < 5) {
- // Total length is to short
- throw new IllegalArgumentException(MessageFormat.format("Total length of{0} characters is to short (5 minimum)", totalLength)); //NOI18N
- } else if (blockSize < 3) {
- // 3 charcters is minimum
- throw new IllegalArgumentException(MessageFormat.format("Block size of {0} characters is to short (3 minimum)", blockSize)); //NOI18N
- }
-
- // Init number
- StringBuilder customerNumber = new StringBuilder(totalLength);
-
- // Calculate total blockas
- long totalBlocks = Math.round(totalLength / (blockSize - 1) - 0.5) - 1;
-
- // Generate customer number
- for (int i = 0; i < totalBlocks; i++) {
- // Fill it up with leading zeros and append it + separator character
- customerNumber.append(genrateBlock(blockSize)).append(separator);
- }
-
- // Calculate remaining charcters
- long remain = totalLength - (blockSize + 1) * totalBlocks;
-
- // Generate new block and append it
- customerNumber.append(genrateBlock((short) remain));
-
- // Return finished number
- return customerNumber.toString();
- }
-
- /**
- * Generates a block of numbers with leading zeros, if the random number is
- * shorter than block size
- * <p>
- * @param blockSize Block size
- * <p>
- * @return Generated block
- */
- private static String genrateBlock (final short blockSize) {
- // Generate random number
- int num = RANDOM_NUMBER_GENERATOR.nextInt((int) Math.pow(10, blockSize));
-
- // Generate "block" and return it
- return StringUtils.leftPad(String.valueOf(num), blockSize, '0');
- }
-
- /**
- * No constructors for utility classes
- */
- private Customers () {
- }
-
-}