Skip to content

Package: JsonNodeFactory$LiteralType

JsonNodeFactory$LiteralType

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

Coverage

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