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) 2017 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jsonld.serialization;
16:
17: import cz.cvut.kbss.jsonld.common.CollectionType;
18: import cz.cvut.kbss.jsonld.serialization.model.*;
19:
20: import java.net.URI;
21: import java.util.Collection;
22: import java.util.List;
23: import java.util.Set;
24:
25: /**
26: * Factory for constructing {@link JsonNode} instances.
27: */
28: public class JsonNodeFactory {
29:
30: private JsonNodeFactory() {
31: throw new AssertionError();
32: }
33:
34: private enum LiteralType {
35: BOOLEAN, NUMBER, STRING
36: }
37:
38: public static LiteralNode createLiteralNode(Object value) {
39: return createLiteralNode(null, value);
40: }
41:
42: public static LiteralNode createLiteralNode(String name, Object value) {
43: final LiteralType type = determineLiteralType(value);
44: LiteralNode node = null;
45: switch (type) {
46: case BOOLEAN:
47: node = name != null ? new BooleanLiteralNode(name, (Boolean) value) : new BooleanLiteralNode(
48: (Boolean) value);
49: break;
50: case NUMBER:
51: node = name != null ? new NumericLiteralNode<>(name, (Number) value) :
52: new NumericLiteralNode<>((Number) value);
53: break;
54: case STRING:
55: node = name != null ? new StringLiteralNode(name, value.toString()) :
56: new StringLiteralNode(value.toString());
57: break;
58: }
59: return node;
60: }
61:
62: private static LiteralType determineLiteralType(Object value) {
63: if (value instanceof Boolean) {
64: return LiteralType.BOOLEAN;
65: } else if (value instanceof Number) {
66: return LiteralType.NUMBER;
67: }
68: return LiteralType.STRING;
69: }
70:
71: /**
72: * Creates collection node for the specified collection.
73: * <p>
74: * The node is without name, so it cannot be used as attribute.
75: *
76: * @param value The collection. It is used only to determine the type of the target node, no values are added to the
77: * result
78: * @return An empty collection node
79: */
80: public static CollectionNode createCollectionNode(Collection<?> value) {
81: return createCollectionNode(null, value);
82: }
83:
84: /**
85: * Creates collection node with the specified name, for the specified collection.
86: *
87: * @param name Name of the node (attribute)
88: * @param value The collection. It is used only to determine the type of the target node, no values are added to the
89: * result
90: * @return An empty collection node
91: */
92: public static CollectionNode createCollectionNode(String name, Collection<?> value) {
93: final CollectionType type = determineCollectionType(value);
94: CollectionNode n = null;
95: switch (type) {
96: case LIST:
97: n = name != null ? new ListNode(name) : new ListNode();
98: break;
99: case SET:
100: n = name != null ? new SetNode(name) : new SetNode();
101: break;
102: }
103: return n;
104: }
105:
106: private static CollectionType determineCollectionType(Collection<?> collection) {
107: if (collection instanceof List) {
108: return CollectionType.LIST;
109: } else if (collection instanceof Set) {
110: return CollectionType.SET;
111: } else {
112: throw new IllegalArgumentException("Unsupported collection type " + collection.getClass());
113: }
114: }
115:
116: public static CollectionNode createCollectionNodeFromArray() {
117: return new SetNode();
118: }
119:
120: public static CollectionNode createCollectionNodeFromArray(String name) {
121: return new SetNode(name);
122: }
123:
124: public static ObjectNode createObjectNode() {
125: return new ObjectNode();
126: }
127:
128: public static ObjectNode createObjectNode(String name) {
129: return new ObjectNode(name);
130: }
131:
132: public static ObjectIdNode createObjectIdNode(Object id) {
133: return new ObjectIdNode(URI.create(id.toString()));
134: }
135:
136: public static ObjectIdNode createObjectIdNode(String name, Object id) {
137: return new ObjectIdNode(name, URI.create(id.toString()));
138: }
139: }