Skip to content

Commit

Permalink
Memoizing fieldName lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
monitorjbl committed Dec 4, 2016
1 parent cf16ea8 commit 2436f90
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
Expand Up @@ -514,12 +514,14 @@ private Annotation[] getAnnotations(Field field) {
}

private String getFieldName(Field field) {
JsonProperty jsonProperty = field.getAnnotation(JsonProperty.class);
if(jsonProperty != null && jsonProperty.value().length() > 0) {
return jsonProperty.value();
} else {
return field.getName();
}
return memoizer.fieldName(field, () -> {
JsonProperty jsonProperty = field.getAnnotation(JsonProperty.class);
if(jsonProperty != null && jsonProperty.value().length() > 0) {
return jsonProperty.value();
} else {
return field.getName();
}
});
}

@SuppressWarnings("unchecked")
Expand Down
7 changes: 6 additions & 1 deletion json-view/src/main/java/com/monitorjbl/json/Memoizer.java
Expand Up @@ -7,6 +7,7 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;

import static com.monitorjbl.json.Memoizer.FunctionCache.FIELD_NAME;
import static com.monitorjbl.json.Memoizer.FunctionCache.IGNORE_ANNOTATIONS;
import static com.monitorjbl.json.Memoizer.FunctionCache.MATCHES;

Expand All @@ -30,6 +31,10 @@ public <T> T matches(Set<String> values, String pattern, boolean matchPrefix, Su
return (T) fitToMaxSize(MATCHES).computeIfAbsent(new TriArg(values, pattern, matchPrefix), (k) -> compute.get());
}

public <T> T fieldName(Field f, Supplier<T> compute) {
return (T) fitToMaxSize(FIELD_NAME).computeIfAbsent(new MonoArg(f), (k) -> compute.get());
}

private Map<Arg, Object> fitToMaxSize(FunctionCache key) {
Map<Arg, Object> map = cache.get(key);
if(map.size() > maxCacheSize) {
Expand All @@ -39,7 +44,7 @@ private Map<Arg, Object> fitToMaxSize(FunctionCache key) {
}

enum FunctionCache {
IGNORE_ANNOTATIONS, MATCHES
IGNORE_ANNOTATIONS, MATCHES, FIELD_NAME
}

private interface Arg {}
Expand Down

0 comments on commit 2436f90

Please sign in to comment.