From: Roland Häder Date: Sun, 22 Jan 2023 05:09:43 +0000 (+0100) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=e59b9a1ea740baa93c276141a096f054f328abd1;p=jaddressbook-core.git Continued: - added checks for constructor parameters - introduced AddressbookUtils class with first method compare() (the name says it) --- diff --git a/src/org/mxchange/jaddressbook/model/addressbook/entry/UserAddressbookEntry.java b/src/org/mxchange/jaddressbook/model/addressbook/entry/UserAddressbookEntry.java index 9a0e26a..f926442 100644 --- a/src/org/mxchange/jaddressbook/model/addressbook/entry/UserAddressbookEntry.java +++ b/src/org/mxchange/jaddressbook/model/addressbook/entry/UserAddressbookEntry.java @@ -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. + *

+ * @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 diff --git a/src/org/mxchange/jaddressbook/model/addressbook/status/AddressbokStatus.java b/src/org/mxchange/jaddressbook/model/addressbook/status/AddressbokStatus.java index e3c6a69..7e0eb59 100644 --- a/src/org/mxchange/jaddressbook/model/addressbook/status/AddressbokStatus.java +++ b/src/org/mxchange/jaddressbook/model/addressbook/status/AddressbokStatus.java @@ -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 index 0000000..a661ccd --- /dev/null +++ b/src/org/mxchange/jaddressbook/model/utils/AddressbookUtils.java @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2023 Roland Häder + * + * 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.jaddressbook.model.utils; + +import java.util.Objects; +import org.mxchange.jaddressbook.model.addressbook.Addressbook; + +/** + * An utilities class for Addressbook instances + *

+ * @author Roland Häder + */ +public class AddressbookUtils { + + /** + * Compares two instances of an Addressbook class + *

+ * @param addressbook1 First Addressbook instance + * @param addressbook2 Second Addressbook instance + *

+ * @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 () { + } + +}