convert
This commit is contained in:
@@ -122,6 +122,7 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
||||
this.register(java.time.LocalTime.class, LocalTimeSimpledCoder.instance);
|
||||
this.register(java.time.LocalDateTime.class, LocalDateTimeSimpledCoder.instance);
|
||||
this.register(java.time.Duration.class, DurationSimpledCoder.instance);
|
||||
this.register(AtomicBoolean.class, AtomicBooleanSimpledCoder.instance);
|
||||
this.register(AtomicInteger.class, AtomicIntegerSimpledCoder.instance);
|
||||
this.register(AtomicLong.class, AtomicLongSimpledCoder.instance);
|
||||
this.register(BigInteger.class, BigIntegerSimpledCoder.instance);
|
||||
|
||||
@@ -21,8 +21,11 @@ import org.redkale.convert.*;
|
||||
*/
|
||||
public class BsonArrayDecoder<T> extends ArrayDecoder<BsonReader, T> {
|
||||
|
||||
public BsonArrayDecoder(final BsonFactory factory, final Type type) {
|
||||
private final boolean skip;
|
||||
|
||||
public BsonArrayDecoder(final BsonFactory factory, final Type type, boolean skip) {
|
||||
super(factory, type);
|
||||
this.skip = skip;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -32,7 +35,10 @@ public class BsonArrayDecoder<T> extends ArrayDecoder<BsonReader, T> {
|
||||
if (len == Reader.SIGN_NULL) {
|
||||
return null;
|
||||
}
|
||||
final Decodeable<BsonReader, T> itemDecoder = BsonFactory.typeEnum(in.readArrayItemTypeEnum());
|
||||
Decodeable<BsonReader, T> itemDecoder = this.componentDecoder;
|
||||
if (skip) {
|
||||
itemDecoder = BsonFactory.skipTypeEnum(in.readArrayItemTypeEnum());
|
||||
}
|
||||
final List<T> result = new ArrayList();
|
||||
// 固定长度
|
||||
for (int i = 0; i < len; i++) {
|
||||
|
||||
@@ -20,8 +20,11 @@ import org.redkale.convert.*;
|
||||
*/
|
||||
public class BsonCollectionDecoder<T> extends CollectionDecoder<BsonReader, T> {
|
||||
|
||||
public BsonCollectionDecoder(final ConvertFactory factory, final Type type) {
|
||||
private final boolean skip;
|
||||
|
||||
public BsonCollectionDecoder(final ConvertFactory factory, final Type type, boolean skip) {
|
||||
super(factory, type);
|
||||
this.skip = skip;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -31,7 +34,10 @@ public class BsonCollectionDecoder<T> extends CollectionDecoder<BsonReader, T> {
|
||||
if (len == Reader.SIGN_NULL) {
|
||||
return null;
|
||||
}
|
||||
final Decodeable<BsonReader, T> itemDecoder = BsonFactory.typeEnum(in.readArrayItemTypeEnum());
|
||||
Decodeable<BsonReader, T> itemDecoder = this.componentDecoder;
|
||||
if (skip) {
|
||||
itemDecoder = BsonFactory.skipTypeEnum(in.readArrayItemTypeEnum());
|
||||
}
|
||||
final Collection<T> result = this.creator.create();
|
||||
// 固定长度
|
||||
for (int i = 0; i < len; i++) {
|
||||
|
||||
@@ -8,6 +8,9 @@ package org.redkale.convert.bson;
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.stream.Stream;
|
||||
import org.redkale.convert.*;
|
||||
import org.redkale.convert.ext.*;
|
||||
@@ -37,16 +40,16 @@ public final class BsonFactory extends ConvertFactory<BsonReader, BsonWriter> {
|
||||
static final Encodeable objectEncoder = instance.loadEncoder(Object.class);
|
||||
|
||||
// only for BsonRead.skipValue
|
||||
static final Decodeable skipArrayDecoder = new BsonArrayDecoder(instance, Object[].class);
|
||||
static final Decodeable skipArrayDecoder = new BsonArrayDecoder(instance, Object[].class, true);
|
||||
|
||||
// only for BsonRead.skipValue
|
||||
static final Decodeable skipCollectionDecoder = new BsonCollectionDecoder(instance, Collection.class);
|
||||
static final Decodeable skipCollectionDecoder = new BsonCollectionDecoder(instance, Collection.class, true);
|
||||
|
||||
// only for BsonRead.skipValue
|
||||
static final Decodeable skipStreamDecoder = new BsonStreamDecoder(instance, Stream.class);
|
||||
static final Decodeable skipStreamDecoder = new BsonStreamDecoder(instance, Stream.class, true);
|
||||
|
||||
// only for BsonRead.skipValue
|
||||
static final Decodeable skipMapDecoder = new BsonMapDecoder(instance, Map.class);
|
||||
static final Decodeable skipMapDecoder = new BsonMapDecoder(instance, Map.class, true);
|
||||
|
||||
static {
|
||||
instance.register(Serializable.class, objectDecoder);
|
||||
@@ -124,22 +127,22 @@ public final class BsonFactory extends ConvertFactory<BsonReader, BsonWriter> {
|
||||
|
||||
@Override
|
||||
protected <E> Decodeable<BsonReader, E> createArrayDecoder(Type type) {
|
||||
return new BsonArrayDecoder(this, type);
|
||||
return new BsonArrayDecoder(this, type, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected <E> Decodeable<BsonReader, E> createCollectionDecoder(Type type) {
|
||||
return new BsonCollectionDecoder(this, type);
|
||||
return new BsonCollectionDecoder(this, type, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected <E> Decodeable<BsonReader, E> createStreamDecoder(Type type) {
|
||||
return new BsonStreamDecoder(this, type);
|
||||
return new BsonStreamDecoder(this, type, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected <E> Decodeable<BsonReader, E> createMapDecoder(Type type) {
|
||||
return new BsonMapDecoder(this, type);
|
||||
return new BsonMapDecoder(this, type, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -161,7 +164,7 @@ public final class BsonFactory extends ConvertFactory<BsonReader, BsonWriter> {
|
||||
Objects.requireNonNull(type);
|
||||
Class clazz = TypeToken.typeToClass(type);
|
||||
byte typeval = 127; // 字段的类型值
|
||||
if (clazz == boolean.class || clazz == Boolean.class) {
|
||||
if (clazz == boolean.class || clazz == Boolean.class || clazz == AtomicBoolean.class) {
|
||||
typeval = 11;
|
||||
} else if (clazz == byte.class || clazz == Byte.class) {
|
||||
typeval = 12;
|
||||
@@ -169,9 +172,9 @@ public final class BsonFactory extends ConvertFactory<BsonReader, BsonWriter> {
|
||||
typeval = 13;
|
||||
} else if (clazz == char.class || clazz == Character.class) {
|
||||
typeval = 14;
|
||||
} else if (clazz == int.class || clazz == Integer.class) {
|
||||
} else if (clazz == int.class || clazz == Integer.class || clazz == AtomicInteger.class) {
|
||||
typeval = 15;
|
||||
} else if (clazz == long.class || clazz == Long.class) {
|
||||
} else if (clazz == long.class || clazz == Long.class || clazz == AtomicLong.class) {
|
||||
typeval = 16;
|
||||
} else if (clazz == float.class || clazz == Float.class) {
|
||||
typeval = 17;
|
||||
@@ -179,7 +182,7 @@ public final class BsonFactory extends ConvertFactory<BsonReader, BsonWriter> {
|
||||
typeval = 18;
|
||||
} else if (clazz == String.class) {
|
||||
typeval = 19;
|
||||
} else if (clazz == boolean[].class || clazz == Boolean[].class) {
|
||||
} else if (clazz == boolean[].class || clazz == Boolean[].class || clazz == AtomicBoolean[].class) {
|
||||
typeval = 21;
|
||||
} else if (clazz == byte[].class || clazz == Byte[].class) {
|
||||
typeval = 22;
|
||||
@@ -187,9 +190,9 @@ public final class BsonFactory extends ConvertFactory<BsonReader, BsonWriter> {
|
||||
typeval = 23;
|
||||
} else if (clazz == char[].class || clazz == Character[].class) {
|
||||
typeval = 24;
|
||||
} else if (clazz == int[].class || clazz == Integer[].class) {
|
||||
} else if (clazz == int[].class || clazz == Integer[].class || clazz == AtomicInteger[].class) {
|
||||
typeval = 25;
|
||||
} else if (clazz == long[].class || clazz == Long[].class) {
|
||||
} else if (clazz == long[].class || clazz == Long[].class || clazz == AtomicLong[].class) {
|
||||
typeval = 26;
|
||||
} else if (clazz == float[].class || clazz == Float[].class) {
|
||||
typeval = 27;
|
||||
@@ -209,7 +212,7 @@ public final class BsonFactory extends ConvertFactory<BsonReader, BsonWriter> {
|
||||
return typeval;
|
||||
}
|
||||
|
||||
protected static Decodeable typeEnum(final byte typeval) {
|
||||
protected static Decodeable skipTypeEnum(final byte typeval) {
|
||||
switch (typeval) {
|
||||
case 11:
|
||||
return BoolSimpledCoder.instance;
|
||||
|
||||
@@ -20,8 +20,11 @@ import org.redkale.convert.*;
|
||||
*/
|
||||
public class BsonMapDecoder<K, V> extends MapDecoder<BsonReader, K, V> {
|
||||
|
||||
public BsonMapDecoder(final BsonFactory factory, final Type type) {
|
||||
private final boolean skip;
|
||||
|
||||
public BsonMapDecoder(final BsonFactory factory, final Type type, boolean skip) {
|
||||
super(factory, type);
|
||||
this.skip = skip;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -31,8 +34,12 @@ public class BsonMapDecoder<K, V> extends MapDecoder<BsonReader, K, V> {
|
||||
if (len == Reader.SIGN_NULL) {
|
||||
return null;
|
||||
}
|
||||
Decodeable<BsonReader, K> kdecoder = BsonFactory.typeEnum(in.readMapKeyTypeEnum());
|
||||
Decodeable<BsonReader, V> vdecoder = BsonFactory.typeEnum(in.readmapValueTypeEnum());
|
||||
Decodeable<BsonReader, K> kdecoder = this.keyDecoder;
|
||||
Decodeable<BsonReader, V> vdecoder = this.valueDecoder;
|
||||
if (skip) {
|
||||
kdecoder = BsonFactory.skipTypeEnum(in.readMapKeyTypeEnum());
|
||||
vdecoder = BsonFactory.skipTypeEnum(in.readmapValueTypeEnum());
|
||||
}
|
||||
final Map<K, V> result = this.creator.create();
|
||||
// 固定长度
|
||||
for (int i = 0; i < len; i++) {
|
||||
|
||||
@@ -104,7 +104,7 @@ public class BsonReader extends Reader {
|
||||
return;
|
||||
}
|
||||
this.fieldTypeEnum = 0;
|
||||
Decodeable decoder = BsonFactory.typeEnum(val);
|
||||
Decodeable decoder = BsonFactory.skipTypeEnum(val);
|
||||
decoder.convertFrom(this);
|
||||
}
|
||||
|
||||
|
||||
@@ -21,8 +21,11 @@ import org.redkale.convert.*;
|
||||
*/
|
||||
public class BsonStreamDecoder<T> extends StreamDecoder<BsonReader, T> {
|
||||
|
||||
public BsonStreamDecoder(final BsonFactory factory, final Type type) {
|
||||
private final boolean skip;
|
||||
|
||||
public BsonStreamDecoder(final BsonFactory factory, final Type type, boolean skip) {
|
||||
super(factory, type);
|
||||
this.skip = skip;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -32,7 +35,10 @@ public class BsonStreamDecoder<T> extends StreamDecoder<BsonReader, T> {
|
||||
if (len == Reader.SIGN_NULL) {
|
||||
return null;
|
||||
}
|
||||
final Decodeable<BsonReader, T> itemDecoder = BsonFactory.typeEnum(in.readArrayItemTypeEnum());
|
||||
Decodeable<BsonReader, T> itemDecoder = this.componentDecoder;
|
||||
if (skip) {
|
||||
itemDecoder = BsonFactory.skipTypeEnum(in.readArrayItemTypeEnum());
|
||||
}
|
||||
final List<T> result = new ArrayList();
|
||||
// 固定长度
|
||||
for (int i = 0; i < len; i++) {
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright (c) 2016-2116 Redkale
|
||||
* All rights reserved.
|
||||
*/
|
||||
package org.redkale.convert.ext;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import org.redkale.convert.Reader;
|
||||
import org.redkale.convert.SimpledCoder;
|
||||
import org.redkale.convert.Writer;
|
||||
|
||||
/**
|
||||
* AtomicAtomicBoolean 的SimpledCoder实现
|
||||
*
|
||||
* <p>详情见: https://redkale.org
|
||||
*
|
||||
* @author zhangjx
|
||||
* @param <R> Reader输入的子类型
|
||||
* @param <W> Writer输出的子类型
|
||||
*/
|
||||
public class AtomicBooleanSimpledCoder<R extends Reader, W extends Writer> extends SimpledCoder<R, W, AtomicBoolean> {
|
||||
|
||||
public static final AtomicBooleanSimpledCoder instance = new AtomicBooleanSimpledCoder();
|
||||
|
||||
@Override
|
||||
public void convertTo(W out, AtomicBoolean value) {
|
||||
out.writeBoolean(value != null && value.get());
|
||||
}
|
||||
|
||||
@Override
|
||||
public AtomicBoolean convertFrom(R in) {
|
||||
return new AtomicBoolean(in.readBoolean());
|
||||
}
|
||||
}
|
||||
@@ -472,6 +472,27 @@ public abstract class ProtobufCoders {
|
||||
}
|
||||
}
|
||||
|
||||
public static class ProtobufAtomicBooleanCollectionSimpledCoder
|
||||
extends SimpledCoder<ProtobufReader, ProtobufWriter, Collection<AtomicBoolean>>
|
||||
implements ProtobufPrimitivable {
|
||||
|
||||
private final Creator<? extends Collection> creator;
|
||||
|
||||
public ProtobufAtomicBooleanCollectionSimpledCoder(Creator<? extends Collection> creator) {
|
||||
this.creator = creator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void convertTo(ProtobufWriter out, Collection<AtomicBoolean> values) {
|
||||
out.writeAtomicBooleans(values);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<AtomicBoolean> convertFrom(ProtobufReader in) {
|
||||
return in.readAtomicBooleans(creator);
|
||||
}
|
||||
}
|
||||
|
||||
public static class ProtobufAtomicIntegerCollectionSimpledCoder
|
||||
extends SimpledCoder<ProtobufReader, ProtobufWriter, Collection<AtomicInteger>>
|
||||
implements ProtobufPrimitivable {
|
||||
@@ -642,6 +663,24 @@ public abstract class ProtobufCoders {
|
||||
}
|
||||
}
|
||||
|
||||
public static class ProtobufAtomicBooleanStreamSimpledCoder
|
||||
extends SimpledCoder<ProtobufReader, ProtobufWriter, Stream<AtomicBoolean>>
|
||||
implements ProtobufPrimitivable {
|
||||
|
||||
public static final ProtobufAtomicBooleanStreamSimpledCoder instance =
|
||||
new ProtobufAtomicBooleanStreamSimpledCoder();
|
||||
|
||||
@Override
|
||||
public void convertTo(ProtobufWriter out, Stream<AtomicBoolean> values) {
|
||||
out.writeAtomicBooleans(values);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Stream<AtomicBoolean> convertFrom(ProtobufReader in) {
|
||||
return in.readAtomicBooleans(LIST_CREATOR).stream();
|
||||
}
|
||||
}
|
||||
|
||||
public static class ProtobufAtomicIntegerStreamSimpledCoder
|
||||
extends SimpledCoder<ProtobufReader, ProtobufWriter, Stream<AtomicInteger>>
|
||||
implements ProtobufPrimitivable {
|
||||
|
||||
@@ -238,6 +238,8 @@ public abstract class ProtobufDynEncoder<T> extends ProtobufObjectEncoder<T> {
|
||||
wmethodName = "writeFieldLongsValue";
|
||||
} else if (componentType == Double.class) {
|
||||
wmethodName = "writeFieldDoublesValue";
|
||||
} else if (componentType == AtomicBoolean.class) {
|
||||
wmethodName = "writeFieldAtomicBooleansValue";
|
||||
} else if (componentType == AtomicInteger.class) {
|
||||
wmethodName = "writeFieldAtomicIntegersValue";
|
||||
} else if (componentType == AtomicLong.class) {
|
||||
|
||||
@@ -11,6 +11,8 @@ import java.util.*;
|
||||
import java.util.concurrent.atomic.*;
|
||||
import java.util.stream.Stream;
|
||||
import org.redkale.convert.*;
|
||||
import org.redkale.convert.pb.ProtobufCoders.ProtobufAtomicBooleanCollectionSimpledCoder;
|
||||
import org.redkale.convert.pb.ProtobufCoders.ProtobufAtomicBooleanStreamSimpledCoder;
|
||||
import org.redkale.convert.pb.ProtobufCoders.ProtobufAtomicIntegerArraySimpledCoder;
|
||||
import org.redkale.convert.pb.ProtobufCoders.ProtobufAtomicIntegerCollectionSimpledCoder;
|
||||
import org.redkale.convert.pb.ProtobufCoders.ProtobufAtomicIntegerStreamSimpledCoder;
|
||||
@@ -214,6 +216,9 @@ public class ProtobufFactory extends ConvertFactory<ProtobufReader, ProtobufWrit
|
||||
} else if (componentType == Double.class) {
|
||||
Creator<? extends Collection> creator = loadCreator((Class) pt.getRawType());
|
||||
return (Decodeable) new ProtobufDoubleCollectionSimpledCoder(creator);
|
||||
} else if (componentType == AtomicBoolean.class) {
|
||||
Creator<? extends Collection> creator = loadCreator((Class) pt.getRawType());
|
||||
return (Decodeable) new ProtobufAtomicBooleanCollectionSimpledCoder(creator);
|
||||
} else if (componentType == AtomicInteger.class) {
|
||||
Creator<? extends Collection> creator = loadCreator((Class) pt.getRawType());
|
||||
return (Decodeable) new ProtobufAtomicIntegerCollectionSimpledCoder(creator);
|
||||
@@ -247,6 +252,8 @@ public class ProtobufFactory extends ConvertFactory<ProtobufReader, ProtobufWrit
|
||||
return (Encodeable) new ProtobufLongCollectionSimpledCoder(creator);
|
||||
} else if (componentType == Double.class) {
|
||||
return (Encodeable) new ProtobufDoubleCollectionSimpledCoder(creator);
|
||||
} else if (componentType == AtomicBoolean.class) {
|
||||
return (Encodeable) new ProtobufAtomicBooleanCollectionSimpledCoder(creator);
|
||||
} else if (componentType == AtomicInteger.class) {
|
||||
return (Encodeable) new ProtobufAtomicIntegerCollectionSimpledCoder(creator);
|
||||
} else if (componentType == AtomicLong.class) {
|
||||
@@ -277,6 +284,8 @@ public class ProtobufFactory extends ConvertFactory<ProtobufReader, ProtobufWrit
|
||||
return (Decodeable) ProtobufLongStreamSimpledCoder.instance;
|
||||
} else if (componentType == Double.class) {
|
||||
return (Decodeable) ProtobufDoubleStreamSimpledCoder.instance;
|
||||
} else if (componentType == AtomicBoolean.class) {
|
||||
return (Decodeable) ProtobufAtomicBooleanStreamSimpledCoder.instance;
|
||||
} else if (componentType == AtomicInteger.class) {
|
||||
return (Decodeable) ProtobufAtomicIntegerStreamSimpledCoder.instance;
|
||||
} else if (componentType == AtomicLong.class) {
|
||||
@@ -307,6 +316,8 @@ public class ProtobufFactory extends ConvertFactory<ProtobufReader, ProtobufWrit
|
||||
return (Encodeable) ProtobufLongStreamSimpledCoder.instance;
|
||||
} else if (componentType == Double.class) {
|
||||
return (Encodeable) ProtobufDoubleStreamSimpledCoder.instance;
|
||||
} else if (componentType == AtomicBoolean.class) {
|
||||
return (Encodeable) ProtobufAtomicBooleanStreamSimpledCoder.instance;
|
||||
} else if (componentType == AtomicInteger.class) {
|
||||
return (Encodeable) ProtobufAtomicIntegerStreamSimpledCoder.instance;
|
||||
} else if (componentType == AtomicLong.class) {
|
||||
@@ -364,6 +375,7 @@ public class ProtobufFactory extends ConvertFactory<ProtobufReader, ProtobufWrit
|
||||
|| fieldClass == Float.class
|
||||
|| fieldClass == Long.class
|
||||
|| fieldClass == Double.class
|
||||
|| fieldClass == AtomicBoolean.class
|
||||
|| fieldClass == AtomicInteger.class
|
||||
|| fieldClass == AtomicLong.class
|
||||
|| fieldClass == String.class
|
||||
@@ -383,6 +395,7 @@ public class ProtobufFactory extends ConvertFactory<ProtobufReader, ProtobufWrit
|
||||
|| fieldClass == Float[].class
|
||||
|| fieldClass == Long[].class
|
||||
|| fieldClass == Double[].class
|
||||
|| fieldClass == AtomicBoolean[].class
|
||||
|| fieldClass == AtomicInteger[].class
|
||||
|| fieldClass == AtomicLong[].class
|
||||
|| fieldClass == String[].class;
|
||||
@@ -398,6 +411,7 @@ public class ProtobufFactory extends ConvertFactory<ProtobufReader, ProtobufWrit
|
||||
|| componentType == Float.class
|
||||
|| componentType == Long.class
|
||||
|| componentType == Double.class
|
||||
|| componentType == AtomicBoolean.class
|
||||
|| componentType == AtomicInteger.class
|
||||
|| componentType == AtomicLong.class
|
||||
|| componentType == String.class;
|
||||
@@ -429,53 +443,49 @@ public class ProtobufFactory extends ConvertFactory<ProtobufReader, ProtobufWrit
|
||||
|
||||
// see com.google.protobuf.CodedOutputStream
|
||||
protected static int computeInt32SizeNoTag(final int value) {
|
||||
if (value == 0) {
|
||||
return 1;
|
||||
}
|
||||
if (value == 0) return 1;
|
||||
return computeUInt64SizeNoTag(value);
|
||||
}
|
||||
|
||||
protected static int computeUInt64SizeNoTag(final long value) {
|
||||
if (value == 0) {
|
||||
return 1;
|
||||
}
|
||||
if (value == 0) return 1;
|
||||
int clz = Long.numberOfLeadingZeros(value);
|
||||
return ((Long.SIZE * 9 + (1 << 6)) - (clz * 9)) >>> 6;
|
||||
}
|
||||
|
||||
protected static int computeSInt32SizeNoTag(final int value) {
|
||||
if (value == 0) {
|
||||
return 1;
|
||||
}
|
||||
if (value == 0) return 1;
|
||||
return computeUInt32SizeNoTag(encodeZigZag32(value));
|
||||
}
|
||||
|
||||
protected static int computeSInt64SizeNoTag(final long value) {
|
||||
if (value == 0) {
|
||||
return 1;
|
||||
public static void main(String[] args) throws Throwable {
|
||||
for (int i = 0; i < 10000; i++) {
|
||||
int len1 = computeRawVarint32Size(encodeZigZag32(i));
|
||||
int len2 = computeSInt32SizeNoTag(i);
|
||||
if (len1 != len2) {
|
||||
throw new RuntimeException(" i = " + i + ", len1: " + len1 + ", len2: " + len2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected static int computeSInt64SizeNoTag(final long value) {
|
||||
if (value == 0) return 1;
|
||||
return computeUInt64SizeNoTag(encodeZigZag64(value));
|
||||
}
|
||||
|
||||
protected static int computeUInt32SizeNoTag(final int value) {
|
||||
if (value == 0) {
|
||||
return 1;
|
||||
}
|
||||
if (value == 0) return 1;
|
||||
int clz = Integer.numberOfLeadingZeros(value);
|
||||
return ((Integer.SIZE * 9 + (1 << 6)) - (clz * 9)) >>> 6;
|
||||
}
|
||||
|
||||
protected static int encodeZigZag32(final int n) {
|
||||
if (n == 0) {
|
||||
return 0;
|
||||
}
|
||||
if (n == 0) return 0;
|
||||
return (n << 1) ^ (n >> 31);
|
||||
}
|
||||
|
||||
protected static long encodeZigZag64(final long n) {
|
||||
if (n == 0) {
|
||||
return 0L;
|
||||
}
|
||||
if (n == 0) return 0L;
|
||||
return (n << 1) ^ (n >> 63);
|
||||
}
|
||||
|
||||
@@ -504,7 +514,8 @@ public class ProtobufFactory extends ConvertFactory<ProtobufReader, ProtobufWrit
|
||||
|| (javaType == short.class || javaType == Short.class)
|
||||
|| (javaType == int.class || javaType == Integer.class)
|
||||
|| (javaType == long.class || javaType == Long.class)
|
||||
|| (javaType == AtomicInteger.class || javaType == AtomicLong.class)) {
|
||||
|| (javaType == AtomicBoolean.class || javaType == AtomicInteger.class)
|
||||
|| javaType == AtomicLong.class) {
|
||||
return 0;
|
||||
} else if (!enumtostring && (javaType instanceof Class) && ((Class) javaType).isEnum()) {
|
||||
return 0;
|
||||
|
||||
@@ -190,6 +190,24 @@ public class ProtobufReader extends Reader {
|
||||
return data;
|
||||
}
|
||||
|
||||
public final AtomicBoolean readAtomicBoolean() {
|
||||
return new AtomicBoolean(readBoolean());
|
||||
}
|
||||
|
||||
public final AtomicBoolean[] readAtomicBooleans() {
|
||||
Collection<AtomicBoolean> data = readAtomicBooleans(LIS_CREATOR);
|
||||
return data.toArray(new AtomicBoolean[data.size()]);
|
||||
}
|
||||
|
||||
public final Collection<AtomicBoolean> readAtomicBooleans(Creator<? extends Collection> creator) {
|
||||
int size = readRawVarint32();
|
||||
Collection<AtomicBoolean> data = creator.create();
|
||||
for (int i = 0; i < size; i++) {
|
||||
data.add(new AtomicBoolean(readBoolean()));
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final byte readByte() {
|
||||
return (byte) readInt();
|
||||
|
||||
@@ -24,6 +24,7 @@ public class ProtobufWriter extends Writer implements ByteTuple {
|
||||
Integer.getInteger("redkale.convert.writer.buffer.defsize", 1024));
|
||||
|
||||
private static final int CHILD_SIZE = 8;
|
||||
protected static final byte[] EMPTY_BYTES = new byte[0];
|
||||
|
||||
protected static final int TENTHOUSAND_MAX = 10001;
|
||||
|
||||
@@ -401,7 +402,7 @@ public class ProtobufWriter extends Writer implements ByteTuple {
|
||||
if (array != null && array.length > 0) {
|
||||
for (String item : array) {
|
||||
writeTag(tag);
|
||||
byte[] bs = item == null ? new byte[0] : item.getBytes(StandardCharsets.UTF_8);
|
||||
byte[] bs = item == null ? EMPTY_BYTES : item.getBytes(StandardCharsets.UTF_8);
|
||||
writeBytes(bs);
|
||||
}
|
||||
}
|
||||
@@ -412,7 +413,7 @@ public class ProtobufWriter extends Writer implements ByteTuple {
|
||||
if (array != null && !array.isEmpty()) {
|
||||
for (String item : array) {
|
||||
writeTag(tag);
|
||||
byte[] bs = item == null ? new byte[0] : item.getBytes(StandardCharsets.UTF_8);
|
||||
byte[] bs = item == null ? EMPTY_BYTES : item.getBytes(StandardCharsets.UTF_8);
|
||||
writeBytes(bs);
|
||||
}
|
||||
}
|
||||
@@ -758,6 +759,32 @@ public class ProtobufWriter extends Writer implements ByteTuple {
|
||||
}
|
||||
}
|
||||
|
||||
public final void writeAtomicBooleans(AtomicBoolean[] value) {
|
||||
AtomicBoolean[] array = value;
|
||||
if (array != null && array.length > 0) {
|
||||
writeLength(array.length);
|
||||
for (AtomicBoolean item : array) {
|
||||
writeTo(item != null && item.get() ? (byte) 1 : (byte) 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public final void writeAtomicBooleans(Collection<AtomicBoolean> value) {
|
||||
Collection<AtomicBoolean> array = value;
|
||||
if (array != null && !array.isEmpty()) {
|
||||
writeLength(array.size());
|
||||
for (AtomicBoolean item : array) {
|
||||
writeTo(item != null && item.get() ? (byte) 1 : (byte) 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public final void writeAtomicBooleans(Stream<AtomicBoolean> value) {
|
||||
if (value != null) {
|
||||
writeAtomicBooleans(value.toArray(s -> new AtomicBoolean[s]));
|
||||
}
|
||||
}
|
||||
|
||||
public final void writeAtomicIntegers(AtomicInteger[] value) {
|
||||
AtomicInteger[] array = value;
|
||||
if (array != null && array.length > 0) {
|
||||
@@ -961,6 +988,14 @@ public class ProtobufWriter extends Writer implements ByteTuple {
|
||||
}
|
||||
}
|
||||
|
||||
@ClassDepends
|
||||
public final void writeFieldValue(int tag, AtomicBoolean value) {
|
||||
if (value != null && value.get()) {
|
||||
writeTag(tag);
|
||||
writeBoolean(value.get());
|
||||
}
|
||||
}
|
||||
|
||||
@ClassDepends
|
||||
public final void writeFieldValue(int tag, AtomicInteger value) {
|
||||
if (value != null && value.get() != 0) {
|
||||
@@ -1105,6 +1140,14 @@ public class ProtobufWriter extends Writer implements ByteTuple {
|
||||
}
|
||||
}
|
||||
|
||||
@ClassDepends
|
||||
public final void writeFieldValue(int tag, AtomicBoolean[] value) {
|
||||
if (value != null && value.length > 0) {
|
||||
writeTag(tag);
|
||||
writeAtomicBooleans(value);
|
||||
}
|
||||
}
|
||||
|
||||
@ClassDepends
|
||||
public final void writeFieldValue(int tag, AtomicInteger[] value) {
|
||||
if (value != null && value.length > 0) {
|
||||
@@ -1192,6 +1235,14 @@ public class ProtobufWriter extends Writer implements ByteTuple {
|
||||
}
|
||||
}
|
||||
|
||||
@ClassDepends
|
||||
public final void writeFieldAtomicBooleansValue(int tag, Collection<AtomicBoolean> value) {
|
||||
if (value != null && !value.isEmpty()) {
|
||||
writeTag(tag);
|
||||
writeAtomicBooleans(value);
|
||||
}
|
||||
}
|
||||
|
||||
@ClassDepends
|
||||
public final void writeFieldAtomicIntegersValue(int tag, Collection<AtomicInteger> value) {
|
||||
if (value != null && !value.isEmpty()) {
|
||||
|
||||
@@ -11,6 +11,8 @@ import java.nio.ByteBuffer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.function.Supplier;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.redkale.convert.ConvertColumn;
|
||||
@@ -27,7 +29,7 @@ import org.redkale.util.Utility;
|
||||
public class GenericEntityTest {
|
||||
private static final Type ENTITY_TYPE = new TypeToken<GenericEntity<Long, String, SimpleEntity>>() {}.getType();
|
||||
private static final String JSON =
|
||||
"{\"oneEntry\":{\"key\":\"aaaa\",\"value\":{\"addr\":\"127.0.0.1:6666\",\"addrs\":[22222,33333,44444,55555,66666,77777,88888,99999],\"desc\":\"\",\"id\":1000000001,\"lists\":[\"aaaa\",\"bbbb\",\"cccc\"],\"map\":{\"AAA\":111,\"CCC\":333,\"BBB\":222},\"name\":\"this is name\\n \\\"test\",\"strings\":[\"zzz\",\"yyy\",\"xxx\"]}},\"oneList\":[1234567890],\"oneName\":\"你好\"}";
|
||||
"{\"oneEntry\":{\"key\":\"aaaa\",\"value\":{\"addr\":\"127.0.0.1:6666\",\"addrs\":[22222,-33333,44444,-55555,66666,-77777,88888,-99999],\"desc\":\"\",\"id\":1000000001,\"lists\":[\"aaaa\",\"bbbb\",\"cccc\"],\"map\":{\"AAA\":111,\"CCC\":333,\"BBB\":-222},\"name\":\"this is name\\n \\\"test\",\"strings\":[\"zzz\",\"yyy\",\"xxx\"]}},\"oneList\":[1234567890],\"oneName\":\"你好\",\"oneStatus\":[true,false],\"oneTimes\":[128]}";
|
||||
|
||||
public static void main(String[] args) throws Throwable {
|
||||
GenericEntityTest test = new GenericEntityTest();
|
||||
@@ -175,6 +177,10 @@ public class GenericEntityTest {
|
||||
List<Long> list = new ArrayList<>();
|
||||
list.add(1234567890L);
|
||||
bean.setOneList(list);
|
||||
bean.setOneStatus(new AtomicBoolean[] {new AtomicBoolean(true), new AtomicBoolean(false)});
|
||||
List<AtomicLong> times = new ArrayList<>();
|
||||
times.add(new AtomicLong(128L));
|
||||
bean.setOneTimes(times);
|
||||
bean.setOneEntry(new Entry<>("aaaa", SimpleEntity.create()));
|
||||
return bean;
|
||||
}
|
||||
@@ -190,6 +196,12 @@ public class GenericEntityTest {
|
||||
@ConvertColumn(index = 3)
|
||||
private K oneName;
|
||||
|
||||
@ConvertColumn(index = 4)
|
||||
private AtomicBoolean[] oneStatus;
|
||||
|
||||
@ConvertColumn(index = 5)
|
||||
private List<AtomicLong> oneTimes;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return JsonConvert.root().convertTo(this);
|
||||
@@ -218,6 +230,22 @@ public class GenericEntityTest {
|
||||
public void setOneName(K oneName) {
|
||||
this.oneName = oneName;
|
||||
}
|
||||
|
||||
public AtomicBoolean[] getOneStatus() {
|
||||
return oneStatus;
|
||||
}
|
||||
|
||||
public void setOneStatus(AtomicBoolean[] oneStatus) {
|
||||
this.oneStatus = oneStatus;
|
||||
}
|
||||
|
||||
public List<AtomicLong> getOneTimes() {
|
||||
return oneTimes;
|
||||
}
|
||||
|
||||
public void setOneTimes(List<AtomicLong> oneTimes) {
|
||||
this.oneTimes = oneTimes;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Entry<K, V> {
|
||||
|
||||
@@ -41,7 +41,7 @@ public class SimpleEntity {
|
||||
SimpleEntity v = new SimpleEntity();
|
||||
v.setName("this is name\n \"test");
|
||||
v.setId(1000000001);
|
||||
v.setAddrs(new int[] {22222, 33333, 44444, 55555, 66666, 77777, 88888, 99999});
|
||||
v.setAddrs(new int[] {22222, -33333, 44444, -55555, 66666, -77777, 88888, -99999});
|
||||
v.setStrings(new String[] {"zzz", "yyy", "xxx"});
|
||||
List<String> list = new ArrayList<>();
|
||||
list.add("aaaa");
|
||||
@@ -50,7 +50,7 @@ public class SimpleEntity {
|
||||
v.setLists(list);
|
||||
Map<String, Integer> map = new HashMap<>();
|
||||
map.put("AAA", 111);
|
||||
map.put("BBB", 222);
|
||||
map.put("BBB", -222);
|
||||
map.put("CCC", 333);
|
||||
v.setMap(map);
|
||||
v.setAddr(new InetSocketAddress("127.0.0.1", 6666));
|
||||
|
||||
Reference in New Issue
Block a user