Debian Codegraph @

src/org/eclipse/parsson/JsonPatchBuilderImpl.java

raw symbols SCIP
1 /*
2 * Copyright (c) 2015, 2023 Oracle and/or its affiliates. All rights reserved.
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v. 2.0, which is available at
6 * http://www.eclipse.org/legal/epl-2.0.
7 *
8 * This Source Code may also be made available under the following Secondary
9 * Licenses when the conditions for such availability set forth in the
10 * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11 * version 2 with the GNU Classpath Exception, which is available at
12 * https://www.gnu.org/software/classpath/license.html.
13 *
14 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15 */
16
17 package org.eclipse.parsson;
18
19 import jakarta.json.JsonArray;
20 import jakarta.json.JsonArrayBuilder;
21 import jakarta.json.JsonException;
22 import jakarta.json.JsonPatch;
23 import jakarta.json.JsonPatch.Operation;
24 import jakarta.json.JsonPatchBuilder;
25 import jakarta.json.JsonStructure;
26 import jakarta.json.JsonValue;
27
28 /**
29 * A builder for constructing a JSON Patch by adding
30 * JSON Patch operations incrementally.
31 * <p>
32 * The following illustrates the approach.
33 * <pre>
34 * JsonPatchBuilder builder = Json.createPatchBuilder();
35 * JsonPatch patch = builder.add("/John/phones/office", "1234-567")
36 * .remove("/Amy/age")
37 * .build();
38 * </pre>
39 * The result is equivalent to the following JSON Patch.
40 * <pre>
41 * [
42 * {"op" = "add", "path" = "/John/phones/office", "value" = "1234-567"},
43 * {"op" = "remove", "path" = "/Amy/age"}
44 * ] </pre>
45 *
46 * @since 1.1
47 */
48 public final class JsonPatchBuilderImpl implements JsonPatchBuilder {
49
50 private final JsonContext jsonContext;
51 private final JsonArrayBuilder builder;
52
53 /**
54 * Creates a JsonPatchBuilderImpl, starting with the specified
55 * JSON Patch
56 * @param patch the JSON Patch
57 */
58 public JsonPatchBuilderImpl(JsonArray patch, JsonContext jsonContext) {
59 this.jsonContext = jsonContext;
60 builder = new JsonArrayBuilderImpl(patch, jsonContext);
61 }
62
63 /**
64 * Creates JsonPatchBuilderImpl with empty JSON Patch
65 */
66 public JsonPatchBuilderImpl(JsonContext jsonContext) {
67 this.jsonContext = jsonContext;
68 builder = new JsonArrayBuilderImpl(jsonContext);
69 }
70
71 /**
72 * A convenience method for {@code new JsonPatchImpl(build()).apply(target)}.
73 * The target is not modified by the patch.
74 *
75 * @param <T> the target type, must be a subtype of {@link JsonStructure}
76 * @param target the target to apply the patch operations
77 * @return the transformed target after the patch
78 * @throws JsonException if the supplied JSON Patch is malformed or if
79 * it contains references to non-existing members
80 */
81 public <T extends JsonStructure> T apply(T target) {
82 return build().apply(target);
83 }
84
85 /**
86 * Adds an "add" JSON Patch operation.
87 * @param path the "path" member of the operation
88 * @param value the "value" member of the operation
89 * @return this JsonPatchBuilder
90 */
91 @Override
92 public JsonPatchBuilder add(String path, JsonValue value) {
93 builder.add(new JsonObjectBuilderImpl(jsonContext)
94 .add("op", Operation.ADD.operationName())
95 .add("path", path)
96 .add("value", value)
97 );
98 return this;
99 }
100
101 /**
102 * Adds an "add" JSON Patch operation
103 * @param path the "path" member of the operation
104 * @param value the "value" member of the operation
105 * @return this JsonPatchBuilder
106 */
107 @Override
108 public JsonPatchBuilder add(String path, String value) {
109 builder.add(new JsonObjectBuilderImpl(jsonContext)
110 .add("op", Operation.ADD.operationName())
111 .add("path", path)
112 .add("value", value)
113 );
114 return this;
115 }
116
117 /**
118 * Adds an "add" JSON Patch operation
119 * @param path the "path" member of the operation
120 * @param value the "value" member of the operation
121 * @return this JsonPatchBuilder
122 */
123 @Override
124 public JsonPatchBuilder add(String path, int value) {
125 builder.add(new JsonObjectBuilderImpl(jsonContext)
126 .add("op", Operation.ADD.operationName())
127 .add("path", path)
128 .add("value", value)
129 );
130 return this;
131 }
132
133 /**
134 * Adds an "add" JSON Patch operation
135 * @param path the "path" member of the operation
136 * @param value the "value" member of the operation
137 * @return this JsonPatchBuilder
138 */
139 @Override
140 public JsonPatchBuilder add(String path, boolean value) {
141 builder.add(new JsonObjectBuilderImpl(jsonContext)
142 .add("op", Operation.ADD.operationName())
143 .add("path", path)
144 .add("value", value)
145 );
146 return this;
147 }
148
149 /**
150 * Adds a "remove" JSON Patch operation.
151 * @param path the "path" member of the operation
152 * @return this JsonPatchBuilder
153 */
154 @Override
155 public JsonPatchBuilder remove(String path) {
156 builder.add(new JsonObjectBuilderImpl(jsonContext)
157 .add("op", Operation.REMOVE.operationName())
158 .add("path", path)
159 );
160 return this;
161 }
162
163 /**
164 * Adds a "replace" JSON Patch operation.
165 * @param path the "path" member of the operation
166 * @param value the "value" member of the operation
167 * @return this JsonPatchBuilder
168 */
169 @Override
170 public JsonPatchBuilder replace(String path, JsonValue value) {
171 builder.add(new JsonObjectBuilderImpl(jsonContext)
172 .add("op", Operation.REPLACE.operationName())
173 .add("path", path)
174 .add("value", value)
175 );
176 return this;
177 }
178
179 /**
180 * Adds a "replace" JSON Patch operation.
181 * @param path the "path" member of the operation
182 * @param value the "value" member of the operation
183 * @return this JsonPatchBuilder
184 */
185 @Override
186 public JsonPatchBuilder replace(String path, String value) {
187 builder.add(new JsonObjectBuilderImpl(jsonContext)
188 .add("op", Operation.REPLACE.operationName())
189 .add("path", path)
190 .add("value", value)
191 );
192 return this;
193 }
194
195 /**
196 * Adds a "replace" JSON Patch operation.
197 * @param path the "path" member of the operation
198 * @param value the "value" member of the operation
199 * @return this JsonPatchBuilder
200 */
201 @Override
202 public JsonPatchBuilder replace(String path, int value) {
203 builder.add(new JsonObjectBuilderImpl(jsonContext)
204 .add("op", Operation.REPLACE.operationName())
205 .add("path", path)
206 .add("value", value)
207 );
208 return this;
209 }
210
211 /**
212 * Adds a "replace" JSON Patch operation.
213 * @param path the "path" member of the operation
214 * @param value the "value" member of the operation
215 * @return this JsonPatchBuilder
216 */
217 @Override
218 public JsonPatchBuilder replace(String path, boolean value) {
219 builder.add(new JsonObjectBuilderImpl(jsonContext)
220 .add("op", Operation.REPLACE.operationName())
221 .add("path", path)
222 .add("value", value)
223 );
224 return this;
225 }
226
227 /**
228 * Adds a "move" JSON Patch operation.
229 * @param path the "path" member of the operation
230 * @param from the "from" member of the operation
231 * @return this JsonPatchBuilder
232 */
233 @Override
234 public JsonPatchBuilder move(String path, String from) {
235 builder.add(new JsonObjectBuilderImpl(jsonContext)
236 .add("op", Operation.MOVE.operationName())
237 .add("path", path)
238 .add("from", from)
239 );
240 return this;
241 }
242
243 /**
244 * Adds a "copy" JSON Patch operation.
245 * @param path the "path" member of the operation
246 * @param from the "from" member of the operation
247 * @return this JsonPatchBuilder
248 */
249 @Override
250 public JsonPatchBuilder copy(String path, String from) {
251 builder.add(new JsonObjectBuilderImpl(jsonContext)
252 .add("op", Operation.COPY.operationName())
253 .add("path", path)
254 .add("from", from)
255 );
256 return this;
257 }
258
259 /**
260 * Adds a "test" JSON Patch operation.
261 * @param path the "path" member of the operation
262 * @param value the "value" member of the operation
263 * @return this JsonPatchBuilder
264 */
265 @Override
266 public JsonPatchBuilder test(String path, JsonValue value) {
267 builder.add(new JsonObjectBuilderImpl(jsonContext)
268 .add("op", Operation.TEST.operationName())
269 .add("path", path)
270 .add("value", value)
271 );
272 return this;
273 }
274
275 /**
276 * Adds a "test" JSON Patch operation.
277 * @param path the "path" member of the operation
278 * @param value the "value" member of the operation
279 * @return this JsonPatchBuilder
280 */
281 @Override
282 public JsonPatchBuilder test(String path, String value) {
283 builder.add(new JsonObjectBuilderImpl(jsonContext)
284 .add("op", Operation.TEST.operationName())
285 .add("path", path)
286 .add("value", value)
287 );
288 return this;
289 }
290
291 /**
292 * Adds a "test" JSON Patch operation.
293 * @param path the "path" member of the operation
294 * @param value the "value" member of the operation
295 * @return this JsonPatchBuilder
296 */
297 @Override
298 public JsonPatchBuilder test(String path, int value) {
299 builder.add(new JsonObjectBuilderImpl(jsonContext)
300 .add("op", Operation.TEST.operationName())
301 .add("path", path)
302 .add("value", value)
303 );
304 return this;
305 }
306
307 /**
308 * Adds a "test" JSON Patch operation.
309 * @param path the "path" member of the operation
310 * @param value the "value" member of the operation
311 * @return this JsonPatchBuilder
312 */
313 @Override
314 public JsonPatchBuilder test(String path, boolean value) {
315 builder.add(new JsonObjectBuilderImpl(jsonContext)
316 .add("op", Operation.TEST.operationName())
317 .add("path", path)
318 .add("value", value)
319 );
320 return this;
321 }
322
323 /**
324 * Returns the patch operations in a JsonArray
325 * @return the patch operations in a JsonArray
326 */
327 public JsonArray buildAsJsonArray() {
328 return builder.build();
329 }
330
331 /**
332 * Returns the patch operation in a JsonPatch
333 * @return the patch operation in a JsonPatch
334 */
335 @Override
336 public JsonPatch build() {
337 return new JsonPatchImpl(buildAsJsonArray(), jsonContext);
338 }
339
340 }
341