diff --git a/src/main/java/io/github/yezhihao/protostar/schema/CollectionSchema.java b/src/main/java/io/github/yezhihao/protostar/schema/CollectionSchema.java index 748ba74..e20fc67 100644 --- a/src/main/java/io/github/yezhihao/protostar/schema/CollectionSchema.java +++ b/src/main/java/io/github/yezhihao/protostar/schema/CollectionSchema.java @@ -1,7 +1,7 @@ package io.github.yezhihao.protostar.schema; -import io.netty.buffer.ByteBuf; import io.github.yezhihao.protostar.Schema; +import io.netty.buffer.ByteBuf; import java.util.ArrayList; import java.util.HashMap; @@ -10,13 +10,14 @@ import java.util.Map; public class CollectionSchema implements Schema> { - private static volatile Map cache = new HashMap<>(); + private static volatile Map cache = new HashMap<>(); public static Schema getInstance(Schema schema) { - CollectionSchema instance = cache.get(schema); - if (instance == null) { + Object key = schema; + CollectionSchema instance; + if ((instance = cache.get(key)) == null) { synchronized (cache) { - if (instance == null) { + if ((instance = cache.get(key)) == null) { instance = new CollectionSchema(schema); cache.put(schema, instance); log.debug("new CollectionSchema({})", schema); diff --git a/src/main/java/io/github/yezhihao/protostar/schema/ConvertSchema.java b/src/main/java/io/github/yezhihao/protostar/schema/ConvertSchema.java index 24dccfb..aeb4b12 100644 --- a/src/main/java/io/github/yezhihao/protostar/schema/ConvertSchema.java +++ b/src/main/java/io/github/yezhihao/protostar/schema/ConvertSchema.java @@ -1,8 +1,8 @@ package io.github.yezhihao.protostar.schema; -import io.netty.buffer.ByteBuf; -import io.github.yezhihao.protostar.converter.Converter; import io.github.yezhihao.protostar.Schema; +import io.github.yezhihao.protostar.converter.Converter; +import io.netty.buffer.ByteBuf; import java.util.HashMap; import java.util.Map; @@ -12,18 +12,18 @@ import java.util.Map; */ public class ConvertSchema implements Schema { - private static volatile Map cache = new HashMap<>(); + private static volatile Map cache = new HashMap<>(); public static Schema getInstance(Class clazz) { - String name = clazz.getName(); - ConvertSchema instance = cache.get(name); - if (instance == null) { + String key = clazz.getName(); + ConvertSchema instance; + if ((instance = cache.get(key)) == null) { synchronized (cache) { - if (instance == null) { + if ((instance = cache.get(key)) == null) { try { Converter converter = clazz.newInstance(); instance = new ConvertSchema(converter); - cache.put(name, instance); + cache.put(key, instance); log.debug("new ConvertSchema({})", clazz); } catch (Exception e) { throw new RuntimeException(e); diff --git a/src/main/java/io/github/yezhihao/protostar/schema/ObjectSchema.java b/src/main/java/io/github/yezhihao/protostar/schema/ObjectSchema.java index 914b5ea..599469d 100644 --- a/src/main/java/io/github/yezhihao/protostar/schema/ObjectSchema.java +++ b/src/main/java/io/github/yezhihao/protostar/schema/ObjectSchema.java @@ -1,20 +1,21 @@ package io.github.yezhihao.protostar.schema; -import io.netty.buffer.ByteBuf; import io.github.yezhihao.protostar.Schema; +import io.netty.buffer.ByteBuf; import java.util.HashMap; import java.util.Map; public class ObjectSchema implements Schema { - private static volatile Map cache = new HashMap<>(); + private static volatile Map cache = new HashMap<>(); public static Schema getInstance(Schema schema) { - ObjectSchema instance = cache.get(schema); - if (instance == null) { + Object key = schema; + ObjectSchema instance; + if ((instance = cache.get(key)) == null) { synchronized (cache) { - if (instance == null) { + if ((instance = cache.get(key)) == null) { instance = new ObjectSchema(schema); cache.put(schema, instance); log.debug("new ObjectSchema({})", schema); diff --git a/src/main/java/io/github/yezhihao/protostar/schema/StringSchema.java b/src/main/java/io/github/yezhihao/protostar/schema/StringSchema.java index 09f1013..6e4a0d8 100644 --- a/src/main/java/io/github/yezhihao/protostar/schema/StringSchema.java +++ b/src/main/java/io/github/yezhihao/protostar/schema/StringSchema.java @@ -1,8 +1,8 @@ package io.github.yezhihao.protostar.schema; -import io.netty.buffer.ByteBuf; import io.github.yezhihao.protostar.Schema; import io.github.yezhihao.protostar.util.Bcd; +import io.netty.buffer.ByteBuf; import java.nio.charset.Charset; import java.util.Arrays; @@ -12,15 +12,15 @@ import java.util.Map; public class StringSchema { public static class Chars implements Schema { - private static volatile Map cache = new HashMap<>(); + private static volatile Map cache = new HashMap<>(); public static Schema getInstance(byte pad, String charset) { charset = charset.toLowerCase(); String key = new StringBuilder(10).append((char) pad).append('/').append(charset).toString(); - Chars instance = cache.get(key); - if (instance == null) { + Chars instance; + if ((instance = cache.get(key)) == null) { synchronized (cache) { - if (instance == null) { + if ((instance = cache.get(key)) == null) { instance = new Chars(pad, charset); cache.put(key, instance); log.debug("new StringSchema({},{})", pad, charset);