修复单例模式双检查错误
parent
52fef50772
commit
514edd4796
|
@ -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<T> implements Schema<List<T>> {
|
||||
|
||||
private static volatile Map<Schema, CollectionSchema> cache = new HashMap<>();
|
||||
private static volatile Map<Object, CollectionSchema> cache = new HashMap<>();
|
||||
|
||||
public static Schema<List> 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);
|
||||
|
|
|
@ -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<T> implements Schema<T> {
|
||||
|
||||
private static volatile Map<String, ConvertSchema> cache = new HashMap<>();
|
||||
private static volatile Map<Object, ConvertSchema> cache = new HashMap<>();
|
||||
|
||||
public static Schema getInstance(Class<? extends Converter> 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);
|
||||
|
|
|
@ -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<T> implements Schema<T> {
|
||||
|
||||
private static volatile Map<Schema, ObjectSchema> cache = new HashMap<>();
|
||||
private static volatile Map<Object, ObjectSchema> 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);
|
||||
|
|
|
@ -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<String> {
|
||||
private static volatile Map<String, Chars> cache = new HashMap<>();
|
||||
private static volatile Map<Object, Chars> cache = new HashMap<>();
|
||||
|
||||
public static Schema<String> 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);
|
||||
|
|
Loading…
Reference in New Issue