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