@@ -449,14 +449,47 @@ public JsonArray set(int pos, Object value) {
449
449
}
450
450
451
451
/**
452
- * Does the JSON array contain the specified value? This method will scan the entire array until it finds a value
453
- * or reaches the end.
452
+ * Returns {@code true} if this JSON Array contains the specified value.
453
+ * More formally, returns {@code true} if and only if this JSON array contains
454
+ * at least one entry {@code entry} such that {@code Objects.equals(value, entry)}.
454
455
*
455
- * @param value the value
456
+ * @param value the value whose presence in this JSON array is to be tested
456
457
* @return true if it contains the value, false if not
457
458
*/
458
459
public boolean contains (Object value ) {
459
- return list .contains (value );
460
+ return indexOf (value ) >= 0 ;
461
+ }
462
+
463
+ /**
464
+ * Returns the index of the last occurrence of the specified value
465
+ * in this JSON array, or -1 if this JSON array does not contain the value.
466
+ * More formally, returns the highest index {@code i} such that
467
+ * {@code Objects.equals(value, get(i))},
468
+ * or -1 if there is no such index.
469
+ *
470
+ * @param value the value whose index in this JSON array is to be returned
471
+ * @return the index of the value in the array, or -1 if the value is not in the array
472
+ */
473
+ public int indexOf (Object value ) {
474
+ // in case of JsonObject/JsonArray, the list might still contain an unwrapped Map/List, we need to check for both
475
+ if (value instanceof JsonObject ) {
476
+ return indexOfFirst (value , ((JsonObject ) value ).getMap ());
477
+ } else if (value instanceof JsonArray ) {
478
+ return indexOfFirst (value , ((JsonArray ) value ).getList ());
479
+ } else {
480
+ return list .indexOf (value );
481
+ }
482
+ }
483
+
484
+ private int indexOfFirst (Object value , Object value2 ) {
485
+ for (int i = 0 ; i < list .size (); i ++) {
486
+ Object entry = list .get (i );
487
+ if (value .equals (entry ) || value2 .equals (entry )) {
488
+ return i ;
489
+ }
490
+ }
491
+
492
+ return -1 ;
460
493
}
461
494
462
495
/**
0 commit comments