forked from eclipse-ee4j/yasson
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Anton Pinsky <[email protected]>
- Loading branch information
1 parent
ac9da78
commit ef0ccdd
Showing
1 changed file
with
44 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
src/test/java/org/eclipse/yasson/serializers/model/TwoObjectsComparer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright (c) 2023 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, | ||
* or the Eclipse Distribution License v. 1.0 which is available at | ||
* http://www.eclipse.org/org/documents/edl-v10.php. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause | ||
*/ | ||
|
||
package org.eclipse.yasson.serializers.model; | ||
|
||
import java.util.Arrays; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
public class TwoObjectsComparer { | ||
public final String propertyName; | ||
public final Object firstObjectValue; | ||
public final Object secondObjectValue; | ||
|
||
public static <T> Optional<TwoObjectsComparer> getDifferentFieldInTwoObjects(T thisObject, T otherObject) { | ||
return otherObject == null ? Optional.empty() : Arrays.stream(thisObject.getClass().getFields()).map(f -> { | ||
try { | ||
return new TwoObjectsComparer(f.getName(), f.get(thisObject), f.get(otherObject)); | ||
} catch (IllegalAccessException e) { | ||
throw new RuntimeException(e); | ||
} | ||
}).filter(o -> !Objects.equals(o.firstObjectValue, o.secondObjectValue)).findFirst(); | ||
} | ||
|
||
private TwoObjectsComparer(String name, Object firstObjectValue, Object secondObjectValue) { | ||
this.propertyName = name; | ||
this.firstObjectValue = firstObjectValue; | ||
this.secondObjectValue = secondObjectValue; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "propertyName: " + propertyName + " firstObjectValue: " + firstObjectValue + " secondObjectValue: " + secondObjectValue; | ||
} | ||
} |