From 2caf8450d9922e6aec0eb865c9e4691655f8619c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Mon, 19 Mar 2018 00:14:45 +0100 Subject: [PATCH] Continued: - renamed CustomerUtils -> Customers - implemented Comparable - added compare() method for easy null-safe comparison - moved copyAll() to Customers utilities class MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../model/customer/ContactCustomer.java | 67 ++++++++++++------- .../model/customer/Customer.java | 9 +-- .../{CustomerUtils.java => Customers.java} | 39 +++++++++-- 3 files changed, 77 insertions(+), 38 deletions(-) rename src/org/mxchange/jcustomercore/utils/{CustomerUtils.java => Customers.java} (68%) 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/Customers.java similarity index 68% rename from src/org/mxchange/jcustomercore/utils/CustomerUtils.java rename to src/org/mxchange/jcustomercore/utils/Customers.java index ae95246..20ed6fd 100644 --- a/src/org/mxchange/jcustomercore/utils/CustomerUtils.java +++ b/src/org/mxchange/jcustomercore/utils/Customers.java @@ -20,13 +20,14 @@ 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 CustomerUtils { +public class Customers { /** * Random number generator @@ -41,12 +42,40 @@ public class CustomerUtils { 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 Seperator + * @param blockSize Block size + * @param separator Separator *

* @return Generated customer number */ @@ -99,9 +128,9 @@ public class CustomerUtils { } /** - * No constructors for utiltity classes + * No constructors for utility classes */ - private CustomerUtils () { + private Customers () { } } -- 2.39.5