]> git.mxchange.org Git - jcustomer-core.git/commitdiff
Continued:
authorRoland Häder <roland@mxchange.org>
Sun, 18 Mar 2018 23:14:45 +0000 (00:14 +0100)
committerRoland Häder <roland@mxchange.org>
Sun, 18 Mar 2018 23:14:45 +0000 (00:14 +0100)
- 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 <roland@mxchange.org>
src/org/mxchange/jcustomercore/model/customer/ContactCustomer.java
src/org/mxchange/jcustomercore/model/customer/Customer.java
src/org/mxchange/jcustomercore/utils/CustomerUtils.java [deleted file]
src/org/mxchange/jcustomercore/utils/Customers.java [new file with mode: 0644]

index b132c35de6866fc584c5d61077415a9613acdc75..b92dd197d96709c213c962213d19af9b0054d569 100644 (file)
@@ -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
         * <p>
         * @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;
+       }
+
 }
index 05c2225caa15aee064d0ca8491df50e0aba60ebe..45273deaad36566b398f223dcf1daf0a49997ec2 100644 (file)
@@ -26,14 +26,7 @@ import org.mxchange.jcustomercore.model.customer.status.CustomerAccountStatus;
  * <p>
  * @author Roland Häder<roland@mxchange.org>
  */
-public interface Customer extends Serializable {
-
-       /**
-        * Copies all attributes from other customer object to this
-        * <p>
-        * @param customer Source instance
-        */
-       void copyAll (final Customer customer);
+public interface Customer extends Comparable<Customer>, 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 (file)
index ae95246..0000000
+++ /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 <http://www.gnu.org/licenses/>.
- */
-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
- * <p>
- * @author Roland Häder<roland@mxchange.org>
- */
-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
-        * <p>
-        * @param totalLength Length of the number
-        * @param blockSize Block size
-        * @param separator Seperator
-        * <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 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 (file)
index 0000000..20ed6fd
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.
+ */
+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
+ * <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();
+       }
+
+       /**
+        * 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 () {
+       }
+
+}