From: Roland Häder Date: Thu, 6 Oct 2022 13:09:07 +0000 (+0200) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=e6285ceb313d6fdd50688a97fef1fef9ff218d6d;p=jcountry-core.git Continued: - added utilities method Countries.compare() - renamed class Countries to CountryUtils --- diff --git a/src/org/mxchange/jcountry/model/data/Countries.java b/src/org/mxchange/jcountry/model/data/Countries.java deleted file mode 100644 index 825ec11..0000000 --- a/src/org/mxchange/jcountry/model/data/Countries.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * Copyright (C) 2022 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 . - */ -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 - *

- * @throws NullPointerException If sourceCountry or targetCountry is null - * @throws IllegalArgumentException If both Country instances are equal - */ - public static void copyCountryData (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)) { - /* - * Source and target country should never be equal, please check the - * condition before invoking this method. - */ - throw new IllegalArgumentException("Both sourceCountry and targetCountry are equal."); - } - - // Copy all fields - targetCountry.setCountryAbroadDialPrefix(sourceCountry.getCountryAbroadDialPrefix()); - targetCountry.setCountryCode(sourceCountry.getCountryCode()); - 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/CountryUtils.java b/src/org/mxchange/jcountry/model/data/CountryUtils.java new file mode 100644 index 0000000..7086765 --- /dev/null +++ b/src/org/mxchange/jcountry/model/data/CountryUtils.java @@ -0,0 +1,100 @@ +/* + * Copyright (C) 2022 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 . + */ +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 CountryUtils implements Serializable { + + /** + * Serial number + */ + private static final long serialVersionUID = 286_956_737_410L; + + /** + * Compares two instances of Country with each other + *

+ * @param country1 First instance of Country + * @param country2 Second instance of Country + *

+ * @return See Comparable.compareTo() + */ + public static int compare (final Country country1, final Country country2) { + // Check equality, then at least first must be given + if (Objects.equals(country1, country2)) { + // Both are same + return 0; + } else if (null == country1) { + // First is null + return -1; + } else if (null == country2) { + // Second is null + return 1; + } + + // Invoke compareTo() method + return country1.compareTo(country2); + } + + /** + * Copies data from sourceCountry to targetCountry. + *

+ * @param sourceCountry Source Country instance to copy data from + * @param targetCountry Target Country instance to copy data to + *

+ * @throws NullPointerException If sourceCountry or targetCountry is null + * @throws IllegalArgumentException If both Country instances are equal + */ + public static void copyCountryData (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)) { + /* + * Source and target country should never be equal, please check the + * condition before invoking this method. + */ + throw new IllegalArgumentException("Both sourceCountry and targetCountry are equal."); + } + + // Copy all fields + targetCountry.setCountryAbroadDialPrefix(sourceCountry.getCountryAbroadDialPrefix()); + targetCountry.setCountryCode(sourceCountry.getCountryCode()); + 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 CountryUtils () { + } + +}