From d85cd0029eb16ca52c00353c4d1107d121f04d1c Mon Sep 17 00:00:00 2001 From: Marco Zanon Date: Mon, 10 Jul 2017 12:08:17 +0000 Subject: [PATCH] Removed iterator() and added a check when setting formats. --- .../macaco/conversion/MDateConverter.java | 8 +-- .../conversion/MLocalDateConverter.java | 50 ++++++++++++++----- .../conversion/MLocalDateTimeConverter.java | 50 ++++++++++++++----- .../macaco/conversion/MNumberConverter.java | 8 +-- 4 files changed, 86 insertions(+), 30 deletions(-) diff --git a/src/java/com/marcozanon/macaco/conversion/MDateConverter.java b/src/java/com/marcozanon/macaco/conversion/MDateConverter.java index 4461edf..9ac0925 100644 --- a/src/java/com/marcozanon/macaco/conversion/MDateConverter.java +++ b/src/java/com/marcozanon/macaco/conversion/MDateConverter.java @@ -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 i = dateFormats.iterator(); - while (i.hasNext()) { - MDateConverter.checkDateFormat(i.next()); + for (String dateFormat: dateFormats) { + MDateConverter.checkDateFormat(dateFormat); } } // diff --git a/src/java/com/marcozanon/macaco/conversion/MLocalDateConverter.java b/src/java/com/marcozanon/macaco/conversion/MLocalDateConverter.java index fa5e777..6eef1b2 100644 --- a/src/java/com/marcozanon/macaco/conversion/MLocalDateConverter.java +++ b/src/java/com/marcozanon/macaco/conversion/MLocalDateConverter.java @@ -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 dateFormats = new LinkedHashSet(); + protected Locale locale = null; /* */ - public MLocalDateConverter(String defaultDateFormat) { + public MLocalDateConverter(String defaultDateFormat, Locale locale) { super(); // this.addDateFormat(defaultDateFormat); + this.setLocale(locale); } - public MLocalDateConverter(LinkedHashSet dateFormats) { + public MLocalDateConverter(LinkedHashSet 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 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()); } } diff --git a/src/java/com/marcozanon/macaco/conversion/MLocalDateTimeConverter.java b/src/java/com/marcozanon/macaco/conversion/MLocalDateTimeConverter.java index 956a7d2..2d00e77 100644 --- a/src/java/com/marcozanon/macaco/conversion/MLocalDateTimeConverter.java +++ b/src/java/com/marcozanon/macaco/conversion/MLocalDateTimeConverter.java @@ -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 datetimeFormats = new LinkedHashSet(); + protected Locale locale = null; /* */ - public MLocalDateTimeConverter(String defaultDatetimeFormat) { + public MLocalDateTimeConverter(String defaultDatetimeFormat, Locale locale) { super(); // this.addDatetimeFormat(defaultDatetimeFormat); + this.setLocale(locale); } - public MLocalDateTimeConverter(LinkedHashSet datetimeFormats) { + public MLocalDateTimeConverter(LinkedHashSet 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 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()); } } diff --git a/src/java/com/marcozanon/macaco/conversion/MNumberConverter.java b/src/java/com/marcozanon/macaco/conversion/MNumberConverter.java index 05d3219..a77a66c 100644 --- a/src/java/com/marcozanon/macaco/conversion/MNumberConverter.java +++ b/src/java/com/marcozanon/macaco/conversion/MNumberConverter.java @@ -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 i = numberFormats.iterator(); - while (i.hasNext()) { - MNumberConverter.checkNumberFormat(i.next()); + for (String numberFormat: numberFormats) { + MNumberConverter.checkNumberFormat(numberFormat); } } // -- 2.30.2