Skip to content

Package: DateConverter

DateConverter

nameinstructionbranchcomplexitylinemethod
DateConverter()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
convertToAttribute(Object)
M: 12 C: 28
70%
M: 3 C: 5
63%
M: 3 C: 2
40%
M: 0 C: 7
100%
M: 0 C: 1
100%
convertToAxiomValue(Date)
M: 4 C: 7
64%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 2
100%
M: 0 C: 1
100%
offsetDateTimeToDate(OffsetDateTime)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 1
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
supportsAxiomValueType(Class)
M: 0 C: 12
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2022 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.oom.converter.datetime;
16:
17: import cz.cvut.kbss.jopa.datatype.DateTimeUtil;
18: import cz.cvut.kbss.jopa.datatype.xsd.XsdDateTimeMapper;
19: import cz.cvut.kbss.jopa.oom.converter.ConverterWrapper;
20: import cz.cvut.kbss.jopa.vocabulary.XSD;
21: import cz.cvut.kbss.ontodriver.model.Literal;
22:
23: import java.time.OffsetDateTime;
24: import java.util.Date;
25:
26: /**
27: * Converts between a xsd:dateTime representation and {@link Date} instances.
28: * <p>
29: * Supported representations are {@link OffsetDateTime} and {@link Literal}.
30: * <p>
31: * Note that when transforming to axiom value, UTC time zone is assumed to provide consistent results.
32: */
33: public class DateConverter implements ConverterWrapper<Date, Object> {
34:
35: @Override
36: public Object convertToAxiomValue(Date value) {
37:• assert value != null;
38: return DateTimeUtil.toDateTime(value.toInstant());
39: }
40:
41: @Override
42: public Date convertToAttribute(Object value) {
43:• assert value != null;
44:• if (value instanceof OffsetDateTime) {
45: return offsetDateTimeToDate((OffsetDateTime) value);
46: } else {
47:• assert value instanceof Literal;
48: final Literal literal = (Literal) value;
49:• assert XSD.DATETIME.equals(literal.getDatatype());
50: return offsetDateTimeToDate(XsdDateTimeMapper.map(literal.getLexicalForm()));
51: }
52: }
53:
54: private static Date offsetDateTimeToDate(OffsetDateTime value) {
55: return Date.from(value.toInstant());
56: }
57:
58: @Override
59: public boolean supportsAxiomValueType(Class<?> type) {
60:• return OffsetDateTime.class.isAssignableFrom(type) || Literal.class.isAssignableFrom(type);
61: }
62: }