This commit is contained in:
Redkale
2018-07-16 00:33:45 +08:00
parent 9bd1e0c97b
commit 9f9078cdc5
4 changed files with 25 additions and 15 deletions

View File

@@ -20,11 +20,11 @@ import org.redkale.util.Attribute;
* @param <F> 字段的数据类型 * @param <F> 字段的数据类型
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public final class DeMember<R extends Reader, T, F> implements Comparable<DeMember<R, T, F>> { public final class DeMember<R extends Reader, T, F> {
protected int index; protected int index;
protected boolean fieldSort; protected int position; //从1开始
protected final Attribute<T, F> attribute; protected final Attribute<T, F> attribute;
@@ -76,8 +76,11 @@ public final class DeMember<R extends Reader, T, F> implements Comparable<DeMemb
return this.index; return this.index;
} }
@Override public int getPosition() {
public final int compareTo(DeMember<R, T, F> o) { return this.position;
}
public int compareTo(boolean fieldSort, DeMember<R, T, F> o) {
if (o == null) return -1; if (o == null) return -1;
if (this.index != o.index) return (this.index == 0 ? Integer.MAX_VALUE : this.index) - (o.index == 0 ? Integer.MAX_VALUE : o.index); if (this.index != o.index) return (this.index == 0 ? Integer.MAX_VALUE : this.index) - (o.index == 0 ? Integer.MAX_VALUE : o.index);
return fieldSort ? this.attribute.field().compareTo(o.attribute.field()) : 0; return fieldSort ? this.attribute.field().compareTo(o.attribute.field()) : 0;
@@ -88,7 +91,7 @@ public final class DeMember<R extends Reader, T, F> implements Comparable<DeMemb
if (this == obj) return true; if (this == obj) return true;
if (!(obj instanceof DeMember)) return false; if (!(obj instanceof DeMember)) return false;
DeMember other = (DeMember) obj; DeMember other = (DeMember) obj;
return compareTo(other) == 0; return compareTo(true, other) == 0;
} }
@Override @Override

View File

@@ -20,7 +20,7 @@ import org.redkale.util.Attribute;
* @param <F> 字段的数据类型 * @param <F> 字段的数据类型
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public final class EnMember<W extends Writer, T, F> implements Comparable<EnMember<W, T, F>> { public final class EnMember<W extends Writer, T, F> {
final Attribute<T, F> attribute; final Attribute<T, F> attribute;
@@ -33,7 +33,7 @@ public final class EnMember<W extends Writer, T, F> implements Comparable<EnMemb
protected int index; protected int index;
protected boolean fieldSort; protected int position; //从1开始
public EnMember(Attribute<T, F> attribute, Encodeable<W, F> encoder) { public EnMember(Attribute<T, F> attribute, Encodeable<W, F> encoder) {
this.attribute = attribute; this.attribute = attribute;
@@ -69,8 +69,11 @@ public final class EnMember<W extends Writer, T, F> implements Comparable<EnMemb
return this.index; return this.index;
} }
@Override public int getPosition() {
public final int compareTo(EnMember<W, T, F> o) { return this.position;
}
public int compareTo(boolean fieldSort, EnMember<W, T, F> o) {
if (o == null) return -1; if (o == null) return -1;
if (this.index != o.index) return (this.index == 0 ? Integer.MAX_VALUE : this.index) - (o.index == 0 ? Integer.MAX_VALUE : o.index); if (this.index != o.index) return (this.index == 0 ? Integer.MAX_VALUE : this.index) - (o.index == 0 ? Integer.MAX_VALUE : o.index);
return fieldSort ? this.attribute.field().compareTo(o.attribute.field()) : 0; return fieldSort ? this.attribute.field().compareTo(o.attribute.field()) : 0;
@@ -81,7 +84,7 @@ public final class EnMember<W extends Writer, T, F> implements Comparable<EnMemb
if (this == obj) return true; if (this == obj) return true;
if (!(obj instanceof EnMember)) return false; if (!(obj instanceof EnMember)) return false;
EnMember other = (EnMember) obj; EnMember other = (EnMember) obj;
return compareTo(other) == 0; return compareTo(true, other) == 0;
} }
@Override @Override

View File

@@ -33,7 +33,7 @@ public final class ObjectDecoder<R extends Reader, T> implements Decodeable<R, T
protected DeMember<R, T, ?>[] creatorConstructorMembers = new DeMember[0]; protected DeMember<R, T, ?>[] creatorConstructorMembers = new DeMember[0];
protected DeMember<R, T, ?>[] members; protected DeMember[] members;
protected ConvertFactory factory; protected ConvertFactory factory;
@@ -97,7 +97,6 @@ public final class ObjectDecoder<R extends Reader, T> implements Decodeable<R, T
if (ref != null && ref.ignore()) continue; if (ref != null && ref.ignore()) continue;
Type t = TypeToken.createClassType(TypeToken.getGenericType(field.getGenericType(), this.type), this.type); Type t = TypeToken.createClassType(TypeToken.getGenericType(field.getGenericType(), this.type), this.type);
DeMember member = new DeMember(ObjectEncoder.createAttribute(factory, clazz, field, null, null), factory.loadDecoder(t)); DeMember member = new DeMember(ObjectEncoder.createAttribute(factory, clazz, field, null, null), factory.loadDecoder(t));
member.fieldSort = factory.isFieldSort();
if (ref != null) member.index = ref.getIndex(); if (ref != null) member.index = ref.getIndex();
list.add(member); list.add(member);
} }
@@ -157,7 +156,10 @@ public final class ObjectDecoder<R extends Reader, T> implements Decodeable<R, T
} }
} }
this.members = list.toArray(new DeMember[list.size()]); this.members = list.toArray(new DeMember[list.size()]);
Arrays.sort(this.members); Arrays.sort(this.members, (a, b) -> a.compareTo(factory.isFieldSort(), b));
for (int i = 0; i < this.members.length; i++) {
this.members[i].position = (i + 1);
}
if (cps != null) { if (cps != null) {
final String[] fields = cps; final String[] fields = cps;

View File

@@ -104,12 +104,14 @@ public final class ObjectEncoder<W extends Writer, T> implements Encodeable<W, T
if (ref != null && ref.ignore()) continue; if (ref != null && ref.ignore()) continue;
Type t = TypeToken.createClassType(TypeToken.getGenericType(method.getGenericReturnType(), this.type), this.type); Type t = TypeToken.createClassType(TypeToken.getGenericType(method.getGenericReturnType(), this.type), this.type);
EnMember member = new EnMember(createAttribute(factory, clazz, null, method, null), factory.loadEncoder(t)); EnMember member = new EnMember(createAttribute(factory, clazz, null, method, null), factory.loadEncoder(t));
member.fieldSort = factory.isFieldSort();
if (ref != null) member.index = ref.getIndex(); if (ref != null) member.index = ref.getIndex();
list.add(member); list.add(member);
} }
this.members = list.toArray(new EnMember[list.size()]); this.members = list.toArray(new EnMember[list.size()]);
Arrays.sort(this.members); Arrays.sort(this.members, (a, b) -> a.compareTo(factory.isFieldSort(), b));
for (int i = 0; i < this.members.length; i++) {
this.members[i].position = (i + 1);
}
} catch (Exception ex) { } catch (Exception ex) {
throw new ConvertException(ex); throw new ConvertException(ex);