Class PrimitiveFieldTripComponent<P,T>

java.lang.Object
org.tentackle.trip.impl.FieldTripComponent<P,T>
org.tentackle.trip.impl.PrimitiveFieldTripComponent<P,T>
Type Parameters:
P - the parent type
T - the component type, the wrapper of the primitive
All Implemented Interfaces:
TripComponent<P,T>

public class PrimitiveFieldTripComponent<P,T> extends FieldTripComponent<P,T>
A TripComponent implementation for fields of a primitive type.

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.

  • Constructor Details

    • PrimitiveFieldTripComponent

      public PrimitiveFieldTripComponent(TripType<P> parentType, int ordinal, Field field, boolean hasDefaultValue, T defaultValue)
      Creates a new PrimitiveFieldTripComponent.
      Parameters:
      parentType - the parent type
      ordinal - the ordinal starting from 0
      field - the field, of a primitive type
      hasDefaultValue - true if the component has a default value, false otherwise
      defaultValue - the default value, the wrapper of the primitive
  • Method Details

    • getKind

      Gets the primitive kind of this component.
      Returns:
      the kind, never null
    • write

      public int write(P parent, TripStream stream, int lastOrdinal)
      Description copied from interface: TripComponent
      Writes 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 object
      stream - the stream to write to
      lastOrdinal - the ordinal of the component written before, -1 if none
      Returns:
      the ordinal written, or lastOrdinal if the component was skipped
    • read

      public void read(P parent, TripStream stream, OpCode opCode)
      Description copied from interface: TripComponent
      Reads this component's value from the stream and assigns it to the given parent object.
      Parameters:
      parent - the parent object
      stream - the stream to read from
      opCode - the op code (already read from the stream)
    • kindOf

      public static PrimitiveFieldTripComponent.Kind kindOf(Class<?> clazz)
      Gets the primitive kind of the given class.
      Parameters:
      clazz - the class to map
      Returns:
      the kind, null if the class is not primitive