Skip to content

Commit

Permalink
AVRO-2347: Use Java8 Map API for SpecificData
Browse files Browse the repository at this point in the history
  • Loading branch information
belugabehr authored and dkulp committed Mar 11, 2019
1 parent 8c3c590 commit 6d06987
Showing 1 changed file with 5 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.HashSet;
import java.util.Map;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.WeakHashMap;
Expand Down Expand Up @@ -306,22 +307,17 @@ protected Schema computeValue(Class<?> type) {
}
};
// for non-class objects, use a WeakHashMap, but this needs a sync block around it
private final Map<java.lang.reflect.Type, Schema> schemaTypeCache = new WeakHashMap<>();
private final Map<java.lang.reflect.Type, Schema> schemaTypeCache =
Collections.synchronizedMap(new WeakHashMap<>());

/** Find the schema for a Java type. */
public Schema getSchema(java.lang.reflect.Type type) {
try {
if (type instanceof Class) {
return schemaClassCache.get((Class<?>)type);
}
synchronized (schemaTypeCache) {
Schema s = schemaTypeCache.get(type);
if (s == null) {
s = createSchema(type, new LinkedHashMap<>());
schemaTypeCache.put(type, s);
}
return s;
}
return schemaTypeCache.computeIfAbsent(type,
t -> createSchema(t, new LinkedHashMap<>()));
} catch (Exception e) {
throw (e instanceof AvroRuntimeException) ?
(AvroRuntimeException)e : new AvroRuntimeException(e);
Expand Down

0 comments on commit 6d06987

Please sign in to comment.