Skip to content

Package: MappingUtils

MappingUtils

nameinstructionbranchcomplexitylinemethod
appendField(ObjectNode, String, Object)
M: 139 C: 0
0%
M: 20 C: 0
0%
M: 11 C: 0
0%
M: 25 C: 0
0%
M: 1 C: 0
0%
newArrayNode()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
newArrayNode(Collection)
M: 14 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
newArrayNode(Object[])
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
newArrayNodeFromPredicates(Collection)
M: 20 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
newNumericNode(long)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
newObjectNode()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
newObjectNode(KeyValueEntry[])
M: 26 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
newObjectNode(String, Object)
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
newTextNode(String)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2020 Eurotech and/or its affiliates and others
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License v1.0
6: * which accompanies this distribution, and is available at
7: * http://www.eclipse.org/legal/epl-v10.html
8: *
9: * Contributors:
10: * Eurotech - initial API and implementation
11: *******************************************************************************/
12: package org.eclipse.kapua.service.storable.model.utils;
13:
14: import com.fasterxml.jackson.databind.node.ArrayNode;
15: import com.fasterxml.jackson.databind.node.JsonNodeFactory;
16: import com.fasterxml.jackson.databind.node.NumericNode;
17: import com.fasterxml.jackson.databind.node.ObjectNode;
18: import com.fasterxml.jackson.databind.node.TextNode;
19: import org.eclipse.kapua.commons.util.KapuaDateUtils;
20: import org.eclipse.kapua.model.id.KapuaId;
21: import org.eclipse.kapua.service.storable.exception.InvalidValueMappingException;
22: import org.eclipse.kapua.service.storable.exception.MappingException;
23: import org.eclipse.kapua.service.storable.exception.UnsupportedTypeMappingException;
24: import org.eclipse.kapua.service.storable.model.id.StorableId;
25: import org.eclipse.kapua.service.storable.model.query.predicate.StorablePredicate;
26:
27: import java.text.ParseException;
28: import java.util.Arrays;
29: import java.util.Collection;
30: import java.util.Date;
31:
32: /**
33: * Utilities to manage the generation of the {@link ObjectNode}
34: *
35: * @since 1.3.0
36: */
37: public class MappingUtils {
38:
39: private final static JsonNodeFactory JSON_NODE_FACTORY = JsonNodeFactory.instance;
40:
41: private MappingUtils() {
42: }
43:
44: //
45: // Appends
46: //
47:
48: /**
49: * Appends the provided field/value to the object node
50: *
51: * @param node The {@link ObjectNode} to appends the field to.
52: * @param name The name of the node to append.
53: * @param value The value of the node to append.
54: * @throws UnsupportedTypeMappingException if the type of the given value is not one of the supported ones.
55: * @throws InvalidValueMappingException if the object is a {@link Date} and its value is not compatible with a Date.
56: * @since 1.3.0
57: */
58: public static void appendField(ObjectNode node, String name, Object value) throws MappingException {
59:• if (value instanceof String) {
60: node.set(name, JSON_NODE_FACTORY.textNode((String) value));
61:• } else if (value instanceof Boolean) {
62: node.set(name, JSON_NODE_FACTORY.booleanNode((Boolean) value));
63:• } else if (value instanceof Integer) {
64: node.set(name, JSON_NODE_FACTORY.numberNode((Integer) value));
65:• } else if (value instanceof Long) {
66: node.set(name, JSON_NODE_FACTORY.numberNode((Long) value));
67:• } else if (value instanceof Double) {
68: node.set(name, JSON_NODE_FACTORY.numberNode((Double) value));
69:• } else if (value instanceof Float) {
70: node.set(name, JSON_NODE_FACTORY.numberNode((Float) value));
71:• } else if (value instanceof byte[]) {
72: node.set(name, JSON_NODE_FACTORY.binaryNode((byte[]) value));
73:• } else if (value instanceof Date) {
74: try {
75: node.set(name, JSON_NODE_FACTORY.textNode(KapuaDateUtils.formatDate((Date) value)));
76: } catch (ParseException e) {
77: throw new InvalidValueMappingException(e, name, value, Date.class);
78: }
79:• } else if (value instanceof StorableId) {
80: node.set(name, JSON_NODE_FACTORY.textNode(value.toString()));
81:• } else if (value instanceof KapuaId) {
82: node.set(name, JSON_NODE_FACTORY.textNode(value.toString()));
83: } else {
84: throw new UnsupportedTypeMappingException(name, value);
85: }
86: }
87:
88: //
89: // New
90: //
91:
92: /**
93: * Instantiates a new {@link ArrayNode}.
94: *
95: * @return The newly instantiated {@link ArrayNode}.
96: * @since 1.3.0
97: */
98: public static ArrayNode newArrayNode() {
99: return JSON_NODE_FACTORY.arrayNode();
100: }
101:
102: /**
103: * Instantiates a new {@link ArrayNode}.
104: *
105: * @param fields The fields to add to the {@link ArrayNode}.
106: * @return The newly instantiated {@link ArrayNode}.
107: * @since 1.3.0
108: */
109: public static ArrayNode newArrayNode(Object[] fields) {
110: return newArrayNode(Arrays.asList(fields));
111: }
112:
113: /**
114: * Instantiates a new {@link ArrayNode}.
115: *
116: * @param collection The {@link Collection} to add to the {@link ArrayNode}.
117: * @return The newly instantiated {@link ArrayNode}.
118: * @since 1.3.0
119: */
120: public static ArrayNode newArrayNode(Collection<?> collection) {
121: ArrayNode arrayNode = newArrayNode();
122:
123: collection.stream().map(Object::toString).forEach(arrayNode::add);
124:
125: return arrayNode;
126: }
127:
128: /**
129: * Instantiates a new {@link ArrayNode}.
130: *
131: * @param storablePredicates The {@link Collection} of {@link StorablePredicate} to add to the {@link ArrayNode}.
132: * @return The newly instantiated {@link ArrayNode}.
133: * @since 1.3.0
134: */
135: public static ArrayNode newArrayNodeFromPredicates(Collection<StorablePredicate> storablePredicates) throws MappingException {
136: ArrayNode arrayNode = newArrayNode();
137:
138:• for (StorablePredicate predicate : storablePredicates) {
139: arrayNode.add(predicate.toSerializedMap());
140: }
141:
142: return arrayNode;
143: }
144:
145: /**
146: * Instantiates a new {@link ObjectNode}
147: *
148: * @return The newly instantiated {@link ObjectNode}
149: * @since 1.3.0
150: */
151: public static ObjectNode newObjectNode() {
152: return JSON_NODE_FACTORY.objectNode();
153: }
154:
155: /**
156: * Instantiates a new {@link ObjectNode} adding the given {@link KeyValueEntry}es.
157: *
158: * @param entries The {@link KeyValueEntry}es to be added.
159: * @return A newly instantiated {@link ObjectNode} with the {@link KeyValueEntry}es added.
160: * @throws MappingException if {@link #appendField(ObjectNode, String, Object)} {@code throws} {@link MappingException}
161: * @since 1.3.0
162: */
163: public static ObjectNode newObjectNode(KeyValueEntry[] entries) throws MappingException {
164: ObjectNode objectNode = newObjectNode();
165:
166:• for (KeyValueEntry entry : entries) {
167: appendField(objectNode, entry.getKey(), entry.getValue());
168: }
169:
170: return objectNode;
171: }
172:
173: /**
174: * Instantiates a new {@link ObjectNode} with the given name and value added as a field.
175: *
176: * @param name The name of the field to add.
177: * @param value The value of the field to add.
178: * @return A newly instantiated {@link ObjectNode} with the field added.
179: * @throws MappingException if {@link #appendField(ObjectNode, String, Object)} {@code throws} {@link MappingException}
180: * @since 1.3.0
181: */
182: public static ObjectNode newObjectNode(String name, Object value) throws MappingException {
183: ObjectNode objectNode = newObjectNode();
184:
185: appendField(objectNode, name, value);
186:
187: return objectNode;
188: }
189:
190: /**
191: * Instantiates a new {@link NumericNode} with the given value.
192: *
193: * @param number The value to add to the {@link NumericNode}.
194: * @return The The newly instantiated {@link NumericNode}
195: * @since 1.3.0
196: */
197: public static NumericNode newNumericNode(long number) {
198: return JSON_NODE_FACTORY.numberNode(number);
199: }
200:
201: /**
202: * Instantiates a new {@link TextNode} with the given text.
203: *
204: * @param text The text to add to the {@link NumericNode}.
205: * @return The The newly instantiated {@link NumericNode}
206: * @since 1.3.0
207: */
208: public static TextNode newTextNode(String text) {
209: return JSON_NODE_FACTORY.textNode(text);
210: }
211: }