From: Roland Häder Date: Sun, 18 Mar 2018 23:14:45 +0000 (+0100) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=2caf8450d9922e6aec0eb865c9e4691655f8619c;p=jcustomer-core.git Continued: - renamed CustomerUtils -> Customers - implemented Comparable - added compare() method for easy null-safe comparison - moved copyAll() to Customers utilities class 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 b132c35..b92dd19 100644 --- a/src/org/mxchange/jcustomercore/model/customer/ContactCustomer.java +++ b/src/org/mxchange/jcustomercore/model/customer/ContactCustomer.java @@ -36,8 +36,10 @@ import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; 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.jcoreee.utils.Comparables; import org.mxchange.jcustomercore.model.customer.status.CustomerAccountStatus; /** @@ -51,8 +53,7 @@ import org.mxchange.jcustomercore.model.customer.status.CustomerAccountStatus; ) @NamedQueries ( { - @NamedQuery (name = "AllCustomers", query = "SELECT c FROM customer AS c ORDER BY c.customerId ASC"), - @NamedQuery (name = "SearchCustomerByNumber", query = "SELECT c FROM customer AS c WHERE c.customerNumber = :customerNumber"), + @NamedQuery (name = "AllCustomers", query = "SELECT c FROM customer AS c ORDER BY c.customerId ASC") } ) @SuppressWarnings ("PersistenceUnitPresent") @@ -133,9 +134,9 @@ public class ContactCustomer implements Customer { * Constructor with account status, contact instance and customer number *

* @param customerAccountStatus Account status (Call-agents may only call - * unlocked accounts) - * @param customerContact Contact instance - * @param customerNumber Customer number + * unlocked accounts) + * @param customerContact Contact instance + * @param customerNumber Customer number */ public ContactCustomer (final CustomerAccountStatus customerAccountStatus, final Contact customerContact, final String customerNumber) { // Call other constructor @@ -148,15 +149,31 @@ public class ContactCustomer implements Customer { } @Override - public void copyAll (final Customer customer) { - // Copy all supported fields - this.setCustomerAccountStatus(customer.getCustomerAccountStatus()); - this.setCustomerContact(customer.getCustomerContact()); - this.setCustomerCreated(customer.getCustomerCreated()); - this.setCustomerId(customer.getCustomerId()); - this.setCustomerLastLocked(customer.getCustomerLastLocked()); - this.setCustomerLastLockedReason(customer.getCustomerLastLockedReason()); - this.setCustomerNumber(customer.getCustomerNumber()); + public int compareTo (final Customer customer) { + // For performance reasons + if (null == customer) { + // Should not happen + throw new NullPointerException("customer is null"); //NOI18N + } else if (Objects.equals(this, customer)) { + // Same object + return 0; + } + + // Init comparators + final int comparators[] = { + // First check contact instance + this.getCustomerContact().compareTo(customer.getCustomerContact()), + // ... then customer number + this.getCustomerNumber().compareTo(customer.getCustomerNumber()), + // ... last is confirmation key + StringUtils.compare(this.getCustomerConfirmKey(), customer.getCustomerConfirmKey()) + }; + + // Check all values + final int comparison = Comparables.checkAll(comparators); + + // Return value + return comparison; } @Override @@ -182,17 +199,6 @@ public class ContactCustomer implements Customer { return true; } - @Override - public int hashCode () { - int hash = 7; - - hash = 53 * hash + Objects.hashCode(this.getCustomerContact()); - hash = 53 * hash + Objects.hashCode(this.getCustomerId()); - hash = 53 * hash + Objects.hashCode(this.getCustomerNumber()); - - return hash; - } - @Override public CustomerAccountStatus getCustomerAccountStatus () { return this.customerAccountStatus; @@ -299,4 +305,15 @@ public class ContactCustomer implements Customer { this.customerUpdated = customerUpdated; } + @Override + public int hashCode () { + int hash = 7; + + hash = 53 * hash + Objects.hashCode(this.getCustomerContact()); + hash = 53 * hash + Objects.hashCode(this.getCustomerId()); + hash = 53 * hash + Objects.hashCode(this.getCustomerNumber()); + + return hash; + } + } diff --git a/src/org/mxchange/jcustomercore/model/customer/Customer.java b/src/org/mxchange/jcustomercore/model/customer/Customer.java index 05c2225..45273de 100644 --- a/src/org/mxchange/jcustomercore/model/customer/Customer.java +++ b/src/org/mxchange/jcustomercore/model/customer/Customer.java @@ -26,14 +26,7 @@ import org.mxchange.jcustomercore.model.customer.status.CustomerAccountStatus; *

* @author Roland Häder */ -public interface Customer extends Serializable { - - /** - * Copies all attributes from other customer object to this - *

- * @param customer Source instance - */ - void copyAll (final Customer customer); +public interface Customer extends Comparable, Serializable { /** * Getter for contact instance diff --git a/src/org/mxchange/jcustomercore/utils/CustomerUtils.java b/src/org/mxchange/jcustomercore/utils/CustomerUtils.java deleted file mode 100644 index ae95246..0000000 --- a/src/org/mxchange/jcustomercore/utils/CustomerUtils.java +++ /dev/null @@ -1,107 +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.Random; -import org.apache.commons.lang3.StringUtils; - -/** - * 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(); - } - - /** - * Generates a random customer number with some dashes in it - *

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

- * @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 utiltity classes - */ - private CustomerUtils () { - } - -} diff --git a/src/org/mxchange/jcustomercore/utils/Customers.java b/src/org/mxchange/jcustomercore/utils/Customers.java new file mode 100644 index 0000000..20ed6fd --- /dev/null +++ b/src/org/mxchange/jcustomercore/utils/Customers.java @@ -0,0 +1,136 @@ +/* + * 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.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(); + } + + /** + * 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 () { + } + +}