This commit is contained in:
Redkale
2016-12-15 09:54:23 +08:00
parent 54956e47d2
commit 47f723e63b
6 changed files with 196 additions and 59 deletions

View File

@@ -30,8 +30,13 @@ public final class ArrayDecoder<T> implements Decodeable<Reader, T[]> {
protected final Decodeable<Reader, T> decoder; protected final Decodeable<Reader, T> decoder;
private boolean inited = false;
private final Object lock = new Object();
public ArrayDecoder(final ConvertFactory factory, final Type type) { public ArrayDecoder(final ConvertFactory factory, final Type type) {
this.type = type; this.type = type;
try {
if (type instanceof GenericArrayType) { if (type instanceof GenericArrayType) {
Type t = ((GenericArrayType) type).getGenericComponentType(); Type t = ((GenericArrayType) type).getGenericComponentType();
this.componentType = t instanceof TypeVariable ? Object.class : t; this.componentType = t instanceof TypeVariable ? Object.class : t;
@@ -47,12 +52,29 @@ public final class ArrayDecoder<T> implements Decodeable<Reader, T[]> {
} }
factory.register(type, this); factory.register(type, this);
this.decoder = factory.loadDecoder(this.componentType); this.decoder = factory.loadDecoder(this.componentType);
} finally {
inited = true;
synchronized (lock) {
lock.notifyAll();
}
}
} }
@Override @Override
public T[] convertFrom(Reader in) { public T[] convertFrom(Reader in) {
final int len = in.readArrayB(); final int len = in.readArrayB();
if (len == Reader.SIGN_NULL) return null; if (len == Reader.SIGN_NULL) return null;
if (this.decoder == null) {
if (!this.inited) {
synchronized (lock) {
try {
lock.wait();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
final Decodeable<Reader, T> localdecoder = this.decoder; final Decodeable<Reader, T> localdecoder = this.decoder;
final List<T> result = new ArrayList(); final List<T> result = new ArrayList();
if (len == Reader.SIGN_NOLENGTH) { if (len == Reader.SIGN_NOLENGTH) {

View File

@@ -29,8 +29,13 @@ public final class ArrayEncoder<T> implements Encodeable<Writer, T[]> {
private final Encodeable<Writer, Object> encoder; private final Encodeable<Writer, Object> encoder;
private boolean inited = false;
private final Object lock = new Object();
public ArrayEncoder(final ConvertFactory factory, final Type type) { public ArrayEncoder(final ConvertFactory factory, final Type type) {
this.type = type; this.type = type;
try {
if (type instanceof GenericArrayType) { if (type instanceof GenericArrayType) {
Type t = ((GenericArrayType) type).getGenericComponentType(); Type t = ((GenericArrayType) type).getGenericComponentType();
this.componentType = t instanceof TypeVariable ? Object.class : t; this.componentType = t instanceof TypeVariable ? Object.class : t;
@@ -42,6 +47,12 @@ public final class ArrayEncoder<T> implements Encodeable<Writer, T[]> {
factory.register(type, this); factory.register(type, this);
this.encoder = factory.loadEncoder(this.componentType); this.encoder = factory.loadEncoder(this.componentType);
this.anyEncoder = factory.getAnyEncoder(); this.anyEncoder = factory.getAnyEncoder();
} finally {
inited = true;
synchronized (lock) {
lock.notifyAll();
}
}
} }
@Override @Override
@@ -55,6 +66,17 @@ public final class ArrayEncoder<T> implements Encodeable<Writer, T[]> {
out.writeArrayE(); out.writeArrayE();
return; return;
} }
if (this.encoder == null) {
if (!this.inited) {
synchronized (lock) {
try {
lock.wait();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
out.writeArrayB(value.length); out.writeArrayB(value.length);
final Type comp = this.componentType; final Type comp = this.componentType;
boolean first = true; boolean first = true;

View File

@@ -32,8 +32,13 @@ public final class CollectionDecoder<T> implements Decodeable<Reader, Collection
protected final Decodeable<Reader, T> decoder; protected final Decodeable<Reader, T> decoder;
private boolean inited = false;
private final Object lock = new Object();
public CollectionDecoder(final ConvertFactory factory, final Type type) { public CollectionDecoder(final ConvertFactory factory, final Type type) {
this.type = type; this.type = type;
try {
if (type instanceof ParameterizedType) { if (type instanceof ParameterizedType) {
final ParameterizedType pt = (ParameterizedType) type; final ParameterizedType pt = (ParameterizedType) type;
this.componentType = pt.getActualTypeArguments()[0]; this.componentType = pt.getActualTypeArguments()[0];
@@ -43,12 +48,29 @@ public final class CollectionDecoder<T> implements Decodeable<Reader, Collection
} else { } else {
throw new ConvertException("collectiondecoder not support the type (" + type + ")"); throw new ConvertException("collectiondecoder not support the type (" + type + ")");
} }
} finally {
inited = true;
synchronized (lock) {
lock.notifyAll();
}
}
} }
@Override @Override
public Collection<T> convertFrom(Reader in) { public Collection<T> convertFrom(Reader in) {
final int len = in.readArrayB(); final int len = in.readArrayB();
if (len == Reader.SIGN_NULL) return null; if (len == Reader.SIGN_NULL) return null;
if (this.decoder == null) {
if (!this.inited) {
synchronized (lock) {
try {
lock.wait();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
final Decodeable<Reader, T> localdecoder = this.decoder; final Decodeable<Reader, T> localdecoder = this.decoder;
final Collection<T> result = this.creator.create(); final Collection<T> result = this.creator.create();
if (len == Reader.SIGN_NOLENGTH) { if (len == Reader.SIGN_NOLENGTH) {

View File

@@ -13,7 +13,9 @@ import java.util.Collection;
* 集合大小不能超过 32767。 在BSON中集合大小设定的是short对于大于32767长度的集合传输会影响性能所以没有采用int存储。 * 集合大小不能超过 32767。 在BSON中集合大小设定的是short对于大于32767长度的集合传输会影响性能所以没有采用int存储。
* 支持一定程度的泛型。 * 支持一定程度的泛型。
* *
* <p> 详情见: https://redkale.org * <p>
* 详情见: https://redkale.org
*
* @author zhangjx * @author zhangjx
* @param <T> 序列化的集合元素类型 * @param <T> 序列化的集合元素类型
*/ */
@@ -24,8 +26,13 @@ public final class CollectionEncoder<T> implements Encodeable<Writer, Collection
private final Encodeable<Writer, Object> encoder; private final Encodeable<Writer, Object> encoder;
private boolean inited = false;
private final Object lock = new Object();
public CollectionEncoder(final ConvertFactory factory, final Type type) { public CollectionEncoder(final ConvertFactory factory, final Type type) {
this.type = type; this.type = type;
try {
if (type instanceof ParameterizedType) { if (type instanceof ParameterizedType) {
Type t = ((ParameterizedType) type).getActualTypeArguments()[0]; Type t = ((ParameterizedType) type).getActualTypeArguments()[0];
if (t instanceof TypeVariable) { if (t instanceof TypeVariable) {
@@ -36,6 +43,12 @@ public final class CollectionEncoder<T> implements Encodeable<Writer, Collection
} else { } else {
this.encoder = factory.getAnyEncoder(); this.encoder = factory.getAnyEncoder();
} }
} finally {
inited = true;
synchronized (lock) {
lock.notifyAll();
}
}
} }
@Override @Override
@@ -49,6 +62,17 @@ public final class CollectionEncoder<T> implements Encodeable<Writer, Collection
out.writeArrayE(); out.writeArrayE();
return; return;
} }
if (this.encoder == null) {
if (!this.inited) {
synchronized (lock) {
try {
lock.wait();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
out.writeArrayB(value.size()); out.writeArrayB(value.size());
boolean first = true; boolean first = true;
for (Object v : value) { for (Object v : value) {

View File

@@ -12,7 +12,9 @@ import java.util.Map;
/** /**
* *
* <p> 详情见: https://redkale.org * <p>
* 详情见: https://redkale.org
*
* @author zhangjx * @author zhangjx
* @param <K> Map key的数据类型 * @param <K> Map key的数据类型
* @param <V> Map value的数据类型 * @param <V> Map value的数据类型
@@ -32,8 +34,13 @@ public final class MapDecoder<K, V> implements Decodeable<Reader, Map<K, V>> {
protected final Decodeable<Reader, V> valueDecoder; protected final Decodeable<Reader, V> valueDecoder;
private boolean inited = false;
private final Object lock = new Object();
public MapDecoder(final ConvertFactory factory, final Type type) { public MapDecoder(final ConvertFactory factory, final Type type) {
this.type = type; this.type = type;
try {
if (type instanceof ParameterizedType) { if (type instanceof ParameterizedType) {
final ParameterizedType pt = (ParameterizedType) type; final ParameterizedType pt = (ParameterizedType) type;
this.keyType = pt.getActualTypeArguments()[0]; this.keyType = pt.getActualTypeArguments()[0];
@@ -45,10 +52,27 @@ public final class MapDecoder<K, V> implements Decodeable<Reader, Map<K, V>> {
} else { } else {
throw new ConvertException("mapdecoder not support the type (" + type + ")"); throw new ConvertException("mapdecoder not support the type (" + type + ")");
} }
} finally {
inited = true;
synchronized (lock) {
lock.notifyAll();
}
}
} }
@Override @Override
public Map<K, V> convertFrom(Reader in) { public Map<K, V> convertFrom(Reader in) {
if (this.keyDecoder == null || this.valueDecoder == null) {
if (!this.inited) {
synchronized (lock) {
try {
lock.wait();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
final int len = in.readMapB(); final int len = in.readMapB();
if (len == Reader.SIGN_NULL) return null; if (len == Reader.SIGN_NULL) return null;
final Map<K, V> result = this.creator.create(); final Map<K, V> result = this.creator.create();

View File

@@ -27,8 +27,13 @@ public final class MapEncoder<K, V> implements Encodeable<Writer, Map<K, V>> {
private final Encodeable<Writer, V> valencoder; private final Encodeable<Writer, V> valencoder;
private boolean inited = false;
private final Object lock = new Object();
public MapEncoder(final ConvertFactory factory, final Type type) { public MapEncoder(final ConvertFactory factory, final Type type) {
this.type = type; this.type = type;
try {
if (type instanceof ParameterizedType) { if (type instanceof ParameterizedType) {
final Type[] pt = ((ParameterizedType) type).getActualTypeArguments(); final Type[] pt = ((ParameterizedType) type).getActualTypeArguments();
this.keyencoder = factory.loadEncoder(pt[0]); this.keyencoder = factory.loadEncoder(pt[0]);
@@ -37,6 +42,12 @@ public final class MapEncoder<K, V> implements Encodeable<Writer, Map<K, V>> {
this.keyencoder = factory.getAnyEncoder(); this.keyencoder = factory.getAnyEncoder();
this.valencoder = factory.getAnyEncoder(); this.valencoder = factory.getAnyEncoder();
} }
} finally {
inited = true;
synchronized (lock) {
lock.notifyAll();
}
}
} }
@Override @Override
@@ -46,6 +57,18 @@ public final class MapEncoder<K, V> implements Encodeable<Writer, Map<K, V>> {
out.writeNull(); out.writeNull();
return; return;
} }
if (this.keyencoder == null || this.valencoder == null) {
if (!this.inited) {
synchronized (lock) {
try {
lock.wait();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
out.writeMapB(values.size()); out.writeMapB(values.size());
boolean first = true; boolean first = true;
for (Map.Entry<K, V> en : values.entrySet()) { for (Map.Entry<K, V> en : values.entrySet()) {