@@ -332,6 +332,64 @@ public JsonPointer append(JsonPointer tail) {
332
332
return compile (currentJsonPointer + tail ._asString );
333
333
}
334
334
335
+ /**
336
+ * ATTENTION! {@link JsonPointer} is head centric, tail appending is much costlier than head appending.
337
+ * It is not recommended to overuse the method.
338
+ *
339
+ * Mutant factory method that will return
340
+ *<ul>
341
+ * <li>`this` instance if `property` is null or empty String, OR
342
+ * </li>
343
+ * <li>Newly constructed {@link JsonPointer} instance that starts with all segments
344
+ * of `this`, followed by new segment of 'property' name.
345
+ * </li>
346
+ *</ul>
347
+ *
348
+ * 'property' format is starting separator (optional, added automatically if not provided) and new segment name.
349
+ *
350
+ * @param property new segment property name
351
+ *
352
+ * @return Either `this` instance, or a newly created combination, as per description above.
353
+ */
354
+ public JsonPointer appendProperty (String property ) {
355
+ if (property == null || property .isEmpty ()) {
356
+ return this ;
357
+ }
358
+ if (property .charAt (0 ) != SEPARATOR ) {
359
+ property = SEPARATOR + property ;
360
+ }
361
+ String currentJsonPointer = _asString ;
362
+ if (currentJsonPointer .endsWith ("/" )) {
363
+ //removes final slash
364
+ currentJsonPointer = currentJsonPointer .substring (0 , currentJsonPointer .length ()-1 );
365
+ }
366
+ return compile (currentJsonPointer + property );
367
+ }
368
+
369
+ /**
370
+ * ATTENTION! {@link JsonPointer} is head centric, tail appending is much costlier than head appending.
371
+ * It is not recommended to overuse the method.
372
+ *
373
+ * Mutant factory method that will return newly constructed {@link JsonPointer} instance that starts with all
374
+ * segments of `this`, followed by new segment of element 'index'. Element 'index' should be non-negative.
375
+ *
376
+ * @param index new segment element index
377
+ *
378
+ * @return Newly created combination, as per description above.
379
+ * @throws IllegalArgumentException if element index is negative
380
+ */
381
+ public JsonPointer appendIndex (int index ) {
382
+ if (index < 0 ) {
383
+ throw new IllegalArgumentException ("Negative index cannot be appended" );
384
+ }
385
+ String currentJsonPointer = _asString ;
386
+ if (currentJsonPointer .endsWith ("/" )) {
387
+ //removes final slash
388
+ currentJsonPointer = currentJsonPointer .substring (0 , currentJsonPointer .length ()-1 );
389
+ }
390
+ return compile (currentJsonPointer + SEPARATOR + index );
391
+ }
392
+
335
393
/**
336
394
* Method that may be called to see if the pointer head (first segment)
337
395
* would match property (of a JSON Object) with given name.
0 commit comments