Class PrimitiveFieldTripComponent<P,T>
- Type Parameters:
P- the parent typeT- the component type, the wrapper of the primitive
- All Implemented Interfaces:
TripComponent<P,T>
Serializing a primitive field through the generic FieldTripComponent boxes it twice on
the way out — once to read it off the VarHandle, whose erased call
site is typed (Object)Object, and once more to compare it with the default value through
Objects.equals(Object, Object) — and allocates a box per field on the way in. For a
long or a double that is an allocation every time; for an int it is one
outside the Integer cache. An entity with a dozen numeric columns pays that a dozen times
per instance, on both sides, for every object of a result list.
This implementation keeps the value primitive end to end: the VarHandle is invoked with
a primitive signature, the default is compared as a primitive, and the value goes straight to the
primitive codec of its trip type.
A single class with a PrimitiveFieldTripComponent.Kind switch rather than one class per primitive: the loop in
ObjectTripType walks a TripComponent[], so eight
implementations would make that call site megamorphic, while one keeps it to two receivers.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumThe primitive kinds a field can have. -
Field Summary
Fields inherited from class FieldTripComponent
MUTATE_FINAL_FIELDS_PROPERTY -
Constructor Summary
ConstructorsConstructorDescriptionPrimitiveFieldTripComponent(TripType<P> parentType, int ordinal, Field field, boolean hasDefaultValue, T defaultValue) Creates a new PrimitiveFieldTripComponent. -
Method Summary
Modifier and TypeMethodDescriptiongetKind()Gets the primitive kind of this component.Gets the primitive kind of the given class.voidread(P parent, TripStream stream, OpCode opCode) Reads this component's value from the stream and assigns it to the given parent object.intwrite(P parent, TripStream stream, int lastOrdinal) Writes this component of the given parent object to the stream, unless it holds its default value.Methods inherited from class FieldTripComponent
getComponentClass, getDefaultValue, getField, getHandle, getName, getOrdinal, getParentType, getType, getValue, hasDefaultValue, isFinalField, isFinalFieldMutationEnabled, setFinalFieldMutationEnabled, setValue, toStringMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface TripComponent
readValue, writeValue
-
Constructor Details
-
PrimitiveFieldTripComponent
public PrimitiveFieldTripComponent(TripType<P> parentType, int ordinal, Field field, boolean hasDefaultValue, T defaultValue) Creates a new PrimitiveFieldTripComponent.- Parameters:
parentType- the parent typeordinal- the ordinal starting from 0field- the field, of a primitive typehasDefaultValue- true if the component has a default value, false otherwisedefaultValue- the default value, the wrapper of the primitive
-
-
Method Details
-
getKind
Gets the primitive kind of this component.- Returns:
- the kind, never null
-
write
Description copied from interface:TripComponentWrites this component of the given parent object to the stream, unless it holds its default value.The component's ordinal precedes the value, unless it simply follows the one written before (see
TripStream.writeNextComponentOrdinal(int, TripComponent)), or it is the first component written and its value is null. A component without a default value is always written, which is what records and classes with a canonical constructor rely on.The value is read from the parent exactly once, which is why the default check lives here rather than in a predicate of its own: for a component backed by a getter, reading it twice would mean invoking the getter twice, and for a primitive one it would mean boxing twice.
- Parameters:
parent- the parent objectstream- the stream to write tolastOrdinal- the ordinal of the component written before, -1 if none- Returns:
- the ordinal written, or
lastOrdinalif the component was skipped
-
read
Description copied from interface:TripComponentReads this component's value from the stream and assigns it to the given parent object.- Parameters:
parent- the parent objectstream- the stream to read fromopCode- the op code (already read from the stream)
-
kindOf
Gets the primitive kind of the given class.- Parameters:
clazz- the class to map- Returns:
- the kind, null if the class is not primitive
-