From: Roland Häder Date: Thu, 6 Oct 2022 14:16:47 +0000 (+0200) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=89988c95728960ace1b4d8ba515ec3b6907c65cc;p=jcustomer-core.git Continued: - renamed utilities class Customers to CustomerUtils - moved it to own package --- diff --git a/src/org/mxchange/jcustomercore/model/customer/ContactCustomer.java b/src/org/mxchange/jcustomercore/model/customer/ContactCustomer.java index 04daa5d..81f12fa 100644 --- a/src/org/mxchange/jcustomercore/model/customer/ContactCustomer.java +++ b/src/org/mxchange/jcustomercore/model/customer/ContactCustomer.java @@ -39,6 +39,7 @@ import javax.persistence.Transient; import org.apache.commons.lang3.StringUtils; import org.mxchange.jcontacts.model.contact.Contact; import org.mxchange.jcontacts.model.contact.UserContact; +import org.mxchange.jcontacts.model.utils.ContactUtils; import org.mxchange.jcoreutils.Comparables; import org.mxchange.jcustomercore.model.customer.status.CustomerAccountStatus; @@ -168,8 +169,8 @@ public class ContactCustomer implements Customer { // Check parameter on null-reference and equality to this if (null == customer) { // Should not happen - throw new NullPointerException("customer is null"); //NOI18N - } else if (customer.equals(this)) { + throw new NullPointerException("Parameter 'customer' is null"); //NOI18N + } else if (Objects.equals(this, customer)) { // Same object return 0; } @@ -177,9 +178,9 @@ public class ContactCustomer implements Customer { // Init comparators final int comparators[] = { // First check contact instance - this.getCustomerContact().compareTo(customer.getCustomerContact()), + ContactUtils.compare(this.getCustomerContact(), customer.getCustomerContact()), // ... then customer number - this.getCustomerNumber().compareTo(customer.getCustomerNumber()), + StringUtils.compare(this.getCustomerNumber(), customer.getCustomerNumber()), // ... last is confirmation key StringUtils.compare(this.getCustomerConfirmKey(), customer.getCustomerConfirmKey()) }; diff --git a/src/org/mxchange/jcustomercore/model/customer/Customers.java b/src/org/mxchange/jcustomercore/model/customer/Customers.java deleted file mode 100644 index d9da596..0000000 --- a/src/org/mxchange/jcustomercore/model/customer/Customers.java +++ /dev/null @@ -1,164 +0,0 @@ -/* - * Copyright (C) 2016 - 2022 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 equality, 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 copyCustomerData (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 - } else if (Objects.equals(sourceCustomer, targetCustomer)) { - // Is exactly the same! - throw new IllegalArgumentException("sourcerCustomer and targetCustomer are the same."); //NOI18N - } - - // Copy all fields - targetCustomer.setCustomerAccountStatus(sourceCustomer.getCustomerAccountStatus()); - targetCustomer.setCustomerContact(sourceCustomer.getCustomerContact()); - 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/model/utils/CustomerUtils.java b/src/org/mxchange/jcustomercore/model/utils/CustomerUtils.java new file mode 100644 index 0000000..7558e91 --- /dev/null +++ b/src/org/mxchange/jcustomercore/model/utils/CustomerUtils.java @@ -0,0 +1,165 @@ +/* + * Copyright (C) 2016 - 2022 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.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 CustomerUtils { + + /** + * 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 equality, 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 copyCustomerData (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 + } else if (Objects.equals(sourceCustomer, targetCustomer)) { + // Is exactly the same! + throw new IllegalArgumentException("sourcerCustomer and targetCustomer are the same."); //NOI18N + } + + // Copy all fields + targetCustomer.setCustomerAccountStatus(sourceCustomer.getCustomerAccountStatus()); + targetCustomer.setCustomerContact(sourceCustomer.getCustomerContact()); + 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 CustomerUtils () { + } + +}