From 60401685ba6e4af19cfb201aa53fe633e51d8993 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Tue, 13 Mar 2018 01:07:59 +0100 Subject: [PATCH] Continued: - implemented unfinished Comparable - moved copyAll() method to Countries.copyCountry() as such "complex" methods should not exist in entities MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../events/added/AdminAddedCountryEvent.java | 2 +- .../ObservableAdminAddedCountryEvent.java | 2 +- .../CountryAlreadyAddedException.java | 2 +- .../exceptions/CountryNotFoundException.java | 2 +- .../jcountry/model/data/Countries.java | 69 +++++++++++++++++++ .../mxchange/jcountry/model/data/Country.java | 13 +--- .../jcountry/model/data/CountryData.java | 38 +++++----- 7 files changed, 93 insertions(+), 35 deletions(-) create mode 100644 src/org/mxchange/jcountry/model/data/Countries.java diff --git a/src/org/mxchange/jcountry/events/added/AdminAddedCountryEvent.java b/src/org/mxchange/jcountry/events/added/AdminAddedCountryEvent.java index 899a5b7..0f05afc 100644 --- a/src/org/mxchange/jcountry/events/added/AdminAddedCountryEvent.java +++ b/src/org/mxchange/jcountry/events/added/AdminAddedCountryEvent.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016, 2017 Free Software Foundation + * 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 diff --git a/src/org/mxchange/jcountry/events/added/ObservableAdminAddedCountryEvent.java b/src/org/mxchange/jcountry/events/added/ObservableAdminAddedCountryEvent.java index e356a09..953ea86 100644 --- a/src/org/mxchange/jcountry/events/added/ObservableAdminAddedCountryEvent.java +++ b/src/org/mxchange/jcountry/events/added/ObservableAdminAddedCountryEvent.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016, 2017 Free Software Foundation + * 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 diff --git a/src/org/mxchange/jcountry/exceptions/CountryAlreadyAddedException.java b/src/org/mxchange/jcountry/exceptions/CountryAlreadyAddedException.java index b27ad0d..ee2db1e 100644 --- a/src/org/mxchange/jcountry/exceptions/CountryAlreadyAddedException.java +++ b/src/org/mxchange/jcountry/exceptions/CountryAlreadyAddedException.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016, 2017 Free Software Foundation + * 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 Affero General Public License as diff --git a/src/org/mxchange/jcountry/exceptions/CountryNotFoundException.java b/src/org/mxchange/jcountry/exceptions/CountryNotFoundException.java index e2808ed..5e10664 100644 --- a/src/org/mxchange/jcountry/exceptions/CountryNotFoundException.java +++ b/src/org/mxchange/jcountry/exceptions/CountryNotFoundException.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016, 2017 Free Software Foundation + * 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 Affero General Public License as diff --git a/src/org/mxchange/jcountry/model/data/Countries.java b/src/org/mxchange/jcountry/model/data/Countries.java new file mode 100644 index 0000000..8fd5d8e --- /dev/null +++ b/src/org/mxchange/jcountry/model/data/Countries.java @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2018 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.jcountry.model.data; + +import java.io.Serializable; +import java.util.Objects; + +/** + * An utilities class for countries + *

+ * @author Roland Häder + */ +public class Countries implements Serializable { + + /** + * Serial number + */ + private static final long serialVersionUID = 286_956_737_410L; + + /** + * Copies data from sourceCountry to targetCountry. + *

+ * @param sourceCountry Source Country instance to copy data from + * @param targetCountry Target Country instance to copy data to + */ + public static void copyCountry (final Country sourceCountry, final Country targetCountry) { + // Parameters should not be null + if (null == sourceCountry) { + // Throw NPE + throw new NullPointerException("sourceCountry is null"); //NOI18N + } else if (null == targetCountry) { + // Throw NPE + throw new NullPointerException("targetCountry is null"); //NOI18N + } else if (Objects.equals(sourceCountry, targetCountry)) { + // Should never be equal + } + + // Copy all fields + targetCountry.setCountryAbroadDialPrefix(sourceCountry.getCountryAbroadDialPrefix()); + targetCountry.setCountryCode(sourceCountry.getCountryCode()); + targetCountry.setCountryEntryCreated(sourceCountry.getCountryEntryCreated()); + targetCountry.setCountryExternalDialPrefix(sourceCountry.getCountryExternalDialPrefix()); + targetCountry.setCountryI18nKey(sourceCountry.getCountryI18nKey()); + targetCountry.setCountryId(sourceCountry.getCountryId()); + targetCountry.setCountryIsLocalPrefixRequired(sourceCountry.getCountryIsLocalPrefixRequired()); + targetCountry.setCountryPhoneCode(sourceCountry.getCountryPhoneCode()); + } + + /** + * Utility classes should not have instances + */ + private Countries () { + } + +} diff --git a/src/org/mxchange/jcountry/model/data/Country.java b/src/org/mxchange/jcountry/model/data/Country.java index 96b1575..d595527 100644 --- a/src/org/mxchange/jcountry/model/data/Country.java +++ b/src/org/mxchange/jcountry/model/data/Country.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016, 2017 Free Software Foundation + * 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 @@ -24,14 +24,7 @@ import java.util.Date; *

* @author Roland Häder */ -public interface Country extends Serializable { - - /** - * Copies all entries from sounce country to this - *

- * @param sourceCountry Source country to copy from - */ - void copyAll (final Country sourceCountry); +public interface Country extends Comparable, Serializable { /** * Getter for country code (example: 49 for Germany, 63 for Philippines) @@ -128,7 +121,7 @@ public interface Country extends Serializable { * Setter for whether the local dial prefix is required for local calls *

* @param countryIsLocalPrefixRequired Whether the local dial prefix is - * required + * required */ void setCountryIsLocalPrefixRequired (final Boolean countryIsLocalPrefixRequired); diff --git a/src/org/mxchange/jcountry/model/data/CountryData.java b/src/org/mxchange/jcountry/model/data/CountryData.java index cb5763d..f851abb 100644 --- a/src/org/mxchange/jcountry/model/data/CountryData.java +++ b/src/org/mxchange/jcountry/model/data/CountryData.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016, 2017 Free Software Foundation + * 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 @@ -117,22 +117,8 @@ public class CountryData implements Country { private Short countryPhoneCode; @Override - public void copyAll (final Country sourceCountry) { - // Parameter should not be null - if (null == sourceCountry) { - // Throw NPE - throw new NullPointerException("sourceCountry is null"); //NOI18N - } - - // Copy all - this.setCountryAbroadDialPrefix(sourceCountry.getCountryAbroadDialPrefix()); - this.setCountryCode(sourceCountry.getCountryCode()); - this.setCountryEntryCreated(sourceCountry.getCountryEntryCreated()); - this.setCountryExternalDialPrefix(sourceCountry.getCountryExternalDialPrefix()); - this.setCountryI18nKey(sourceCountry.getCountryI18nKey()); - this.setCountryId(sourceCountry.getCountryId()); - this.setCountryIsLocalPrefixRequired(sourceCountry.getCountryIsLocalPrefixRequired()); - this.setCountryPhoneCode(sourceCountry.getCountryPhoneCode()); + public int compareTo (final Country country) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override @@ -145,13 +131,20 @@ public class CountryData implements Country { return false; } - final Country other = (Country) object; + // @todo Maybe a bit unsafe cast? + final Country country = (Country) object; - if (!Objects.equals(this.getCountryId(), other.getCountryId())) { + if (!Objects.equals(this.getCountryId(), country.getCountryId())) { + return false; + } else if (!Objects.equals(this.getCountryCode(), country.getCountryCode())) { + return false; + } else if (!Objects.equals(this.getCountryAbroadDialPrefix(), country.getCountryAbroadDialPrefix())) { + return false; + } else if (!Objects.equals(this.getCountryExternalDialPrefix(), country.getCountryExternalDialPrefix())) { return false; - } else if (!Objects.equals(this.getCountryCode(), other.getCountryCode())) { + } else if (!Objects.equals(this.getCountryPhoneCode(), country.getCountryPhoneCode())) { return false; - } else if (!Objects.equals(this.getCountryI18nKey(), other.getCountryI18nKey())) { + } else if (!Objects.equals(this.getCountryI18nKey(), country.getCountryI18nKey())) { return false; } @@ -246,6 +239,9 @@ public class CountryData implements Country { hash = 41 * hash + Objects.hashCode(this.getCountryId()); hash = 41 * hash + Objects.hashCode(this.getCountryCode()); + hash = 41 * hash + Objects.hashCode(this.getCountryAbroadDialPrefix()); + hash = 41 * hash + Objects.hashCode(this.getCountryExternalDialPrefix()); + hash = 41 * hash + Objects.hashCode(this.getCountryPhoneCode()); hash = 41 * hash + Objects.hashCode(this.getCountryI18nKey()); return hash; -- 2.39.5