Skip to content

Package: EnumConverter

EnumConverter

nameinstructionbranchcomplexitylinemethod
EnumConverter(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: 26
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: 3
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: 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: * Copyright (C) 2020 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
5: * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
6: * version.
7: * <p>
8: * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
10: * details. You should have received a copy of the GNU General Public License along with this program. If not, see
11: * <http://www.gnu.org/licenses/>.
12: */
13: package cz.cvut.kbss.jopa.oom.converter;
14:
15: import cz.cvut.kbss.jopa.exception.UnsupportedTypeTransformation;
16:
17: /**
18: * Built-in converter for mapping to/from enum-valued attributes.
19: * <p>
20: * Uses the default {@link Enum#valueOf(Class, String)} method to transform string-based value loaded from repository to
21: * the appropriate attribute type. In the opposite direction, enum value is converted to {@code String}.
22: *
23: * @param <E>
24: */
25: public class EnumConverter<E extends Enum<E>> implements ConverterWrapper<E, Object> {
26:
27: private final Class<E> enumType;
28:
29: public EnumConverter(Class<E> enumType) {
30: this.enumType = enumType;
31: }
32:
33: @Override
34: public boolean supportsAxiomValueType(Class<?> type) {
35: return true;
36: }
37:
38: @Override
39: public Object convertToAxiomValue(E value) {
40: return value.toString();
41: }
42:
43: @Override
44: public E convertToAttribute(Object value) {
45: try {
46: return Enum.valueOf(enumType, ToLexicalFormConverter.INSTANCE.convertToAttribute(value));
47: } catch (IllegalArgumentException e) {
48: throw new UnsupportedTypeTransformation("Unable to transform value " + value + " to enum " + enumType, e);
49: }
50: }
51: }