]> git.mxchange.org Git - jcountry-core.git/blobdiff - src/org/mxchange/jcountry/model/data/CountryUtils.java
Continued:
[jcountry-core.git] / src / org / mxchange / jcountry / model / data / CountryUtils.java
diff --git a/src/org/mxchange/jcountry/model/data/CountryUtils.java b/src/org/mxchange/jcountry/model/data/CountryUtils.java
new file mode 100644 (file)
index 0000000..7086765
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.jcountry.model.data;
+
+import java.io.Serializable;
+import java.util.Objects;
+
+/**
+ * An utilities class for countries
+ * <p>
+ * @author Roland Häder<roland@mxchange.org>
+ */
+public class CountryUtils implements Serializable {
+
+       /**
+        * Serial number
+        */
+       private static final long serialVersionUID = 286_956_737_410L;
+
+       /**
+        * Compares two instances of Country with each other
+        * <p>
+        * @param country1 First instance of Country
+        * @param country2 Second instance of Country
+        * <p>
+        * @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.
+        * <p>
+        * @param sourceCountry Source Country instance to copy data from
+        * @param targetCountry Target Country instance to copy data to
+        * <p>
+        * @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 () {
+       }
+
+}