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.Collections; |
25 | |
import java.util.LinkedHashMap; |
26 | |
import java.util.Map; |
27 | |
|
28 | |
import javax.json.JsonArrayBuilder; |
29 | |
import javax.json.JsonObject; |
30 | |
import javax.json.JsonObjectBuilder; |
31 | |
import javax.json.JsonValue; |
32 | |
|
33 | 4580 | class JsonObjectBuilderImpl implements JsonObjectBuilder, Serializable { |
34 | |
private Map<String, JsonValue> tmpMap; |
35 | |
|
36 | |
@Override |
37 | |
public JsonObjectBuilder add(final String name, final JsonValue value) { |
38 | 34745 | putValue(name, value); |
39 | 34745 | return this; |
40 | |
} |
41 | |
|
42 | |
@Override |
43 | |
public JsonObjectBuilder add(final String name, final String value) { |
44 | 2 | putValue(name, new JsonStringImpl(value)); |
45 | 2 | return this; |
46 | |
} |
47 | |
|
48 | |
@Override |
49 | |
public JsonObjectBuilder add(final String name, final BigInteger value) { |
50 | 0 | putValue(name, new JsonNumberImpl(new BigDecimal(value))); |
51 | 0 | return this; |
52 | |
} |
53 | |
|
54 | |
@Override |
55 | |
public JsonObjectBuilder add(final String name, final BigDecimal value) { |
56 | 0 | putValue(name, new JsonNumberImpl(value)); |
57 | 0 | return this; |
58 | |
} |
59 | |
|
60 | |
@Override |
61 | |
public JsonObjectBuilder add(final String name, final int value) { |
62 | 0 | putValue(name, new JsonLongImpl(value)); |
63 | 0 | return this; |
64 | |
} |
65 | |
|
66 | |
@Override |
67 | |
public JsonObjectBuilder add(final String name, final long value) { |
68 | 0 | putValue(name, new JsonLongImpl(value)); |
69 | 0 | return this; |
70 | |
} |
71 | |
|
72 | |
@Override |
73 | |
public JsonObjectBuilder add(final String name, final double value) { |
74 | 0 | putValue(name, new JsonDoubleImpl(value)); |
75 | 0 | return this; |
76 | |
} |
77 | |
|
78 | |
@Override |
79 | |
public JsonObjectBuilder add(final String name, final boolean value) { |
80 | 6671 | putValue(name, value ? JsonValue.TRUE : JsonValue.FALSE); |
81 | 6671 | return this; |
82 | |
} |
83 | |
|
84 | |
@Override |
85 | |
public JsonObjectBuilder addNull(final String name) { |
86 | 3035 | putValue(name, JsonValue.NULL); |
87 | 3035 | return this; |
88 | |
} |
89 | |
|
90 | |
@Override |
91 | |
public JsonObjectBuilder add(final String name, final JsonObjectBuilder builder) { |
92 | 1011 | putValue(name, builder.build()); |
93 | 1011 | return this; |
94 | |
} |
95 | |
|
96 | |
@Override |
97 | |
public JsonObjectBuilder add(final String name, final JsonArrayBuilder builder) { |
98 | 2240 | putValue(name, builder.build()); |
99 | 2240 | return this; |
100 | |
} |
101 | |
|
102 | |
private void putValue(String name, JsonValue value){ |
103 | 47704 | if(name == null || value == null) { |
104 | 0 | throw npe(); |
105 | |
} |
106 | |
|
107 | 47704 | if(tmpMap==null){ |
108 | 4546 | tmpMap=new LinkedHashMap<String, JsonValue>(); |
109 | |
} |
110 | |
|
111 | 47704 | tmpMap.put(name, value); |
112 | 47704 | } |
113 | |
|
114 | |
private static NullPointerException npe() { |
115 | 0 | return new NullPointerException("name or value/builder must not be null"); |
116 | |
} |
117 | |
|
118 | |
@Override |
119 | |
public JsonObject build() { |
120 | |
|
121 | 4517 | if(tmpMap==null) { |
122 | 2 | return new JsonObjectImpl(Collections.EMPTY_MAP); |
123 | |
} else { |
124 | 4515 | Map<String, JsonValue> dump = (Collections.unmodifiableMap(tmpMap)); |
125 | 4515 | tmpMap=null; |
126 | 4515 | return new JsonObjectImpl(dump); |
127 | |
} |
128 | |
|
129 | |
|
130 | |
} |
131 | |
} |