*/
package org.mxchange.jaddressbook.model.addressbook.entry;
+import java.text.MessageFormat;
import java.util.Date;
import java.util.Objects;
import javax.persistence.Basic;
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;
@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;
// 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
--- /dev/null
+/*
+ * 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 () {
+ }
+
+}