1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.johnzon.core; |
20 | |
|
21 | |
import java.io.Serializable; |
22 | |
import java.math.BigDecimal; |
23 | |
import java.math.BigInteger; |
24 | |
import java.util.ArrayList; |
25 | |
import java.util.Collections; |
26 | |
import java.util.List; |
27 | |
|
28 | |
import javax.json.JsonArray; |
29 | |
import javax.json.JsonArrayBuilder; |
30 | |
import javax.json.JsonObjectBuilder; |
31 | |
import javax.json.JsonValue; |
32 | |
|
33 | 2295 | class JsonArrayBuilderImpl implements JsonArrayBuilder, Serializable { |
34 | |
private List<JsonValue> tmpList; |
35 | |
|
36 | |
@Override |
37 | |
public JsonArrayBuilder add(final JsonValue value) { |
38 | 4322 | addValue(value); |
39 | 4321 | return this; |
40 | |
} |
41 | |
|
42 | |
@Override |
43 | |
public JsonArrayBuilder add(final String value) { |
44 | 5 | addValue(new JsonStringImpl(value)); |
45 | 4 | return this; |
46 | |
} |
47 | |
|
48 | |
@Override |
49 | |
public JsonArrayBuilder add(final BigDecimal value) { |
50 | 2 | addValue(new JsonNumberImpl(value)); |
51 | 1 | return this; |
52 | |
} |
53 | |
|
54 | |
@Override |
55 | |
public JsonArrayBuilder add(final BigInteger value) { |
56 | 1 | addValue(new JsonNumberImpl(new BigDecimal(value))); |
57 | 0 | return this; |
58 | |
} |
59 | |
|
60 | |
@Override |
61 | |
public JsonArrayBuilder add(final int value) { |
62 | 12 | addValue(new JsonLongImpl(value)); |
63 | 12 | return this; |
64 | |
} |
65 | |
|
66 | |
@Override |
67 | |
public JsonArrayBuilder add(final long value) { |
68 | 0 | addValue(new JsonLongImpl(value)); |
69 | 0 | return this; |
70 | |
} |
71 | |
|
72 | |
@Override |
73 | |
public JsonArrayBuilder add(final double value) { |
74 | 6 | addValue(new JsonDoubleImpl(value)); |
75 | 3 | return this; |
76 | |
} |
77 | |
|
78 | |
@Override |
79 | |
public JsonArrayBuilder add(final boolean value) { |
80 | 1014 | addValue(value ? JsonValue.TRUE : JsonValue.FALSE); |
81 | 1014 | return this; |
82 | |
} |
83 | |
|
84 | |
@Override |
85 | |
public JsonArrayBuilder addNull() { |
86 | 6 | addValue(JsonValue.NULL); |
87 | 6 | return this; |
88 | |
} |
89 | |
|
90 | |
@Override |
91 | |
public JsonArrayBuilder add(final JsonObjectBuilder builder) { |
92 | 3450 | addValue(builder.build()); |
93 | 3449 | return this; |
94 | |
} |
95 | |
|
96 | |
@Override |
97 | |
public JsonArrayBuilder add(final JsonArrayBuilder builder) { |
98 | 5 | addValue(builder.build()); |
99 | 4 | return this; |
100 | |
} |
101 | |
|
102 | |
private void addValue(JsonValue value){ |
103 | 8815 | if (value == null) { |
104 | 1 | throw npe(); |
105 | |
} |
106 | |
|
107 | 8814 | if(tmpList==null){ |
108 | 2275 | tmpList=new ArrayList<JsonValue>(); |
109 | |
} |
110 | |
|
111 | 8814 | tmpList.add(value); |
112 | 8814 | } |
113 | |
|
114 | |
@Override |
115 | |
public JsonArray build() { |
116 | |
|
117 | 2271 | if(tmpList == null) { |
118 | 5 | return new JsonArrayImpl(Collections.EMPTY_LIST); |
119 | |
} else { |
120 | 2266 | List<JsonValue> dump = (Collections.unmodifiableList(tmpList)); |
121 | 2266 | tmpList=null; |
122 | 2266 | return new JsonArrayImpl(dump); |
123 | |
} |
124 | |
|
125 | |
} |
126 | |
|
127 | |
private static NullPointerException npe() { |
128 | 1 | throw new NullPointerException("value/builder must not be null"); |
129 | |
} |
130 | |
} |