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,29 +30,51 @@ 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;
if (type instanceof GenericArrayType) { try {
Type t = ((GenericArrayType) type).getGenericComponentType(); if (type instanceof GenericArrayType) {
this.componentType = t instanceof TypeVariable ? Object.class : t; Type t = ((GenericArrayType) type).getGenericComponentType();
} else if ((type instanceof Class) && ((Class) type).isArray()) { this.componentType = t instanceof TypeVariable ? Object.class : t;
this.componentType = ((Class) type).getComponentType(); } else if ((type instanceof Class) && ((Class) type).isArray()) {
} else { this.componentType = ((Class) type).getComponentType();
throw new ConvertException("(" + type + ") is not a array type"); } else {
throw new ConvertException("(" + type + ") is not a array type");
}
if (this.componentType instanceof ParameterizedType) {
this.componentClass = (Class) ((ParameterizedType) this.componentType).getRawType();
} else {
this.componentClass = (Class) this.componentType;
}
factory.register(type, this);
this.decoder = factory.loadDecoder(this.componentType);
} finally {
inited = true;
synchronized (lock) {
lock.notifyAll();
}
} }
if (this.componentType instanceof ParameterizedType) {
this.componentClass = (Class) ((ParameterizedType) this.componentType).getRawType();
} else {
this.componentClass = (Class) this.componentType;
}
factory.register(type, this);
this.decoder = factory.loadDecoder(this.componentType);
} }
@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,19 +29,30 @@ 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;
if (type instanceof GenericArrayType) { try {
Type t = ((GenericArrayType) type).getGenericComponentType(); if (type instanceof GenericArrayType) {
this.componentType = t instanceof TypeVariable ? Object.class : t; Type t = ((GenericArrayType) type).getGenericComponentType();
} else if ((type instanceof Class) && ((Class) type).isArray()) { this.componentType = t instanceof TypeVariable ? Object.class : t;
this.componentType = ((Class) type).getComponentType(); } else if ((type instanceof Class) && ((Class) type).isArray()) {
} else { this.componentType = ((Class) type).getComponentType();
throw new ConvertException("(" + type + ") is not a array type"); } else {
throw new ConvertException("(" + type + ") is not a array type");
}
factory.register(type, this);
this.encoder = factory.loadEncoder(this.componentType);
this.anyEncoder = factory.getAnyEncoder();
} finally {
inited = true;
synchronized (lock) {
lock.notifyAll();
}
} }
factory.register(type, this);
this.encoder = factory.loadEncoder(this.componentType);
this.anyEncoder = factory.getAnyEncoder();
} }
@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,16 +32,27 @@ 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;
if (type instanceof ParameterizedType) { try {
final ParameterizedType pt = (ParameterizedType) type; if (type instanceof ParameterizedType) {
this.componentType = pt.getActualTypeArguments()[0]; final ParameterizedType pt = (ParameterizedType) type;
this.creator = factory.loadCreator((Class) pt.getRawType()); this.componentType = pt.getActualTypeArguments()[0];
factory.register(type, this); this.creator = factory.loadCreator((Class) pt.getRawType());
this.decoder = factory.loadDecoder(this.componentType); factory.register(type, this);
} else { this.decoder = factory.loadDecoder(this.componentType);
throw new ConvertException("collectiondecoder not support the type (" + type + ")"); } else {
throw new ConvertException("collectiondecoder not support the type (" + type + ")");
}
} finally {
inited = true;
synchronized (lock) {
lock.notifyAll();
}
} }
} }
@@ -49,6 +60,17 @@ public final class CollectionDecoder<T> implements Decodeable<Reader, Collection
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

@@ -10,10 +10,12 @@ 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,17 +26,28 @@ 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;
if (type instanceof ParameterizedType) { try {
Type t = ((ParameterizedType) type).getActualTypeArguments()[0]; if (type instanceof ParameterizedType) {
if (t instanceof TypeVariable) { Type t = ((ParameterizedType) type).getActualTypeArguments()[0];
this.encoder = factory.getAnyEncoder(); if (t instanceof TypeVariable) {
this.encoder = factory.getAnyEncoder();
} else {
this.encoder = factory.loadEncoder(t);
}
} else { } else {
this.encoder = factory.loadEncoder(t); this.encoder = factory.getAnyEncoder();
}
} finally {
inited = true;
synchronized (lock) {
lock.notifyAll();
} }
} else {
this.encoder = factory.getAnyEncoder();
} }
} }
@@ -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,23 +34,45 @@ 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;
if (type instanceof ParameterizedType) { try {
final ParameterizedType pt = (ParameterizedType) type; if (type instanceof ParameterizedType) {
this.keyType = pt.getActualTypeArguments()[0]; final ParameterizedType pt = (ParameterizedType) type;
this.valueType = pt.getActualTypeArguments()[1]; this.keyType = pt.getActualTypeArguments()[0];
this.creator = factory.loadCreator((Class) pt.getRawType()); this.valueType = pt.getActualTypeArguments()[1];
factory.register(type, this); this.creator = factory.loadCreator((Class) pt.getRawType());
this.keyDecoder = factory.loadDecoder(this.keyType); factory.register(type, this);
this.valueDecoder = factory.loadDecoder(this.valueType); this.keyDecoder = factory.loadDecoder(this.keyType);
} else { this.valueDecoder = factory.loadDecoder(this.valueType);
throw new ConvertException("mapdecoder not support the type (" + type + ")"); } else {
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,15 +27,26 @@ 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;
if (type instanceof ParameterizedType) { try {
final Type[] pt = ((ParameterizedType) type).getActualTypeArguments(); if (type instanceof ParameterizedType) {
this.keyencoder = factory.loadEncoder(pt[0]); final Type[] pt = ((ParameterizedType) type).getActualTypeArguments();
this.valencoder = factory.loadEncoder(pt[1]); this.keyencoder = factory.loadEncoder(pt[0]);
} else { this.valencoder = factory.loadEncoder(pt[1]);
this.keyencoder = factory.getAnyEncoder(); } else {
this.valencoder = factory.getAnyEncoder(); this.keyencoder = factory.getAnyEncoder();
this.valencoder = factory.getAnyEncoder();
}
} finally {
inited = true;
synchronized (lock) {
lock.notifyAll();
}
} }
} }
@@ -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()) {