Skip to content

Commit

Permalink
Add toString, equals, hashCode in JsonMergePatch
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Bescos Gascon <jorge.bescos.gascon@oracle.com>
  • Loading branch information
jbescos authored and lukasj committed May 25, 2022
1 parent 06af407 commit 88aea77
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
35 changes: 34 additions & 1 deletion impl/src/main/java/org/eclipse/parsson/JsonMergePatchImpl.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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();
}
}

Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit 88aea77

Please sign in to comment.