avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dk...@apache.org
Subject [avro] branch master updated: AVRO-2347: Use Java8 Map API for SpecificData
Date Mon, 11 Mar 2019 19:16:04 GMT
This is an automated email from the ASF dual-hosted git repository.

dkulp pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/avro.git


The following commit(s) were added to refs/heads/master by this push:
     new 6d06987  AVRO-2347: Use Java8 Map API for SpecificData
6d06987 is described below

commit 6d0698705299e67e2f463216b95bf962ab6f11a2
Author: Beluga Behr <dam6923@gmail.com>
AuthorDate: Mon Mar 11 10:34:33 2019 -0400

    AVRO-2347: Use Java8 Map API for SpecificData
---
 .../main/java/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 7f53f97..ff01177 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.Arrays;
 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 @@ public class SpecificData extends GenericData {
     }
   };
   // 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) {
@@ -314,14 +316,8 @@ public class SpecificData extends GenericData {
       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);


Mime
View raw message