-
Notifications
You must be signed in to change notification settings - Fork 280
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #302: DRAFT: support ReferenceSchema#equals with cyclic schemas
- Loading branch information
Tobias Marstaller
committed
Jul 24, 2020
1 parent
fe1a51c
commit 13438c9
Showing
4 changed files
with
78 additions
and
16 deletions.
There are no files selected for viewing
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
57 changes: 57 additions & 0 deletions
57
core/src/main/java/org/everit/json/schema/internal/EqualsCycleBreaker.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,57 @@ | ||
package org.everit.json.schema.internal; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.WeakHashMap; | ||
import java.util.function.BiFunction; | ||
|
||
public final class EqualsCycleBreaker | ||
{ | ||
private EqualsCycleBreaker() | ||
{ | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
/** | ||
* For each invocation of <code>a.equals(b)</code> that uses this class, | ||
*/ | ||
private static final ThreadLocal<WeakHashMap<Identity, Set>> ongoingEqualityChecks = ThreadLocal.withInitial(WeakHashMap::new); | ||
|
||
public static <T> boolean equalsWithoutCycle(T self, T other, boolean equalsOnCycle, BiFunction<T, T, Boolean> equalityFunction) { | ||
Set<T> localOngoingEqualityChecks = ongoingEqualityChecks.get() | ||
.computeIfAbsent(new Identity<>(self), (_k) -> new HashSet<>()); | ||
if (localOngoingEqualityChecks.add(other)) { | ||
try { | ||
return equalityFunction.apply(self, other); | ||
} | ||
finally { | ||
localOngoingEqualityChecks.remove(other); | ||
if (localOngoingEqualityChecks.isEmpty()) { | ||
ongoingEqualityChecks.remove(); | ||
} | ||
} | ||
} else { | ||
return equalsOnCycle; | ||
} | ||
} | ||
|
||
private static class Identity<E> { | ||
private final E e; | ||
|
||
public Identity(E e) | ||
{ | ||
this.e = e; | ||
} | ||
|
||
public int hashCode() { | ||
return System.identityHashCode(e); | ||
} | ||
|
||
public boolean equals(Object o) { | ||
if (!(o instanceof Identity)) { | ||
return false; | ||
} | ||
return ((Identity<?>) o).e == e; | ||
} | ||
} | ||
} |
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
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