修复单例模式双检查错误
parent
52fef50772
commit
514edd4796
|
@ -1,7 +1,7 @@
|
||||||
package io.github.yezhihao.protostar.schema;
|
package io.github.yezhihao.protostar.schema;
|
||||||
|
|
||||||
import io.netty.buffer.ByteBuf;
|
|
||||||
import io.github.yezhihao.protostar.Schema;
|
import io.github.yezhihao.protostar.Schema;
|
||||||
|
import io.netty.buffer.ByteBuf;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
@ -10,13 +10,14 @@ import java.util.Map;
|
||||||
|
|
||||||
public class CollectionSchema<T> implements Schema<List<T>> {
|
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) {
|
public static Schema<List> getInstance(Schema schema) {
|
||||||
CollectionSchema instance = cache.get(schema);
|
Object key = schema;
|
||||||
if (instance == null) {
|
CollectionSchema instance;
|
||||||
|
if ((instance = cache.get(key)) == null) {
|
||||||
synchronized (cache) {
|
synchronized (cache) {
|
||||||
if (instance == null) {
|
if ((instance = cache.get(key)) == null) {
|
||||||
instance = new CollectionSchema(schema);
|
instance = new CollectionSchema(schema);
|
||||||
cache.put(schema, instance);
|
cache.put(schema, instance);
|
||||||
log.debug("new CollectionSchema({})", schema);
|
log.debug("new CollectionSchema({})", schema);
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
package io.github.yezhihao.protostar.schema;
|
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.Schema;
|
||||||
|
import io.github.yezhihao.protostar.converter.Converter;
|
||||||
|
import io.netty.buffer.ByteBuf;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
@ -12,18 +12,18 @@ import java.util.Map;
|
||||||
*/
|
*/
|
||||||
public class ConvertSchema<T> implements Schema<T> {
|
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) {
|
public static Schema getInstance(Class<? extends Converter> clazz) {
|
||||||
String name = clazz.getName();
|
String key = clazz.getName();
|
||||||
ConvertSchema instance = cache.get(name);
|
ConvertSchema instance;
|
||||||
if (instance == null) {
|
if ((instance = cache.get(key)) == null) {
|
||||||
synchronized (cache) {
|
synchronized (cache) {
|
||||||
if (instance == null) {
|
if ((instance = cache.get(key)) == null) {
|
||||||
try {
|
try {
|
||||||
Converter converter = clazz.newInstance();
|
Converter converter = clazz.newInstance();
|
||||||
instance = new ConvertSchema(converter);
|
instance = new ConvertSchema(converter);
|
||||||
cache.put(name, instance);
|
cache.put(key, instance);
|
||||||
log.debug("new ConvertSchema({})", clazz);
|
log.debug("new ConvertSchema({})", clazz);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
|
|
|
@ -1,20 +1,21 @@
|
||||||
package io.github.yezhihao.protostar.schema;
|
package io.github.yezhihao.protostar.schema;
|
||||||
|
|
||||||
import io.netty.buffer.ByteBuf;
|
|
||||||
import io.github.yezhihao.protostar.Schema;
|
import io.github.yezhihao.protostar.Schema;
|
||||||
|
import io.netty.buffer.ByteBuf;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class ObjectSchema<T> implements Schema<T> {
|
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) {
|
public static Schema getInstance(Schema schema) {
|
||||||
ObjectSchema instance = cache.get(schema);
|
Object key = schema;
|
||||||
if (instance == null) {
|
ObjectSchema instance;
|
||||||
|
if ((instance = cache.get(key)) == null) {
|
||||||
synchronized (cache) {
|
synchronized (cache) {
|
||||||
if (instance == null) {
|
if ((instance = cache.get(key)) == null) {
|
||||||
instance = new ObjectSchema(schema);
|
instance = new ObjectSchema(schema);
|
||||||
cache.put(schema, instance);
|
cache.put(schema, instance);
|
||||||
log.debug("new ObjectSchema({})", schema);
|
log.debug("new ObjectSchema({})", schema);
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
package io.github.yezhihao.protostar.schema;
|
package io.github.yezhihao.protostar.schema;
|
||||||
|
|
||||||
import io.netty.buffer.ByteBuf;
|
|
||||||
import io.github.yezhihao.protostar.Schema;
|
import io.github.yezhihao.protostar.Schema;
|
||||||
import io.github.yezhihao.protostar.util.Bcd;
|
import io.github.yezhihao.protostar.util.Bcd;
|
||||||
|
import io.netty.buffer.ByteBuf;
|
||||||
|
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
@ -12,15 +12,15 @@ import java.util.Map;
|
||||||
public class StringSchema {
|
public class StringSchema {
|
||||||
|
|
||||||
public static class Chars implements Schema<String> {
|
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) {
|
public static Schema<String> getInstance(byte pad, String charset) {
|
||||||
charset = charset.toLowerCase();
|
charset = charset.toLowerCase();
|
||||||
String key = new StringBuilder(10).append((char) pad).append('/').append(charset).toString();
|
String key = new StringBuilder(10).append((char) pad).append('/').append(charset).toString();
|
||||||
Chars instance = cache.get(key);
|
Chars instance;
|
||||||
if (instance == null) {
|
if ((instance = cache.get(key)) == null) {
|
||||||
synchronized (cache) {
|
synchronized (cache) {
|
||||||
if (instance == null) {
|
if ((instance = cache.get(key)) == null) {
|
||||||
instance = new Chars(pad, charset);
|
instance = new Chars(pad, charset);
|
||||||
cache.put(key, instance);
|
cache.put(key, instance);
|
||||||
log.debug("new StringSchema({},{})", pad, charset);
|
log.debug("new StringSchema({},{})", pad, charset);
|
||||||
|
|
Loading…
Reference in New Issue