Skip to content

Commit

Permalink
Removed unused methods, no longer use thread local
Browse files Browse the repository at this point in the history
- Use default 'equals' implementation of record
- Store all thread related data within a central concurrent hash map in
  order to better track potential leaking data
  • Loading branch information
reinhapa committed Apr 30, 2024
1 parent c22bcdc commit f7a7e46
Showing 1 changed file with 29 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import java.util.Deque;
import java.util.Iterator;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
* Avoid infinite recursions in Triggers by preventing the same trigger
Expand All @@ -37,10 +39,10 @@
*/
public class TriggerStatePerThread {

private static final ThreadLocal<Deque<TriggerState>> THREAD_LOCAL_STATES = ThreadLocal.withInitial(ArrayDeque::new);
private static final ConcurrentMap<Thread, Deque<TriggerState>> THREAD_LOCAL_STATES = new ConcurrentHashMap<>();

public static void setAndTest(final Trigger trigger, final TriggerPhase triggerPhase, final TriggerEvent triggerEvent, final XmldbURI src, final @Nullable XmldbURI dst) throws CyclicTriggerException {
final Deque<TriggerState> states = THREAD_LOCAL_STATES.get();
final Deque<TriggerState> states = getStates();

if (states.isEmpty()) {
if (triggerPhase != TriggerPhase.BEFORE) {
Expand Down Expand Up @@ -117,7 +119,7 @@ public static void clearIfFinished(final TriggerPhase phase) {
if (phase == TriggerPhase.AFTER) {

int depth = 0;
final Deque<TriggerState> states = THREAD_LOCAL_STATES.get();
final Deque<TriggerState> states = getStates();
for (final Iterator<TriggerState> it = states.descendingIterator(); it.hasNext(); ) {
final TriggerState state = it.next();
switch (state.triggerPhase) {
Expand All @@ -139,11 +141,15 @@ public static void clearIfFinished(final TriggerPhase phase) {
}

public static void clear() {
THREAD_LOCAL_STATES.remove();
THREAD_LOCAL_STATES.remove(Thread.currentThread());
}

public static boolean isEmpty() {
return THREAD_LOCAL_STATES.get().isEmpty();
return getStates().isEmpty();
}

private static Deque<TriggerState> getStates() {
return THREAD_LOCAL_STATES.computeIfAbsent(Thread.currentThread(), thread -> new ArrayDeque<>());
}

private record TriggerState(Trigger trigger, TriggerPhase triggerPhase, TriggerEvent triggerEvent, XmldbURI src,
Expand Down Expand Up @@ -179,42 +185,6 @@ public String toString() {
return builder.toString();
}

@Override
public boolean equals(final Object o) {
return equals(o, false);
}

public boolean equalsIgnoringPhase(final Object o) {
return equals(o, true);
}

private boolean equals(final Object o, final boolean ignorePhase) {
if (o instanceof TriggerState that) {
if (possiblyCyclic != that.possiblyCyclic) {
return false;
}

if (!trigger.equals(that.trigger)) {
return false;
}

if (!ignorePhase &&
triggerPhase != that.triggerPhase) {
return false;
}

if (triggerEvent != that.triggerEvent) {
return false;
}

if (!src.equals(that.src)) {
return false;
}

return Objects.equals(dst, that.dst);
}
return false;
}

private boolean equalsIgnoringPhase(final Trigger otherTrigger, final TriggerEvent otherTriggerEvent, final XmldbURI otherSrc, @Nullable final XmldbURI otherDst) {
if (!trigger.equals(otherTrigger)) {
Expand All @@ -229,7 +199,7 @@ private boolean equalsIgnoringPhase(final Trigger otherTrigger, final TriggerEve
return false;
}

return dst != null ? dst.equals(otherDst) : otherDst == null;
return Objects.equals(dst, otherDst);
}

public boolean isCompletedBy(final Trigger otherTrigger, final TriggerPhase otherTriggerPhase, final TriggerEvent otherTriggerEvent, final XmldbURI otherSrc, @Nullable final XmldbURI otherDst) {
Expand All @@ -250,38 +220,31 @@ public boolean isCompletedBy(final Trigger otherTrigger, final TriggerPhase othe
return false;
}

return dst != null ? dst.equals(otherDst) : otherDst == null;
return Objects.equals(dst, otherDst);
}

public boolean completes(final Object o) {
if (this == o) {
return false;
}

if (o == null || getClass() != o.getClass()) {
return false;
}

final TriggerState that = (TriggerState) o;
if (o instanceof TriggerState that) {
if (this.triggerPhase != TriggerPhase.AFTER
|| that.triggerPhase != TriggerPhase.BEFORE) {
return false;
}

if (this.triggerPhase != TriggerPhase.AFTER
|| that.triggerPhase != TriggerPhase.BEFORE) {
return false;
}
if (!trigger.equals(that.trigger)) {
return false;
}

if (!trigger.equals(that.trigger)) {
return false;
}
if (triggerEvent != that.triggerEvent) {
return false;
}

if (triggerEvent != that.triggerEvent) {
return false;
}
if (!src.equals(that.src)) {
return false;
}

if (!src.equals(that.src)) {
return false;
return Objects.equals(dst, that.dst);
}

return dst != null ? dst.equals(that.dst) : that.dst == null;
return false;
}
}
}

0 comments on commit f7a7e46

Please sign in to comment.