Skip to content

Method: convertToAttribute(Object)

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.oom.converter.datetime;
19:
20: import cz.cvut.kbss.jopa.datatype.DateTimeUtil;
21: import cz.cvut.kbss.jopa.datatype.xsd.XsdDateTimeMapper;
22: import cz.cvut.kbss.jopa.oom.converter.ConverterWrapper;
23: import cz.cvut.kbss.jopa.vocabulary.XSD;
24: import cz.cvut.kbss.ontodriver.model.Literal;
25:
26: import java.time.LocalDateTime;
27: import java.time.OffsetDateTime;
28:
29: /**
30: * Converts between Java 8 {@link LocalDateTime} and a supported xsd:dateTime representation.
31: * <p>
32: * This converter supports {@link OffsetDateTime} and {@link cz.cvut.kbss.ontodriver.model.Literal} as date time.
33: */
34: public class LocalDateTimeConverter implements ConverterWrapper<LocalDateTime, Object> {
35:
36: @Override
37: public Object convertToAxiomValue(LocalDateTime value) {
38: assert value != null;
39: return DateTimeUtil.toDateTime(value);
40: }
41:
42: @Override
43: public LocalDateTime convertToAttribute(Object value) {
44:• assert value != null;
45:• if (value instanceof OffsetDateTime) {
46: return ((OffsetDateTime) value).toLocalDateTime();
47: } else {
48:• assert value instanceof Literal;
49: final Literal literal = (Literal) value;
50:• assert XSD.DATETIME.equals(literal.getDatatype());
51: return XsdDateTimeMapper.map(literal.getLexicalForm()).toLocalDateTime();
52: }
53: }
54:
55: @Override
56: public boolean supportsAxiomValueType(Class<?> type) {
57: return OffsetDateTime.class.isAssignableFrom(type) || Literal.class.isAssignableFrom(type);
58: }
59: }