-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8930e0f
commit d27f52d
Showing
3 changed files
with
79 additions
and
1 deletion.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
modules/hcc-common/src/main/java/net/sf/dz3r/common/DurationParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package net.sf.dz3r.common; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
import java.time.Duration; | ||
import java.time.format.DateTimeParseException; | ||
|
||
public class DurationParser { | ||
|
||
private final Logger logger = LogManager.getLogger(); | ||
|
||
public Duration parse(String source) { | ||
|
||
try { | ||
return Duration.parse(source); | ||
} catch (DateTimeParseException ex) { | ||
try { | ||
logger.warn("'{}' doesn't parse, trying PT{}", source, source); | ||
return Duration.parse("PT" + source); | ||
} catch (DateTimeParseException ex2) { | ||
try { | ||
logger.warn("'PT{}' doesn't parse, trying {} hours", source, source); | ||
return Duration.ofHours(Long.parseLong(source)); | ||
} catch (NumberFormatException ex3) { | ||
|
||
// Enough is enough | ||
logger.warn("Failed to parse '{}' as a) Duration, b) PT$duration, c) hours; giving up and returning 0", source); | ||
return Duration.ZERO; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
modules/hcc-common/src/test/java/net/sf/dz3r/common/DurationParserTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package net.sf.dz3r.common; | ||
|
||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import java.time.Duration; | ||
import java.util.stream.Stream; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
|
||
class DurationParserTest { | ||
|
||
private final DurationParser dp = new DurationParser(); | ||
|
||
@ParameterizedTest | ||
@MethodSource("durationProvider") | ||
void parse(String2Duration source) { | ||
|
||
var actual = dp.parse(source.s); | ||
|
||
assertThat(actual).isEqualTo(source.d); | ||
} | ||
|
||
private static Stream<String2Duration> durationProvider() { | ||
return Stream.of( | ||
new String2Duration("PT200H", Duration.ofHours(200)), | ||
new String2Duration("PT200", Duration.ofHours(0)), | ||
new String2Duration("200H", Duration.ofHours(200)), | ||
new String2Duration("200M", Duration.ofMinutes(200)), | ||
new String2Duration("200S", Duration.ofSeconds(200)), | ||
new String2Duration("40H3M4S", Duration.parse("PT40H3M4S")), | ||
new String2Duration("200", Duration.ofHours(200)), | ||
new String2Duration("0", Duration.ofHours(0)), | ||
new String2Duration("huh?", Duration.ofHours(0)) | ||
); | ||
} | ||
|
||
private record String2Duration( | ||
String s, | ||
Duration d | ||
) {} | ||
} |