Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moving memoized code to separate class
- Loading branch information
1 parent
2dc5681
commit ace471b
Showing
10 changed files
with
227 additions
and
137 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
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
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,95 @@ | ||
package com.monitorjbl.json; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.EnumMap; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.function.Supplier; | ||
|
||
import static com.monitorjbl.json.Memoizer.FunctionCache.IGNORE_ANNOTATIONS; | ||
import static com.monitorjbl.json.Memoizer.FunctionCache.MATCHES; | ||
|
||
@SuppressWarnings("unchecked") | ||
class Memoizer { | ||
private final int maxCacheSize; | ||
private final Map<FunctionCache, Map<Arg, Object>> cache = new EnumMap<>(FunctionCache.class); | ||
|
||
public Memoizer(int maxCacheSize) { | ||
this.maxCacheSize = maxCacheSize; | ||
for(FunctionCache key : FunctionCache.class.getEnumConstants()) { | ||
cache.put(key, new ConcurrentHashMap<>()); | ||
} | ||
} | ||
|
||
public <T> T ignoreAnnotations(Field f, Supplier<T> compute) { | ||
return (T) fitToMaxSize(IGNORE_ANNOTATIONS).computeIfAbsent(new MonoArg(f), (k) -> compute.get()); | ||
} | ||
|
||
public <T> T matches(Set<String> values, String pattern, boolean matchPrefix, Supplier<T> compute) { | ||
return (T) fitToMaxSize(MATCHES).computeIfAbsent(new TriArg(values, pattern, matchPrefix), (k) -> compute.get()); | ||
} | ||
|
||
private Map<Arg, Object> fitToMaxSize(FunctionCache key) { | ||
Map<Arg, Object> map = cache.get(key); | ||
if(map.size() > maxCacheSize) { | ||
map.remove(map.keySet().iterator().next()); | ||
} | ||
return map; | ||
} | ||
|
||
enum FunctionCache { | ||
IGNORE_ANNOTATIONS, MATCHES | ||
} | ||
|
||
private interface Arg {} | ||
|
||
private class MonoArg implements Arg { | ||
private final Object arg1; | ||
|
||
public MonoArg(Object arg1) { | ||
this.arg1 = arg1; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
MonoArg monoArg = (MonoArg) o; | ||
|
||
return arg1 != null ? arg1.equals(monoArg.arg1) : monoArg.arg1 == null; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return arg1 != null ? arg1.hashCode() : 0; | ||
} | ||
} | ||
|
||
private class TriArg implements Arg { | ||
private final Object arg1; | ||
private final Object arg2; | ||
private final Object arg3; | ||
|
||
public TriArg(Object arg1, Object arg2, Object arg3) { | ||
this.arg1 = arg1; | ||
this.arg2 = arg2; | ||
this.arg3 = arg3; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
TriArg triArg = (TriArg) o; | ||
|
||
if(arg1 != null ? !arg1.equals(triArg.arg1) : triArg.arg1 != null) return false; | ||
if(arg2 != null ? !arg2.equals(triArg.arg2) : triArg.arg2 != null) return false; | ||
return arg3 != null ? arg3.equals(triArg.arg3) : triArg.arg3 == null; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int result = arg1 != null ? arg1.hashCode() : 0; | ||
result = 31 * result + (arg2 != null ? arg2.hashCode() : 0); | ||
result = 31 * result + (arg3 != null ? arg3.hashCode() : 0); | ||
return result; | ||
} | ||
} | ||
} |
Oops, something went wrong.