Sorted fields sorting logc in BinaryObjectBuilder. This closes
@@ -500,6 +500,21 @@ public class BinaryObjectBuilderImpl implements BinaryObjectBuilder {
}
}
+ /**
+ * If value of {@link #assignedVals} is null, set it according to
+ * {@link BinaryUtils#FIELDS_SORTED_ORDER}.
+ */
+ private Map<String, Object> assignedValues() {
+ if (assignedVals == null) {
+ if (BinaryUtils.FIELDS_SORTED_ORDER)
+ assignedVals = new TreeMap<>();
+ else
+ assignedVals = new LinkedHashMap<>();
+ }
+
+ return assignedVals;
+ }
+
/** {@inheritDoc} */
@SuppressWarnings("unchecked")
@Override public <T> T getField(String name) {
@@ -526,19 +541,12 @@ public class BinaryObjectBuilderImpl implements BinaryObjectBuilder {
@Override public BinaryObjectBuilder setField(String name, Object val0) {
Object val = val0 == null ? new BinaryValueWithType(BinaryUtils.typeByClass(Object.class), null) : val0;
- if (assignedVals == null) {
- if (BinaryUtils.FIELDS_SORTED_ORDER)
- assignedVals = new TreeMap<>();
- else
- assignedVals = new LinkedHashMap<>();
- }
-
- Object oldVal = assignedVals.put(name, val);
+ Object oldVal = assignedValues().put(name, val);
if (oldVal instanceof BinaryValueWithType && val0 != null) {
((BinaryValueWithType)oldVal).value(val);
- assignedVals.put(name, oldVal);
+ assignedValues().put(name, oldVal);
}
return this;
@@ -555,10 +563,7 @@ public class BinaryObjectBuilderImpl implements BinaryObjectBuilder {
else
typeId = BinaryUtils.typeByClass(type);
- if (assignedVals == null)
- assignedVals = new LinkedHashMap<>();
-
- assignedVals.put(name, new BinaryValueWithType(typeId, val));
+ assignedValues().put(name, new BinaryValueWithType(typeId, val));
return this;
}
@@ -578,10 +583,7 @@ public class BinaryObjectBuilderImpl implements BinaryObjectBuilder {
* @return {@code this} instance for chaining.
*/
@Override public BinaryObjectBuilderImpl removeField(String name) {
- if (assignedVals == null)
- assignedVals = new LinkedHashMap<>();
-
- assignedVals.put(name, REMOVED_FIELD_MARKER);
+ assignedValues().put(name, REMOVED_FIELD_MARKER);
return this;
}
