Skip to content

Commit

Permalink
change ItemFilterCache
Browse files Browse the repository at this point in the history
  • Loading branch information
slprime committed Dec 12, 2024
1 parent aebb989 commit 6f45558
Showing 1 changed file with 5 additions and 18 deletions.
23 changes: 5 additions & 18 deletions src/main/java/codechicken/nei/SearchTokenParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.locks.ReentrantLock;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -90,32 +92,17 @@ public boolean matches(ItemStack item) {

private static class ItemFilterCache implements ItemFilter {

private final Map<ItemStack, Boolean> states;
public ItemFilter filter;
private final ItemStackMap<Boolean> states = new ItemStackMap<>();
private final ReentrantLock lock = new ReentrantLock();

public ItemFilterCache(ItemFilter filter) {
this.states = Collections.synchronizedMap(new WeakHashMap<>());
this.filter = filter;
}

@Override
public boolean matches(ItemStack item) {
lock.lock();

try {
Boolean match = states.get(item);

if (match == null) {
states.put(item, match = this.filter.matches(item));
}

return match;
} catch (Throwable th) {
th.printStackTrace();
return false;
} finally {
lock.unlock();
}
return states.computeIfAbsent(item, stack -> this.filter.matches(stack));
}
}

Expand Down

0 comments on commit 6f45558

Please sign in to comment.