blob: 919219c13caf5f100651aa713f8efaf7755a0ae7 [file] [log] [blame]
Tatu Saloranta2f823442011-12-23 20:25:27 -08001package com.fasterxml.jackson.databind.node;
Tatu Salorantae4f23bb2011-12-23 00:31:35 -08002
3import java.math.BigDecimal;
Tatu Saloranta29552ca2013-05-17 20:36:07 -07004import java.math.BigInteger;
Tatu Salorantae4f23bb2011-12-23 00:31:35 -08005
6import com.fasterxml.jackson.core.*;
Tatu Salorantaf0e232d2012-01-29 16:44:28 -08007import com.fasterxml.jackson.databind.JsonNode;
Tatu Saloranta04c9cf52015-04-01 20:37:16 -07008import com.fasterxml.jackson.databind.util.RawValue;
Tatu Salorantaf0e232d2012-01-29 16:44:28 -08009
Tatu Salorantae4f23bb2011-12-23 00:31:35 -080010/**
11 * This intermediate base class is used for all container nodes,
12 * specifically, array and object nodes.
13 */
Tatub471a042012-01-27 11:51:39 -080014public abstract class ContainerNode<T extends ContainerNode<T>>
Tatu Salorantae4f23bb2011-12-23 00:31:35 -080015 extends BaseJsonNode
Tatu Saloranta29552ca2013-05-17 20:36:07 -070016 implements JsonNodeCreator
Tatu Salorantae4f23bb2011-12-23 00:31:35 -080017{
Tatu Salorantaab9413a2019-08-19 19:12:06 -070018 private static final long serialVersionUID = 1L;
19
Tatu Salorantae4f23bb2011-12-23 00:31:35 -080020 /**
21 * We will keep a reference to the Object (usually TreeMapper)
22 * that can construct instances of nodes to add to this container
23 * node.
24 */
Tatub471a042012-01-27 11:51:39 -080025 protected final JsonNodeFactory _nodeFactory;
Tatu Salorantae4f23bb2011-12-23 00:31:35 -080026
Tatu Saloranta04c9cf52015-04-01 20:37:16 -070027 protected ContainerNode(JsonNodeFactory nc) {
Tatu Salorantae4f23bb2011-12-23 00:31:35 -080028 _nodeFactory = nc;
29 }
30
Tatu Saloranta92ca9f42019-06-04 22:15:47 -070031 protected ContainerNode() { _nodeFactory = null; } // only for JDK ser
32
Tatu Salorantae3ae58e2012-01-28 23:08:16 -080033 // all containers are mutable: can't define:
34// @Override public abstract <T extends JsonNode> T deepCopy();
Tatu Salorantae4f23bb2011-12-23 00:31:35 -080035
36 @Override
37 public abstract JsonToken asToken();
Matt Veitasf81a3f92016-04-04 07:45:49 -040038
Tatu Salorantae4f23bb2011-12-23 00:31:35 -080039 @Override
40 public String asText() { return ""; }
41
42 /*
43 /**********************************************************
Tatu Salorantae4f23bb2011-12-23 00:31:35 -080044 /* Methods reset as abstract to force real implementation
45 /**********************************************************
46 */
47
48 @Override
49 public abstract int size();
50
51 @Override
52 public abstract JsonNode get(int index);
53
54 @Override
55 public abstract JsonNode get(String fieldName);
56
57 /*
58 /**********************************************************
Tatu Saloranta8d846ae2019-01-11 19:58:51 -080059 /* JsonNodeCreator implementation, Enumerated/singleton types
Tatu Salorantae4f23bb2011-12-23 00:31:35 -080060 /**********************************************************
61 */
62
Tatu Saloranta8d846ae2019-01-11 19:58:51 -080063 @Override
64 public final BooleanNode booleanNode(boolean v) { return _nodeFactory.booleanNode(v); }
65
66 public JsonNode missingNode() {
67 return _nodeFactory.missingNode();
68 }
69
70 @Override
71 public final NullNode nullNode() { return _nodeFactory.nullNode(); }
72
73 /*
74 /**********************************************************
75 /* JsonNodeCreator implementation, just dispatch to real creator
76 /**********************************************************
77 */
78
Tatu Salorantae4f23bb2011-12-23 00:31:35 -080079 /**
80 * Factory method that constructs and returns an empty {@link ArrayNode}
81 * Construction is done using registered {@link JsonNodeFactory}.
82 */
Tatu Saloranta29552ca2013-05-17 20:36:07 -070083 @Override
Tatu Salorantae4f23bb2011-12-23 00:31:35 -080084 public final ArrayNode arrayNode() { return _nodeFactory.arrayNode(); }
85
86 /**
Matt Veitasf81a3f92016-04-04 07:45:49 -040087 * Factory method that constructs and returns an {@link ArrayNode} with an initial capacity
88 * Construction is done using registered {@link JsonNodeFactory}
89 * @param capacity the initial capacity of the ArrayNode
90 */
91 @Override
92 public final ArrayNode arrayNode(int capacity) { return _nodeFactory.arrayNode(capacity); }
93
94 /**
Tatu Salorantae4f23bb2011-12-23 00:31:35 -080095 * Factory method that constructs and returns an empty {@link ObjectNode}
96 * Construction is done using registered {@link JsonNodeFactory}.
97 */
Tatu Saloranta29552ca2013-05-17 20:36:07 -070098 @Override
Tatu Salorantae4f23bb2011-12-23 00:31:35 -080099 public final ObjectNode objectNode() { return _nodeFactory.objectNode(); }
100
Tatu Saloranta29552ca2013-05-17 20:36:07 -0700101 @Override
Tatu Salorantae4f23bb2011-12-23 00:31:35 -0800102 public final NumericNode numberNode(byte v) { return _nodeFactory.numberNode(v); }
Tatu Saloranta29552ca2013-05-17 20:36:07 -0700103 @Override
Tatu Salorantae4f23bb2011-12-23 00:31:35 -0800104 public final NumericNode numberNode(short v) { return _nodeFactory.numberNode(v); }
Tatu Saloranta29552ca2013-05-17 20:36:07 -0700105 @Override
Tatu Salorantae4f23bb2011-12-23 00:31:35 -0800106 public final NumericNode numberNode(int v) { return _nodeFactory.numberNode(v); }
Tatu Saloranta29552ca2013-05-17 20:36:07 -0700107 @Override
Tatu Saloranta2322ce62013-10-23 22:18:32 -0700108 public final NumericNode numberNode(long v) {
109 return _nodeFactory.numberNode(v);
110 }
Tatu Saloranta29552ca2013-05-17 20:36:07 -0700111
Tatu Saloranta29552ca2013-05-17 20:36:07 -0700112 @Override
Tatu Salorantae4f23bb2011-12-23 00:31:35 -0800113 public final NumericNode numberNode(float v) { return _nodeFactory.numberNode(v); }
Tatu Saloranta29552ca2013-05-17 20:36:07 -0700114 @Override
Tatu Salorantae4f23bb2011-12-23 00:31:35 -0800115 public final NumericNode numberNode(double v) { return _nodeFactory.numberNode(v); }
Tatu Salorantae4f23bb2011-12-23 00:31:35 -0800116
Tatu Saloranta7eaf5992016-10-27 22:52:48 -0700117 @Override
118 public final ValueNode numberNode(BigInteger v) { return _nodeFactory.numberNode(v); }
119 @Override
120 public final ValueNode numberNode(BigDecimal v) { return (_nodeFactory.numberNode(v)); }
121
Tatu Saloranta29552ca2013-05-17 20:36:07 -0700122 @Override
123 public final ValueNode numberNode(Byte v) { return _nodeFactory.numberNode(v); }
124 @Override
125 public final ValueNode numberNode(Short v) { return _nodeFactory.numberNode(v); }
126 @Override
127 public final ValueNode numberNode(Integer v) { return _nodeFactory.numberNode(v); }
128 @Override
129 public final ValueNode numberNode(Long v) { return _nodeFactory.numberNode(v); }
130
131 @Override
132 public final ValueNode numberNode(Float v) { return _nodeFactory.numberNode(v); }
133 @Override
134 public final ValueNode numberNode(Double v) { return _nodeFactory.numberNode(v); }
Matt Veitasf81a3f92016-04-04 07:45:49 -0400135
Tatu Saloranta29552ca2013-05-17 20:36:07 -0700136 @Override
Tatu Salorantae4f23bb2011-12-23 00:31:35 -0800137 public final TextNode textNode(String text) { return _nodeFactory.textNode(text); }
138
Tatu Saloranta29552ca2013-05-17 20:36:07 -0700139 @Override
Tatu Salorantae4f23bb2011-12-23 00:31:35 -0800140 public final BinaryNode binaryNode(byte[] data) { return _nodeFactory.binaryNode(data); }
Tatu Saloranta29552ca2013-05-17 20:36:07 -0700141 @Override
Tatu Salorantae4f23bb2011-12-23 00:31:35 -0800142 public final BinaryNode binaryNode(byte[] data, int offset, int length) { return _nodeFactory.binaryNode(data, offset, length); }
143
Tatu Saloranta29552ca2013-05-17 20:36:07 -0700144 @Override
145 public final ValueNode pojoNode(Object pojo) { return _nodeFactory.pojoNode(pojo); }
Tatu Salorantae4f23bb2011-12-23 00:31:35 -0800146
Tatu Saloranta04c9cf52015-04-01 20:37:16 -0700147 @Override
148 public final ValueNode rawValueNode(RawValue value) { return _nodeFactory.rawValueNode(value); }
149
Tatu Salorantae4f23bb2011-12-23 00:31:35 -0800150 /*
151 /**********************************************************
152 /* Common mutators
153 /**********************************************************
154 */
155
156 /**
157 * Method for removing all children container has (if any)
158 *
159 * @return Container node itself (to allow method call chaining)
Tatu Salorantae4f23bb2011-12-23 00:31:35 -0800160 */
Tatub471a042012-01-27 11:51:39 -0800161 public abstract T removeAll();
Tatu Salorantae4f23bb2011-12-23 00:31:35 -0800162}