]> git.mxchange.org Git - jaddressbook-core.git/commitdiff
Continued:
authorRoland Häder <roland@mxchange.org>
Sun, 22 Jan 2023 05:09:43 +0000 (06:09 +0100)
committerRoland Häder <roland@mxchange.org>
Sun, 22 Jan 2023 05:09:43 +0000 (06:09 +0100)
- added checks for constructor parameters
- introduced AddressbookUtils class with first method compare() (the name says it)

src/org/mxchange/jaddressbook/model/addressbook/entry/UserAddressbookEntry.java
src/org/mxchange/jaddressbook/model/addressbook/status/AddressbokStatus.java
src/org/mxchange/jaddressbook/model/utils/AddressbookUtils.java [new file with mode: 0644]

index 9a0e26a83b9d26748ca93bc1cb14e6377c153c4d..f92644245d6d1318ad8b3cafac63249c39c71f8c 100644 (file)
@@ -16,6 +16,7 @@
  */
 package org.mxchange.jaddressbook.model.addressbook.entry;
 
+import java.text.MessageFormat;
 import java.util.Date;
 import java.util.Objects;
 import javax.persistence.Basic;
@@ -35,6 +36,7 @@ import javax.persistence.TemporalType;
 import javax.persistence.Transient;
 import org.mxchange.jaddressbook.model.addressbook.Addressbook;
 import org.mxchange.jaddressbook.model.addressbook.UserAddressbook;
+import org.mxchange.jaddressbook.model.utils.AddressbookUtils;
 import org.mxchange.jcontacts.model.contact.Contact;
 import org.mxchange.jcontacts.model.contact.UserContact;
 import org.mxchange.jcontacts.model.utils.ContactUtils;
@@ -108,12 +110,42 @@ public class UserAddressbookEntry implements AddressbookEntry {
        @OneToOne (targetEntity = UserAddressbook.class, cascade = CascadeType.REFRESH, optional = false)
        private Addressbook entryAddressbook;
 
+       /**
+        * Default constructor for the JPA, please use other constructor if you need
+        * a properly set instance of this class.
+        */
+       public UserAddressbookEntry () {
+       }
+
+       /**
+        * Constructor with all mandatory entity attributes, except timestamps and
+        * primary key.
+        * <p>
+        * @param addressbook Address book instance to link this entry to
+        */
+       public UserAddressbookEntry (final Addressbook addressbook) {
+               // Check parameter
+               if (null == addressbook) {
+                       // Throw NPE
+                       throw new NullPointerException("Parameter 'addressbook' is null");
+               } else if (addressbook.getAddressbookId() == null) {
+                       // Throw it again
+                       throw new NullPointerException("addressbook.addressbookId is null");
+               } else if (addressbook.getAddressbookId() < 1) {
+                       // Throw IAE
+                       throw new IllegalArgumentException(MessageFormat.format("addressbook.addressbookId={0} is invalid", addressbook.getAddressbookId()));
+               }
+
+               // Set all fields
+               this.entryAddressbook = addressbook;
+       }
+
        @Override
        public int compareTo (final AddressbookEntry addressbookEntry) {
                // Parameter should not be NULL
                if (null == addressbookEntry) {
                        // Should not happen
-                       throw new NullPointerException("addressbookEntry is null"); //NOI18N
+                       throw new NullPointerException("Parameter 'addressbookEntry' is null"); //NOI18N
                } else if (addressbookEntry.equals(this)) {
                        // Same object
                        return 0;
@@ -122,7 +154,7 @@ public class UserAddressbookEntry implements AddressbookEntry {
                // Init comparitors
                final int comparitors[] = {
                        // First compare address books
-                       this.getEntryAddressbook().compareTo(addressbookEntry.getEntryAddressbook()),
+                       AddressbookUtils.compare(this.getEntryAddressbook(), addressbookEntry.getEntryAddressbook()),
                        // ... then business address
                        BasicDataUtils.compare(this.getAddressbookEntryBusinessBasicData(), addressbookEntry.getAddressbookEntryBusinessBasicData()),
                        // ... then private contact
index e3c6a69a51992b489e6348a512cf0b5d63beb003..7e0eb598387680504ae8b14642cb113b7661db30 100644 (file)
@@ -46,15 +46,6 @@ public enum AddressbokStatus implements Serializable {
         * @param messageKey Message key for this enumeration
         */
        private AddressbokStatus (final String messageKey) {
-               // Validate parameter
-               if (null == messageKey) {
-                       // Throw NPE
-                       throw new NullPointerException("messageKey is null"); //NOI18N
-               } else if (messageKey.isEmpty()) {
-                       // Throw IAE
-                       throw new IllegalArgumentException("messageKey is empty"); //NOI18N
-               }
-
                // Set message key
                this.messageKey = messageKey;
        }
diff --git a/src/org/mxchange/jaddressbook/model/utils/AddressbookUtils.java b/src/org/mxchange/jaddressbook/model/utils/AddressbookUtils.java
new file mode 100644 (file)
index 0000000..a661ccd
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2023 Roland Häder<roland@mxchange.org>
+ *
+ * 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.jaddressbook.model.utils;
+
+import java.util.Objects;
+import org.mxchange.jaddressbook.model.addressbook.Addressbook;
+
+/**
+ * An utilities class for Addressbook instances
+ * <p>
+ * @author Roland Häder<roland@mxchange.org>
+ */
+public class AddressbookUtils {
+
+       /**
+        * Compares two instances of an Addressbook class
+        * <p>
+        * @param addressbook1 First Addressbook instance
+        * @param addressbook2 Second Addressbook instance
+        * <p>
+        * @return Comparison value
+        */
+       public static int compare (final Addressbook addressbook1, final Addressbook addressbook2) {
+               // Check equality, then at least first must be given
+               if (Objects.equals(addressbook1, addressbook2)) {
+                       // Both are same
+                       return 0;
+               } else if (null == addressbook1) {
+                       // First is null
+                       return -1;
+               } else if (null == addressbook2) {
+                       // Second is null
+                       return 1;
+               }
+
+               // Invoke compareTo() method
+               return addressbook1.compareTo(addressbook2);
+       }
+
+       /**
+        * No instance from utilities classes, so the constructor is private
+        */
+       private AddressbookUtils () {
+       }
+
+}