From 6d0698705299e67e2f463216b95bf962ab6f11a2 Mon Sep 17 00:00:00 2001 From: Beluga Behr Date: Mon, 11 Mar 2019 10:34:33 -0400 Subject: [PATCH] AVRO-2347: Use Java8 Map API for SpecificData --- .../org/apache/avro/specific/SpecificData.java | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificData.java b/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificData.java index 7f53f9716dc..ff01177831f 100644 --- a/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificData.java +++ b/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificData.java @@ -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; @@ -306,7 +307,8 @@ protected Schema computeValue(Class type) { } }; // for non-class objects, use a WeakHashMap, but this needs a sync block around it - private final Map schemaTypeCache = new WeakHashMap<>(); + private final Map schemaTypeCache = + Collections.synchronizedMap(new WeakHashMap<>()); /** Find the schema for a Java type. */ public Schema getSchema(java.lang.reflect.Type type) { @@ -314,14 +316,8 @@ public Schema getSchema(java.lang.reflect.Type type) { 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);