Removed iterator() and added a check when setting formats.
authorMarco Zanon <info@marcozanon.com>
Mon, 10 Jul 2017 12:08:17 +0000 (12:08 +0000)
committerMarco Zanon <info@marcozanon.com>
Mon, 10 Jul 2017 12:08:17 +0000 (12:08 +0000)
5.x/src/java/com/marcozanon/macaco/conversion/MDateConverter.java
5.x/src/java/com/marcozanon/macaco/conversion/MLocalDateConverter.java
5.x/src/java/com/marcozanon/macaco/conversion/MLocalDateTimeConverter.java
5.x/src/java/com/marcozanon/macaco/conversion/MNumberConverter.java

index 4461edfc0ebfb6727d6a45189502e7d0baa93377..9ac09251b7ca76714a8d9f6d26a9c039db74e53b 100644 (file)
@@ -56,10 +56,12 @@ public class MDateConverter extends MObject {
         if (null == dateFormats) {
             throw new IllegalArgumentException("Invalid 'dateFormats': null.");
         }
+        else if (0 == dateFormats.size()) {
+            throw new IllegalArgumentException("Invalid 'dateFormats': empty.");
+        }
         else {
-            Iterator<String> i = dateFormats.iterator();
-            while (i.hasNext()) {
-                MDateConverter.checkDateFormat(i.next());
+            for (String dateFormat: dateFormats) {
+                MDateConverter.checkDateFormat(dateFormat);
             }
         }
         //
index fa5e7776d0af46d9d0e539cf2b14d414f8b3de95..6eef1b2cba5173b13e87cfe985cdf547c5b0f8e4 100644 (file)
@@ -13,27 +13,31 @@ import java.time.format.DateTimeFormatter;
 import java.time.format.DateTimeParseException;
 import java.util.Iterator;
 import java.util.LinkedHashSet;
+import java.util.Locale;
 
 public class MLocalDateConverter extends MObject {
 
     protected LinkedHashSet<String> dateFormats = new LinkedHashSet<String>();
+    protected Locale locale = null;
 
     /* */
 
-    public MLocalDateConverter(String defaultDateFormat) {
+    public MLocalDateConverter(String defaultDateFormat, Locale locale) {
         super();
         //
         this.addDateFormat(defaultDateFormat);
+        this.setLocale(locale);
     }
 
-    public MLocalDateConverter(LinkedHashSet<String> dateFormats) {
+    public MLocalDateConverter(LinkedHashSet<String> dateFormats, Locale locale) {
         super();
         //
         this.setDateFormats(dateFormats);
+        this.setLocale(locale);
     }
 
     public MLocalDateConverter clone() {
-        return new MLocalDateConverter(this.getDateFormats());
+        return new MLocalDateConverter(this.getDateFormats(), this.getLocale());
     }
 
     /* Date formats. */
@@ -42,10 +46,12 @@ public class MLocalDateConverter extends MObject {
         if (null == dateFormats) {
             throw new IllegalArgumentException("Invalid 'dateFormats': null.");
         }
+        else if (0 == dateFormats.size()) {
+            throw new IllegalArgumentException("Invalid 'dateFormats': empty.");
+        }
         else {
-            Iterator<String> i = dateFormats.iterator();
-            while (i.hasNext()) {
-                MLocalDateConverter.checkDateFormat(i.next());
+            for (String dateFormat: dateFormats) {
+                MLocalDateConverter.checkDateFormat(dateFormat);
             }
         }
         //
@@ -70,6 +76,20 @@ public class MLocalDateConverter extends MObject {
         return this.getDateFormats().iterator().next();
     }
 
+    /* Locale. */
+
+    protected void setLocale(Locale locale) {
+        if (null == locale) {
+            throw new IllegalArgumentException("Invalid 'locale': null.");
+        }
+        //
+        this.locale = locale;
+    }
+
+    public Locale getLocale() {
+        return this.locale;
+    }
+
     /* Conversions. */
 
     protected static void checkDateFormat(String dateFormat) {
@@ -85,15 +105,18 @@ public class MLocalDateConverter extends MObject {
         }
     }
 
-    protected static LocalDate getDateFromStringByParameters(String x, String inputDateFormat) throws MInvalidConversionFormatException {
+    protected static LocalDate getDateFromStringByParameters(String x, String inputDateFormat, Locale inputLocale) throws MInvalidConversionFormatException {
         if (MText.isBlank(x)) {
             throw new IllegalArgumentException("Invalid 'x': null or empty.");
         }
         MLocalDateConverter.checkDateFormat(inputDateFormat);
+        if (null == inputLocale) {
+            throw new IllegalArgumentException("Invalid 'inputLocale': null.");
+        }
         //
         LocalDate d = null;
         try {
-            d = LocalDate.parse(x, DateTimeFormatter.ofPattern(inputDateFormat));
+            d = LocalDate.parse(x, DateTimeFormatter.ofPattern(inputDateFormat, inputLocale));
         }
         catch (DateTimeParseException exception) {
             throw new MInvalidConversionFormatException(String.format("Invalid 'x' or parsing: %s (input format: %s).", x, inputDateFormat)); // no need to propagate exception
@@ -102,20 +125,23 @@ public class MLocalDateConverter extends MObject {
         return d;
     }
 
-    protected static String getStringFromDateByParameters(LocalDate date, String outputDateFormat) {
+    protected static String getStringFromDateByParameters(LocalDate date, String outputDateFormat, Locale outputLocale) {
         if (null == date) {
             throw new IllegalArgumentException("Invalid 'date': null.");
         }
         MLocalDateConverter.checkDateFormat(outputDateFormat);
+        if (null == outputLocale) {
+            throw new IllegalArgumentException("Invalid 'outputLocale': null.");
+        }
         //
-        return date.format(DateTimeFormatter.ofPattern(outputDateFormat));
+        return date.format(DateTimeFormatter.ofPattern(outputDateFormat, outputLocale));
     }
 
     public LocalDate getDateFromString(String x) throws MInvalidConversionFormatException {
         LocalDate y = null;
         for (String dateFormat: this.getDateFormats()) {
             try {
-                y = MLocalDateConverter.getDateFromStringByParameters(x, dateFormat);
+                y = MLocalDateConverter.getDateFromStringByParameters(x, dateFormat, this.getLocale());
                 return y;
             }
             catch (MInvalidConversionFormatException exception) {
@@ -128,7 +154,7 @@ public class MLocalDateConverter extends MObject {
     }
 
     public String getStringFromDate(LocalDate x) {
-        return MLocalDateConverter.getStringFromDateByParameters(x, this.getDefaultDateFormat());
+        return MLocalDateConverter.getStringFromDateByParameters(x, this.getDefaultDateFormat(), this.getLocale());
     }
 
 }
index 956a7d2572d818768cb21367946de87b6489b3b3..2d00e77391457f8582f3f244410f15d470c1bf3a 100644 (file)
@@ -13,27 +13,31 @@ import java.time.format.DateTimeFormatter;
 import java.time.format.DateTimeParseException;
 import java.util.Iterator;
 import java.util.LinkedHashSet;
+import java.util.Locale;
 
 public class MLocalDateTimeConverter extends MObject {
 
     protected LinkedHashSet<String> datetimeFormats = new LinkedHashSet<String>();
+    protected Locale locale = null;
 
     /* */
 
-    public MLocalDateTimeConverter(String defaultDatetimeFormat) {
+    public MLocalDateTimeConverter(String defaultDatetimeFormat, Locale locale) {
         super();
         //
         this.addDatetimeFormat(defaultDatetimeFormat);
+        this.setLocale(locale);
     }
 
-    public MLocalDateTimeConverter(LinkedHashSet<String> datetimeFormats) {
+    public MLocalDateTimeConverter(LinkedHashSet<String> datetimeFormats, Locale locale) {
         super();
         //
         this.setDatetimeFormats(datetimeFormats);
+        this.setLocale(locale);
     }
 
     public MLocalDateTimeConverter clone() {
-        return new MLocalDateTimeConverter(this.getDatetimeFormats());
+        return new MLocalDateTimeConverter(this.getDatetimeFormats(), this.getLocale());
     }
 
     /* Datetime formats. */
@@ -42,10 +46,12 @@ public class MLocalDateTimeConverter extends MObject {
         if (null == datetimeFormats) {
             throw new IllegalArgumentException("Invalid 'datetimeFormats': null.");
         }
+        else if (0 == datetimeFormats.size()) {
+            throw new IllegalArgumentException("Invalid 'datetimeFormats': empty.");
+        }
         else {
-            Iterator<String> i = datetimeFormats.iterator();
-            while (i.hasNext()) {
-                MLocalDateTimeConverter.checkDatetimeFormat(i.next());
+            for (String datetimeFormat: datetimeFormats) {
+                MLocalDateTimeConverter.checkDatetimeFormat(datetimeFormat);
             }
         }
         //
@@ -70,6 +76,20 @@ public class MLocalDateTimeConverter extends MObject {
         return this.getDatetimeFormats().iterator().next();
     }
 
+    /* Locale. */
+
+    protected void setLocale(Locale locale) {
+        if (null == locale) {
+            throw new IllegalArgumentException("Invalid 'locale': null.");
+        }
+        //
+        this.locale = locale;
+    }
+
+    public Locale getLocale() {
+        return this.locale;
+    }
+
     /* Conversions. */
 
     protected static void checkDatetimeFormat(String datetimeFormat) {
@@ -85,15 +105,18 @@ public class MLocalDateTimeConverter extends MObject {
         }
     }
 
-    protected static LocalDateTime getDatetimeFromStringByParameters(String x, String inputDatetimeFormat) throws MInvalidConversionFormatException {
+    protected static LocalDateTime getDatetimeFromStringByParameters(String x, String inputDatetimeFormat, Locale inputLocale) throws MInvalidConversionFormatException {
         if (MText.isBlank(x)) {
             throw new IllegalArgumentException("Invalid 'x': null or empty.");
         }
         MLocalDateTimeConverter.checkDatetimeFormat(inputDatetimeFormat);
+        if (null == inputLocale) {
+            throw new IllegalArgumentException("Invalid 'inputLocale': null.");
+        }
         //
         LocalDateTime dt = null;
         try {
-            dt = LocalDateTime.parse(x, DateTimeFormatter.ofPattern(inputDatetimeFormat));
+            dt = LocalDateTime.parse(x, DateTimeFormatter.ofPattern(inputDatetimeFormat, inputLocale));
         }
         catch (DateTimeParseException exception) {
             throw new MInvalidConversionFormatException(String.format("Invalid 'x' or parsing: %s (input format: %s).", x, inputDatetimeFormat)); // no need to propagate exception
@@ -102,20 +125,23 @@ public class MLocalDateTimeConverter extends MObject {
         return dt;
     }
 
-    protected static String getStringFromDatetimeByParameters(LocalDateTime datetime, String outputDatetimeFormat) {
+    protected static String getStringFromDatetimeByParameters(LocalDateTime datetime, String outputDatetimeFormat, Locale outputLocale) {
         if (null == datetime) {
             throw new IllegalArgumentException("Invalid 'datetime': null.");
         }
         MLocalDateTimeConverter.checkDatetimeFormat(outputDatetimeFormat);
+        if (null == outputLocale) {
+            throw new IllegalArgumentException("Invalid 'outputLocale': null.");
+        }
         //
-        return datetime.format(DateTimeFormatter.ofPattern(outputDatetimeFormat));
+        return datetime.format(DateTimeFormatter.ofPattern(outputDatetimeFormat, outputLocale));
     }
 
     public LocalDateTime getDatetimeFromString(String x) throws MInvalidConversionFormatException {
         LocalDateTime y = null;
         for (String datetimeFormat: this.getDatetimeFormats()) {
             try {
-                y = MLocalDateTimeConverter.getDatetimeFromStringByParameters(x, datetimeFormat);
+                y = MLocalDateTimeConverter.getDatetimeFromStringByParameters(x, datetimeFormat, this.getLocale());
                 return y;
             }
             catch (MInvalidConversionFormatException exception) {
@@ -128,7 +154,7 @@ public class MLocalDateTimeConverter extends MObject {
     }
 
     public String getStringFromDatetime(LocalDateTime x) {
-        return MLocalDateTimeConverter.getStringFromDatetimeByParameters(x, this.getDefaultDatetimeFormat());
+        return MLocalDateTimeConverter.getStringFromDatetimeByParameters(x, this.getDefaultDatetimeFormat(), this.getLocale());
     }
 
 }
index 05d321999528a15245d4f086fa7c9aa5ade9128f..a77a66c9c693f1b993b1bd897f12c2a3af0e38e8 100644 (file)
@@ -48,10 +48,12 @@ public class MNumberConverter extends MObject {
         if (null == numberFormats) {
             throw new IllegalArgumentException("Invalid 'numberFormats': null.");
         }
+        else if (0 == numberFormats.size()) {
+            throw new IllegalArgumentException("Invalid 'numberFormats': empty.");
+        }
         else {
-            Iterator<String> i = numberFormats.iterator();
-            while (i.hasNext()) {
-                MNumberConverter.checkNumberFormat(i.next());
+            for (String numberFormat: numberFormats) {
+                MNumberConverter.checkNumberFormat(numberFormat);
             }
         }
         //