diff --git a/impl/src/main/java/org/eclipse/parsson/JsonMergePatchImpl.java b/impl/src/main/java/org/eclipse/parsson/JsonMergePatchImpl.java index 5a9904da..21e67f9d 100644 --- a/impl/src/main/java/org/eclipse/parsson/JsonMergePatchImpl.java +++ b/impl/src/main/java/org/eclipse/parsson/JsonMergePatchImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, 2021 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2015, 2022 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0, which is available at @@ -113,5 +113,38 @@ static JsonValue diff(JsonValue source, JsonValue target) { return builder.build(); } + /** + * Compares this {@code JsonMergePatchImpl} with another object. + * @param obj the object to compare this {@code JsonMergePatchImpl} against + * @return true if the given object is a {@code JsonMergePatchImpl} with the same + * reference tokens as this one, false otherwise. + */ + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null || obj.getClass() != JsonMergePatchImpl.class) + return false; + return patch.equals(((JsonMergePatchImpl)obj).patch); + } + + /** + * Returns the hash code value for this {@code JsonMergePatchImpl}. + * + * @return the hash code value for this {@code JsonMergePatchImpl} object + */ + @Override + public int hashCode() { + return patch.hashCode(); + } + + /** + * Returns the JSON Patch text + * @return the JSON Patch text + */ + @Override + public String toString() { + return patch.toString(); + } } diff --git a/impl/src/test/java/org/eclipse/parsson/tests/JsonMergePatch2Test.java b/impl/src/test/java/org/eclipse/parsson/tests/JsonMergePatch2Test.java new file mode 100644 index 00000000..2e4b9576 --- /dev/null +++ b/impl/src/test/java/org/eclipse/parsson/tests/JsonMergePatch2Test.java @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2022 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package org.eclipse.parsson.tests; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +import jakarta.json.Json; +import jakarta.json.JsonArray; +import jakarta.json.JsonMergePatch; +import jakarta.json.JsonPatch; + +public class JsonMergePatch2Test { + + @Test + public void testToString() { + JsonArray jsonArray = Json.createArrayBuilder().add(Json.createValue(1)).build(); + JsonPatch jsonPatch = Json.createPatchBuilder(jsonArray).build(); + assertEquals("[1]", jsonPatch.toString()); + JsonMergePatch jsonMergePatch = Json.createMergePatch(jsonArray); + assertEquals("[1]", jsonMergePatch.toString()); + } +}