From: Roland Häder Date: Mon, 19 Mar 2018 23:45:21 +0000 (+0100) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=6a2276b2ba35aa0ae9774dd77b8b18f378bae8cf;p=jcustomer-core.git Continued: - moved Customers utility class to same package as model class is Signed-off-by: Roland Häder --- diff --git a/src/org/mxchange/jcustomercore/model/customer/ContactCustomer.java b/src/org/mxchange/jcustomercore/model/customer/ContactCustomer.java index 840141b..53a04c4 100644 --- a/src/org/mxchange/jcustomercore/model/customer/ContactCustomer.java +++ b/src/org/mxchange/jcustomercore/model/customer/ContactCustomer.java @@ -150,7 +150,7 @@ public class ContactCustomer implements Customer { @Override public int compareTo (final Customer customer) { - // For performance reasons + // Check parameter on null-reference and equality to this if (null == customer) { // Should not happen throw new NullPointerException("customer is null"); //NOI18N diff --git a/src/org/mxchange/jcustomercore/model/customer/Customers.java b/src/org/mxchange/jcustomercore/model/customer/Customers.java new file mode 100644 index 0000000..236cdb6 --- /dev/null +++ b/src/org/mxchange/jcustomercore/model/customer/Customers.java @@ -0,0 +1,162 @@ +/* + * 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 . + */ +package org.mxchange.jcustomercore.model.customer; + +import java.security.SecureRandom; +import java.text.MessageFormat; +import java.util.Objects; +import java.util.Random; +import org.apache.commons.lang3.StringUtils; + +/** + * Customer utilities + *

+ * @author Roland Häder + */ +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. + *

+ * @param customer1 Customer instance 1 + * @param customer2 Customer instance 2 + *

+ * @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. + *

+ * @param targetCustomer Target customer instance 1 + * @param sourceCustomer Source customer instance 2 + *

+ * @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 + *

+ * @param totalLength Length of the number + * @param blockSize Block size + * @param separator Separator + *

+ * @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 + *

+ * @param blockSize Block size + *

+ * @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 () { + } + +} diff --git a/src/org/mxchange/jcustomercore/utils/Customers.java b/src/org/mxchange/jcustomercore/utils/Customers.java deleted file mode 100644 index 866d871..0000000 --- a/src/org/mxchange/jcustomercore/utils/Customers.java +++ /dev/null @@ -1,163 +0,0 @@ -/* - * 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 . - */ -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 - *

- * @author Roland Häder - */ -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. - *

- * @param customer1 Customer instance 1 - * @param customer2 Customer instance 2 - *

- * @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. - *

- * @param targetCustomer Target customer instance 1 - * @param sourceCustomer Source customer instance 2 - *

- * @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 - *

- * @param totalLength Length of the number - * @param blockSize Block size - * @param separator Separator - *

- * @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 - *

- * @param blockSize Block size - *

- * @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 () { - } - -}