Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 37 additions & 4 deletions vertx-core/src/main/java/io/vertx/core/json/JsonArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -449,14 +449,47 @@ public JsonArray set(int pos, Object value) {
}

/**
* Does the JSON array contain the specified value? This method will scan the entire array until it finds a value
* or reaches the end.
* Returns {@code true} if this JSON Array contains the specified value.
* More formally, returns {@code true} if and only if this JSON array contains
* at least one entry {@code entry} such that {@code Objects.equals(value, entry)}.
*
* @param value the value
* @param value the value whose presence in this JSON array is to be tested
* @return true if it contains the value, false if not
*/
public boolean contains(Object value) {
return list.contains(value);
return indexOf(value) >= 0;
}

/**
* Returns the index of the last occurrence of the specified value
* in this JSON array, or -1 if this JSON array does not contain the value.
* More formally, returns the highest index {@code i} such that
* {@code Objects.equals(value, get(i))},
* or -1 if there is no such index.
*
* @param value the value whose index in this JSON array is to be returned
* @return the index of the value in the array, or -1 if the value is not in the array
*/
public int indexOf(Object value) {
// in case of JsonObject/JsonArray, the list might still contain an unwrapped Map/List, we need to check for both
if (value instanceof JsonObject) {
return indexOfFirst(value, ((JsonObject) value).getMap());
} else if (value instanceof JsonArray) {
return indexOfFirst(value, ((JsonArray) value).getList());
} else {
return list.indexOf(value);
}
}

private int indexOfFirst(Object value, Object value2) {
for (int i = 0; i < list.size(); i++) {
Object entry = list.get(i);
if (value.equals(entry) || value2.equals(entry)) {
return i;
}
}

return -1;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,8 @@ public void testContains() {
JsonArray arr = new JsonArray();
jsonArray.add(obj);
jsonArray.add(arr);
jsonArray.add(Map.of("foo", "bar"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually I don't think this should pass

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead rewrite it as jsonArray.getList().add(...)

jsonArray.add(List.of("baz"));
assertFalse(jsonArray.contains("eek"));
assertFalse(jsonArray.contains(false));
assertFalse(jsonArray.contains(321));
Expand All @@ -668,6 +670,8 @@ public void testContains() {
assertTrue(jsonArray.contains(123));
assertTrue(jsonArray.contains(obj));
assertTrue(jsonArray.contains(arr));
assertTrue(jsonArray.contains(new JsonObject().put("foo", "bar")));
assertTrue(jsonArray.contains(new JsonArray().add("baz")));
}

@Test
Expand Down