Skip to content

Package: JsonNodeFactory$LiteralType

JsonNodeFactory$LiteralType

nameinstructionbranchcomplexitylinemethod
static {...}
M: 0 C: 21
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%

Coverage

1: /*
2: * JB4JSON-LD
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.jsonld.serialization;
19:
20: import cz.cvut.kbss.jsonld.common.CollectionType;
21: import cz.cvut.kbss.jsonld.serialization.model.*;
22:
23: import java.util.Collection;
24: import java.util.List;
25: import java.util.Set;
26:
27: /**
28: * Factory for constructing {@link JsonNode} instances.
29: */
30: public class JsonNodeFactory {
31:
32: private JsonNodeFactory() {
33: throw new AssertionError();
34: }
35:
36: private enum LiteralType {
37: BOOLEAN, NUMBER, STRING
38: }
39:
40: public static LiteralNode<?> createLiteralNode(Object value) {
41: return createLiteralNode(null, value);
42: }
43:
44: public static LiteralNode<?> createLiteralNode(String name, Object value) {
45: final LiteralType type = determineLiteralType(value);
46: switch (type) {
47: case BOOLEAN:
48: return createBooleanLiteralNode(name, (Boolean) value);
49: case NUMBER:
50: return createNumericLiteralNode(name, (Number) value);
51: default:
52: return createStringLiteralNode(name, value.toString());
53: }
54: }
55:
56: private static LiteralType determineLiteralType(Object value) {
57: if (value instanceof Boolean) {
58: return LiteralType.BOOLEAN;
59: } else if (value instanceof Number) {
60: return LiteralType.NUMBER;
61: }
62: return LiteralType.STRING;
63: }
64:
65: public static BooleanLiteralNode createBooleanLiteralNode(String name, Boolean value) {
66: return name != null ? new BooleanLiteralNode(name, value) : new BooleanLiteralNode(value);
67: }
68:
69: public static NumericLiteralNode<Number> createNumericLiteralNode(String name, Number value) {
70: return name != null ? new NumericLiteralNode<>(name, value) : new NumericLiteralNode<>(value);
71: }
72:
73: public static StringLiteralNode createStringLiteralNode(String name, String value) {
74: return name != null ? new StringLiteralNode(name, value) : new StringLiteralNode(value);
75: }
76:
77: /**
78: * Creates collection node with the specified name, for the specified collection.
79: *
80: * @param name Name of the node (attribute)
81: * @param value The collection. It is used only to determine the type of the target node, no values are added to the
82: * result
83: * @return An empty collection node
84: */
85: public static CollectionNode<?> createCollectionNode(String name, Collection<?> value) {
86: final CollectionType type = determineCollectionType(value);
87: switch (type) {
88: case LIST:
89: return new ListNode(name);
90: case SET:
91: return createSetNode(name);
92: default:
93: throw new IllegalArgumentException("Unsupported collection type " + type);
94: }
95: }
96:
97: private static CollectionType determineCollectionType(Collection<?> collection) {
98: if (collection instanceof List) {
99: return CollectionType.LIST;
100: } else if (collection instanceof Set) {
101: return CollectionType.SET;
102: } else {
103: throw new IllegalArgumentException("Unsupported collection type " + collection.getClass());
104: }
105: }
106:
107: public static SetNode createSetNode(String name) {
108: return new SetNode(name);
109: }
110:
111: public static SetNode createCollectionNodeFromArray(String name) {
112: return createSetNode(name);
113: }
114:
115: public static ObjectNode createObjectNode() {
116: return new ObjectNode();
117: }
118:
119: public static ObjectNode createObjectNode(String name) {
120: return new ObjectNode(name);
121: }
122:
123: public static ObjectIdNode createObjectIdNode(String name, Object id) {
124: return new ObjectIdNode(name, id.toString());
125: }
126: }