Skip to content

Package: StringEnumConverter

StringEnumConverter

nameinstructionbranchcomplexitylinemethod
StringEnumConverter(Class)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
convertToAttribute(Object)
M: 0 C: 17
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
convertToAxiomValue(Enum)
M: 0 C: 7
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
supportsAxiomValueType(Class)
M: 0 C: 2
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
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.oom.converter;
19:
20: import cz.cvut.kbss.jopa.datatype.exception.UnsupportedTypeTransformationException;
21:
22: /**
23: * Built-in converter for mapping to/from enum-valued attributes.
24: * <p>
25: * Uses the default {@link Enum#valueOf(Class, String)} method to transform string-based value loaded from repository to
26: * the appropriate attribute type. In the opposite direction, enum value is converted to {@code String}. It is used for
27: * enumerated attributes with {@link cz.cvut.kbss.jopa.model.annotations.EnumType#ORDINAL} configuration.
28: *
29: * @param <E> Enum type
30: */
31: public class StringEnumConverter<E extends Enum<E>> implements ConverterWrapper<E, Object> {
32:
33: private final Class<E> enumType;
34:
35: public StringEnumConverter(Class<E> enumType) {
36: this.enumType = enumType;
37: }
38:
39: @Override
40: public boolean supportsAxiomValueType(Class<?> type) {
41: return true;
42: }
43:
44: @Override
45: public Object convertToAxiomValue(E value) {
46:• return value != null ? value.toString() : null;
47: }
48:
49: @Override
50: public E convertToAttribute(Object value) {
51: try {
52: return Enum.valueOf(enumType, ToLexicalFormConverter.INSTANCE.convertToAttribute(value));
53: } catch (IllegalArgumentException e) {
54: throw new UnsupportedTypeTransformationException(
55: "Unable to transform value " + value + " to enum " + enumType, e);
56: }
57: }
58: }