Skip to content

Commit

Permalink
Cleanup message concatenation
Browse files Browse the repository at this point in the history
  • Loading branch information
Morten Haraldsen committed Jan 30, 2024
1 parent 257179f commit 2c7b27a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/ethlo/time/DateTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ private String toString(final DateTime date, final Field lastIncluded, final int
{
if (lastIncluded.ordinal() > date.getMostGranularField().ordinal())
{
throw new DateTimeFormatException("Requested granularity was " + lastIncluded.name() + ", but contains only granularity " + date.getMostGranularField().name());
throw new DateTimeFormatException(String.format("Requested granularity was %s, but contains only granularity %s", lastIncluded.name(), date.getMostGranularField().name()));
}
final TimezoneOffset tz = date.getOffset().orElse(null);
final char[] buffer = new char[35];
Expand Down Expand Up @@ -580,7 +580,7 @@ private void validated()

if (second > 59)
{
throw new DateTimeException("Invalid value for SecondOfMinute (valid values 0 - 59): " + second);
throw new DateTimeException(String.format("Invalid value for SecondOfMinute (valid values 0 - 59): %d", second));
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/main/java/com/ethlo/time/internal/ErrorUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ private ErrorUtil()

public static DateTimeParseException raiseUnexpectedCharacter(String chars, int index)
{
throw new DateTimeParseException("Unexpected character " + chars.charAt(index) + " at position " + (index + 1) + ": " + chars, chars, index);
throw new DateTimeParseException(String.format("Unexpected character %s at position %d: %s", chars.charAt(index), index + 1, chars), chars, index);
}

public static DateTimeParseException raiseUnexpectedEndOfText(final String chars, final int offset)
{
throw new DateTimeParseException("Unexpected end of input: " + chars, chars, offset);
throw new DateTimeParseException(String.format("Unexpected end of input: %s", chars), chars, offset);
}

public static DateTimeParseException raiseMissingGranularity(Field field, final String chars, final int offset)
{
throw new DateTimeParseException("Unexpected end of input, missing field " + field.name() + ": " + chars, chars, offset);
throw new DateTimeParseException(String.format("Unexpected end of input, missing field %s: %s", field.name(), chars), chars, offset);
}

public static void assertPositionContains(Field field, String chars, int index, char expected)
Expand All @@ -56,20 +56,20 @@ public static void assertPositionContains(Field field, String chars, int index,

if (chars.charAt(index) != expected)
{
throw new DateTimeParseException("Expected character " + expected + " at position " + (index + 1) + ", found " + chars.charAt(index) + ": " + chars, chars, index);
throw new DateTimeParseException(String.format("Expected character %s at position %d, found %s: %s", expected, index + 1, chars.charAt(index), chars), chars, index);
}
}

public static void assertFractionDigits(String chars, int fractionDigits, int idx)
{
if (fractionDigits == 0)
{
throw new DateTimeParseException("Must have at least 1 fraction digit: " + chars, chars, idx);
throw new DateTimeParseException(String.format("Must have at least 1 fraction digit: %s", chars), chars, idx);
}

if (fractionDigits > MAX_FRACTION_DIGITS)
{
throw new DateTimeParseException("Maximum supported number of fraction digits in second is 9, got " + fractionDigits + ": " + chars, chars, idx);
throw new DateTimeParseException(String.format("Maximum supported number of fraction digits in second is 9, got %d: %s", fractionDigits, chars), chars, idx);
}
}
}
13 changes: 7 additions & 6 deletions src/main/java/com/ethlo/time/internal/ITUParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ private static void assertAllowedDateTimeSeparator(final int offset, final Strin
final char needle = chars.charAt(index);
if (!config.isDateTimeSeparator(needle))
{
throw new DateTimeParseException("Expected character " + (config.getDateTimeSeparators().length > 1 ? Arrays.toString(config.getDateTimeSeparators()) : config.getDateTimeSeparators()[0]) + " at position " + (index + 1) + ", found " + chars.charAt(index) + ": " + chars, chars, index);
final String allowedCharStr = config.getDateTimeSeparators().length > 1 ? Arrays.toString(config.getDateTimeSeparators()) : Character.toString(config.getDateTimeSeparators()[0]);
throw new DateTimeParseException(String.format("Expected character %s at position %d, found %s: %s", allowedCharStr, index + 1, chars.charAt(index), chars), chars, index);
}
}

Expand All @@ -111,7 +112,7 @@ private static TimezoneOffset parseTimezone(int offset, final ParseConfig parseC

if (left < 6)
{
throw new DateTimeParseException("Invalid timezone offset: " + chars, chars, idx);
throw new DateTimeParseException(String.format("Invalid timezone offset: %s", chars), chars, idx);
}

int hours = parsePositiveInt(chars, idx + 1, idx + 3);
Expand All @@ -137,7 +138,7 @@ private static void assertNoMoreChars(final int offset, final ParseConfig parseC
{
if (chars.length() > lastUsed + 1)
{
throw new DateTimeParseException("Trailing junk data after position " + (lastUsed + 2) + ": " + chars, chars, lastUsed + 1);
throw new DateTimeParseException(String.format("Trailing junk data after position %d: %s", lastUsed + 2, chars), chars, lastUsed + 1);
}
}
}
Expand All @@ -153,12 +154,12 @@ public static DateTime parseLenient(final String chars, final ParseConfig parseC

if (availableLength < 0)
{
throw new IndexOutOfBoundsException("offset is " + offset + " which is equal to or larger than the input length of " + chars.length());
throw new IndexOutOfBoundsException(String.format("offset is %d which is equal to or larger than the input length of %d", offset, chars.length()));
}

if (offset < 0)
{
throw new IndexOutOfBoundsException("offset cannot be negative, was " + offset);
throw new IndexOutOfBoundsException(String.format("offset cannot be negative, was %d", offset));
}

// Date portion
Expand Down Expand Up @@ -289,6 +290,6 @@ public static OffsetDateTime parseDateTime(final String chars, int offset)
}
final Field field = dateTime.getMostGranularField();
final Field nextGranularity = Field.values()[field.ordinal() + 1];
throw new DateTimeParseException("Unexpected end of input, missing field " + nextGranularity + ": " + chars, chars, field.getRequiredLength());
throw new DateTimeParseException(String.format("Unexpected end of input, missing field %s: %s", nextGranularity, chars), chars, field.getRequiredLength());
}
}

0 comments on commit 2c7b27a

Please sign in to comment.