Skip to content

Commit

Permalink
Cleanup and more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Morten Haraldsen committed Feb 3, 2024
1 parent 0484a8a commit c70bbad
Show file tree
Hide file tree
Showing 23 changed files with 200 additions and 131 deletions.
18 changes: 9 additions & 9 deletions src/main/java/com/ethlo/time/DateTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
* #L%
*/

import static com.ethlo.time.internal.ITUParser.DATE_SEPARATOR;
import static com.ethlo.time.internal.ITUParser.SEPARATOR_UPPER;
import static com.ethlo.time.internal.ITUParser.TIME_SEPARATOR;
import static com.ethlo.time.internal.ITUFormatter.finish;
import static com.ethlo.time.internal.LeapSecondHandler.LEAP_SECOND_SECONDS;
import static com.ethlo.time.internal.fixed.ITUParser.DATE_SEPARATOR;
import static com.ethlo.time.internal.fixed.ITUParser.SEPARATOR_UPPER;
import static com.ethlo.time.internal.fixed.ITUParser.TIME_SEPARATOR;
import static com.ethlo.time.internal.fixed.ITUFormatter.finish;
import static com.ethlo.time.internal.util.LeapSecondHandler.LEAP_SECOND_SECONDS;

import java.time.DateTimeException;
import java.time.Instant;
Expand All @@ -43,10 +43,10 @@
import java.util.Optional;

import com.ethlo.time.internal.DateTimeFormatException;
import com.ethlo.time.internal.DateTimeMath;
import com.ethlo.time.internal.DefaultLeapSecondHandler;
import com.ethlo.time.internal.LeapSecondHandler;
import com.ethlo.time.internal.LimitedCharArrayIntegerUtil;
import com.ethlo.time.internal.util.DateTimeMath;
import com.ethlo.time.internal.util.DefaultLeapSecondHandler;
import com.ethlo.time.internal.util.LeapSecondHandler;
import com.ethlo.time.internal.util.LimitedCharArrayIntegerUtil;

/**
* Container class for parsed date/date-time data. The {@link #getMostGranularField()} contains the highest granularity field found, like MONTH, MINUTE, SECOND.
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/ethlo/time/ITU.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
import java.time.YearMonth;
import java.time.format.DateTimeParseException;

import com.ethlo.time.internal.ITUFormatter;
import com.ethlo.time.internal.ITUParser;
import com.ethlo.time.internal.fixed.ITUFormatter;
import com.ethlo.time.internal.fixed.ITUParser;

/**
* The main access to the parse and formatting functions in this library.
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/ethlo/time/ParseConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
* #L%
*/

import static com.ethlo.time.internal.ITUParser.SEPARATOR_LOWER;
import static com.ethlo.time.internal.ITUParser.SEPARATOR_SPACE;
import static com.ethlo.time.internal.ITUParser.SEPARATOR_UPPER;
import static com.ethlo.time.internal.fixed.ITUParser.SEPARATOR_LOWER;
import static com.ethlo.time.internal.fixed.ITUParser.SEPARATOR_SPACE;
import static com.ethlo.time.internal.fixed.ITUParser.SEPARATOR_UPPER;

import java.util.Arrays;
import java.util.Optional;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ethlo.time.internal;
package com.ethlo.time.internal.fixed;

/*-
* #%L
Expand All @@ -20,20 +20,22 @@
* #L%
*/

import static com.ethlo.time.internal.ITUParser.DATE_SEPARATOR;
import static com.ethlo.time.internal.ITUParser.FRACTION_SEPARATOR;
import static com.ethlo.time.internal.ITUParser.MAX_FRACTION_DIGITS;
import static com.ethlo.time.internal.ITUParser.MINUS;
import static com.ethlo.time.internal.ITUParser.PLUS;
import static com.ethlo.time.internal.ITUParser.SEPARATOR_UPPER;
import static com.ethlo.time.internal.ITUParser.TIME_SEPARATOR;
import static com.ethlo.time.internal.ITUParser.ZULU_UPPER;
import static com.ethlo.time.internal.fixed.ITUParser.DATE_SEPARATOR;
import static com.ethlo.time.internal.fixed.ITUParser.FRACTION_SEPARATOR;
import static com.ethlo.time.internal.fixed.ITUParser.MAX_FRACTION_DIGITS;
import static com.ethlo.time.internal.fixed.ITUParser.MINUS;
import static com.ethlo.time.internal.fixed.ITUParser.PLUS;
import static com.ethlo.time.internal.fixed.ITUParser.SEPARATOR_UPPER;
import static com.ethlo.time.internal.fixed.ITUParser.TIME_SEPARATOR;
import static com.ethlo.time.internal.fixed.ITUParser.ZULU_UPPER;

import java.time.OffsetDateTime;
import java.time.ZoneOffset;

import com.ethlo.time.Field;
import com.ethlo.time.TimezoneOffset;
import com.ethlo.time.internal.DateTimeFormatException;
import com.ethlo.time.internal.util.LimitedCharArrayIntegerUtil;

public class ITUFormatter
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ethlo.time.internal;
package com.ethlo.time.internal.fixed;

/*-
* #%L
Expand All @@ -20,13 +20,13 @@
* #L%
*/

import static com.ethlo.time.internal.ErrorUtil.assertFractionDigits;
import static com.ethlo.time.internal.ErrorUtil.assertPositionContains;
import static com.ethlo.time.internal.ErrorUtil.raiseUnexpectedCharacter;
import static com.ethlo.time.internal.ErrorUtil.raiseUnexpectedEndOfText;
import static com.ethlo.time.internal.LimitedCharArrayIntegerUtil.DIGIT_9;
import static com.ethlo.time.internal.LimitedCharArrayIntegerUtil.ZERO;
import static com.ethlo.time.internal.LimitedCharArrayIntegerUtil.parsePositiveInt;
import static com.ethlo.time.internal.util.ErrorUtil.assertFractionDigits;
import static com.ethlo.time.internal.util.ErrorUtil.assertPositionContains;
import static com.ethlo.time.internal.util.ErrorUtil.raiseUnexpectedCharacter;
import static com.ethlo.time.internal.util.ErrorUtil.raiseUnexpectedEndOfText;
import static com.ethlo.time.internal.util.LimitedCharArrayIntegerUtil.DIGIT_9;
import static com.ethlo.time.internal.util.LimitedCharArrayIntegerUtil.ZERO;
import static com.ethlo.time.internal.util.LimitedCharArrayIntegerUtil.parsePositiveInt;

import java.text.ParsePosition;
import java.time.OffsetDateTime;
Expand All @@ -37,6 +37,7 @@
import com.ethlo.time.Field;
import com.ethlo.time.ParseConfig;
import com.ethlo.time.TimezoneOffset;
import com.ethlo.time.internal.util.ArrayUtils;
import com.ethlo.time.token.DateTimeParser;

public class ITUParser implements DateTimeParser
Expand All @@ -46,11 +47,11 @@ public class ITUParser implements DateTimeParser
public static final char SEPARATOR_UPPER = 'T';
public static final char SEPARATOR_LOWER = 't';
public static final char SEPARATOR_SPACE = ' ';
static final char PLUS = '+';
static final char MINUS = '-';
public static final char PLUS = '+';
public static final char MINUS = '-';
public static final char FRACTION_SEPARATOR = '.';
static final char ZULU_UPPER = 'Z';
private static final char ZULU_LOWER = 'z';
public static final char ZULU_UPPER = 'Z';
public static final char ZULU_LOWER = 'z';
public static final int MAX_FRACTION_DIGITS = 9;
public static final int RADIX = 10;
public static final int DIGITS_IN_NANO = 9;
Expand Down Expand Up @@ -78,7 +79,7 @@ private static DateTime handleTime(final int offset, final ParseConfig parseConf
return new DateTime(Field.MINUTE, year, month, day, hour, minute, 0, 0, zoneOffset, 0, charLength);

default:
throw raiseUnexpectedCharacter(chars, offset + 16);
throw raiseUnexpectedCharacter(chars, offset + 16, TIME_SEPARATOR, ZULU_UPPER, ZULU_LOWER, PLUS, MINUS);

Check warning on line 82 in src/main/java/com/ethlo/time/internal/fixed/ITUParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/ethlo/time/internal/fixed/ITUParser.java#L82

Added line #L82 was not covered by tests
}
}

Expand Down Expand Up @@ -111,7 +112,7 @@ private static TimezoneOffset parseTimezone(int offset, final ParseConfig parseC
final char sign = chars.charAt(idx);
if (sign != PLUS && sign != MINUS)
{
raiseUnexpectedCharacter(chars, idx);
raiseUnexpectedCharacter(chars, idx, ZULU_UPPER, ZULU_LOWER, PLUS, MINUS);

Check warning on line 115 in src/main/java/com/ethlo/time/internal/fixed/ITUParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/ethlo/time/internal/fixed/ITUParser.java#L115

Added line #L115 was not covered by tests
}

if (left < 6)
Expand Down Expand Up @@ -260,7 +261,7 @@ else if (c == PLUS || c == MINUS)
}
else
{
throw raiseUnexpectedCharacter(chars, offset + 19);
throw raiseUnexpectedCharacter(chars, offset + 19, ArrayUtils.merge(parseConfig.getFractionSeparators(), new char[]{ZULU_UPPER, ZULU_LOWER, PLUS, MINUS}));

Check warning on line 264 in src/main/java/com/ethlo/time/internal/fixed/ITUParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/ethlo/time/internal/fixed/ITUParser.java#L264

Added line #L264 was not covered by tests
}
}
else if (length == 19)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ethlo.time.token;
package com.ethlo.time.internal.token;

/*-
* #%L
Expand All @@ -9,9 +9,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -20,11 +20,12 @@
* #L%
*/

import com.ethlo.time.Field;
import com.ethlo.time.internal.LimitedCharArrayIntegerUtil;

import java.text.ParsePosition;

import com.ethlo.time.Field;
import com.ethlo.time.internal.util.LimitedCharArrayIntegerUtil;
import com.ethlo.time.token.DateTimeToken;

public class DigitsToken implements DateTimeToken
{
private final Field field;
Expand All @@ -36,16 +37,6 @@ public DigitsToken(Field field, int length)
this.length = length;
}

public static DateTimeToken ofTwo(Field field)
{
return new DigitsToken(field, 2);
}

public static DateTimeToken ofFour(Field field)
{
return new DigitsToken(field, 4);
}

@Override
public int read(String text, ParsePosition parsePosition)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ethlo.time.token;
package com.ethlo.time.internal.token;

/*-
* #%L
Expand All @@ -9,9 +9,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -20,12 +20,13 @@
* #L%
*/

import static com.ethlo.time.internal.LimitedCharArrayIntegerUtil.DIGIT_9;
import static com.ethlo.time.internal.LimitedCharArrayIntegerUtil.ZERO;
import static com.ethlo.time.internal.util.LimitedCharArrayIntegerUtil.DIGIT_9;
import static com.ethlo.time.internal.util.LimitedCharArrayIntegerUtil.ZERO;

import java.text.ParsePosition;

import com.ethlo.time.Field;
import com.ethlo.time.token.DateTimeToken;

public class FractionsToken implements DateTimeToken
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ethlo.time.token;
package com.ethlo.time.internal.token;

/*-
* #%L
Expand All @@ -23,7 +23,8 @@
import java.text.ParsePosition;

import com.ethlo.time.Field;
import com.ethlo.time.internal.ErrorUtil;
import com.ethlo.time.internal.util.ErrorUtil;
import com.ethlo.time.token.DateTimeToken;

public class SeparatorToken implements DateTimeToken
{
Expand All @@ -34,11 +35,6 @@ public SeparatorToken(char separator)
this.separator = separator;
}

public static DateTimeToken separator(char c)
{
return new SeparatorToken(c);
}

@Override
public int read(final String text, final ParsePosition parsePosition)
{
Expand All @@ -53,7 +49,7 @@ else if (text.length() <= index)
}
else if (text.charAt(index) != separator)
{
ErrorUtil.raiseUnexpectedCharacter(text, index);
ErrorUtil.raiseUnexpectedCharacter(text, index, separator);

Check warning on line 52 in src/main/java/com/ethlo/time/internal/token/SeparatorToken.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/ethlo/time/internal/token/SeparatorToken.java#L52

Added line #L52 was not covered by tests
}
parsePosition.setIndex(index + 1);
return 1;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ethlo.time.token;
package com.ethlo.time.internal.token;

/*-
* #%L
Expand All @@ -24,8 +24,8 @@
import java.time.format.DateTimeParseException;
import java.util.Arrays;

import com.ethlo.time.Field;
import com.ethlo.time.internal.ErrorUtil;
import com.ethlo.time.internal.util.ErrorUtil;
import com.ethlo.time.token.DateTimeToken;

public class SeparatorsToken implements DateTimeToken
{
Expand Down Expand Up @@ -56,10 +56,4 @@ public int read(final String text, final ParsePosition parsePosition)
}
throw new DateTimeParseException(String.format("Expected character %s at position %d, found %s: %s", Arrays.toString(separators), index + 1, text.charAt(index), text), text, index);

Check warning on line 57 in src/main/java/com/ethlo/time/internal/token/SeparatorsToken.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/ethlo/time/internal/token/SeparatorsToken.java#L57

Added line #L57 was not covered by tests
}

@Override
public Field getField()
{
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ethlo.time.token;
package com.ethlo.time.internal.token;

/*-
* #%L
Expand All @@ -9,9 +9,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -20,13 +20,18 @@
* #L%
*/

import static com.ethlo.time.internal.ErrorUtil.raiseUnexpectedCharacter;
import static com.ethlo.time.internal.LimitedCharArrayIntegerUtil.parsePositiveInt;
import static com.ethlo.time.internal.fixed.ITUParser.MINUS;
import static com.ethlo.time.internal.fixed.ITUParser.PLUS;
import static com.ethlo.time.internal.fixed.ITUParser.ZULU_LOWER;
import static com.ethlo.time.internal.fixed.ITUParser.ZULU_UPPER;
import static com.ethlo.time.internal.util.ErrorUtil.raiseUnexpectedCharacter;
import static com.ethlo.time.internal.util.LimitedCharArrayIntegerUtil.parsePositiveInt;

import java.text.ParsePosition;
import java.time.format.DateTimeParseException;

import com.ethlo.time.Field;
import com.ethlo.time.token.DateTimeToken;

public class TimeZoneOffsetToken implements DateTimeToken
{
Expand All @@ -51,7 +56,7 @@ public int read(final String text, final ParsePosition parsePosition)
final char sign = text.charAt(idx);
if (sign != '+' && sign != '-')
{
raiseUnexpectedCharacter(text, idx);
raiseUnexpectedCharacter(text, idx, ZULU_UPPER, ZULU_LOWER, PLUS, MINUS);

Check warning on line 59 in src/main/java/com/ethlo/time/internal/token/TimeZoneOffsetToken.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/ethlo/time/internal/token/TimeZoneOffsetToken.java#L59

Added line #L59 was not covered by tests
}

if (left < 6)
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/ethlo/time/internal/util/ArrayUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.ethlo.time.internal.util;

public class ArrayUtils
{
public static char[] merge(char[] a, char[] b)
{
final char[] result = new char[a.length + b.length];
System.arraycopy(a, 0, result, 0, a.length);
System.arraycopy(b, 0, result, a.length, b.length);
return result;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ethlo.time.internal;
package com.ethlo.time.internal.util;

/*-
* #%L
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ethlo.time.internal;
package com.ethlo.time.internal.util;

/*-
* #%L
Expand Down
Loading

0 comments on commit c70bbad

Please sign in to comment.