Skip to content

Package: XsdDurationMapper

XsdDurationMapper

nameinstructionbranchcomplexitylinemethod
XsdDurationMapper()
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
map(String)
M: 0 C: 15
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%

Coverage

1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jopa.datatype.xsd;
19:
20: import cz.cvut.kbss.jopa.datatype.exception.DatatypeMappingException;
21:
22: import java.time.Duration;
23: import java.time.Period;
24: import java.time.format.DateTimeParseException;
25:
26: /**
27: * Maps {@link cz.cvut.kbss.jopa.vocabulary.XSD#DURATION} values to Java {@link Duration}.
28: */
29: public class XsdDurationMapper {
30:
31: private XsdDurationMapper() {
32: throw new AssertionError();
33: }
34:
35: /**
36: * Maps the specified value to {@link Duration}.
37: *
38: * @param value Value to map
39: * @return Parsed duration
40: */
41: public static Object map(String value) {
42: try {
43: return Duration.parse(value);
44: } catch (DateTimeParseException e) {
45: try {
46: return Period.parse(value);
47: } catch (DateTimeParseException e2) {
48: throw new DatatypeMappingException("Unable to resolve value of xsd:duration " + value + ". " +
49: "Maybe it is too large for java.time.Duration, but contains time preventing it from being parsed to java.time.Period.", e);
50: }
51: }
52: }
53: }