Record Class Bound

java.lang.Object
java.lang.Record
org.sofabuffers.sofab.Bound
Record Components:
max - the stated number — a count, maxlen or cap, at least 1 — or -1 for SCHEMA_BOUNDED, whose rule carries none
rule - which rule the number states, and so which verdict a breach earns

public record Bound(long max, Bound.Rule rule) extends Record
Which of the two rules of CORELIB_PLAN §6.2.1 bounds this field: the schema's count/maxlen, or a receiver cap the deployment configured — and, with it, the number and the verdict a breach earns.

The two are mutually exclusive — §6.2.1: receiver limits "MUST NOT be applied to a field the schema already bounds. There the schema bound governs and its violation is INVALID" — and they are the only two answers. This type exists so that a call site has to give one of them, and so that saying which is part of the value rather than a convention beside it:

  • schema(long) — the schema declares the ceiling n. The call it is handed to compares against it, and a breach is SofabError.INVALID_MSG (MESSAGE_SPEC §7.1): the bytes contradict the schema both peers agreed on. It never reports SofabError.LIMIT_EXCEEDED.
  • receiver(long) — the schema declares none, and this is the deployment's max_dyn_string_len / max_dyn_blob_len / max_dyn_array_count for the field. A breach is SofabError.LIMIT_EXCEEDED: the bytes are well-formed and the same message decodes under a looser cap.
  • SCHEMA_BOUNDED — the schema declares a maxlen for a string/blob the caller has already enforced at the same header, so a PayloadAcc length check has no second number to compare. It carries no number, and so it cannot bound an array index: every Seq reservation refuses it as SofabError.ARGUMENT and wants schema(long) instead.

One implementation of each rule. The Seq reservations take the schema count as Bound.schema(count) and compare it themselves, exactly where they compare a receiver cap, so a caller that passes a bound does not also guard in front of the call. §6.2.1's "never both" then holds by construction: one argument, one number, one verdict. The index is compared before anything is created or grown (CORELIB_PLAN §7.2 item 8).

Why a type and not a number. These answers used to share one long parameter, with a negative sentinel for "the schema bounds this" — so a caller who had forgotten to state a cap and a caller asserting the schema's rule handed over the identical bit pattern, and the forgotten one decoded uncapped and unreported. Here the Bound.Rule travels with the number, and a bound carrying a number is made by naming its rule: schema(long), receiver(long), or the canonical constructor, which validates exactly as those two do.

Both "forgot" values are diagnosed, not obeyed. Java writes an unassigned long field as 0 and the retired sentinel was -1; every way of making a bound refuses both with SofabError.ARGUMENT, and a null reference reaching one of the calls is refused the same way rather than read as no cap. §6.2.1 admits "no unset state and no unlimited mode", and a codec "MUST NOT read an omitted argument as unlimited".

This library still holds no limit. A Bound is built by the caller out of the caller's number, used for one comparison and not retained; nothing here defaults one, falls back to one, or clamps to one. Sofab.ARRAY_MAX and Sofab.ID_MAX are format ceilings, and §6.2.1 forbids presenting one as a receiver cap — so there is deliberately no Bound that means "the format ceiling".

Where the value comes from, and what it costs. A cap is a constant of the deployment and a schema bound a constant of the schema, so generated code builds its Bounds once, into static final fields, and passes them per call:

private static final Bound CAP_DYN_ARRAY_COUNT = Bound.receiver(MAX_DYN_ARRAY_COUNT);
private static final Bound SCHEMA_COUNT_8 = Bound.schema(8);

// schema `count: 8` -- compared inside, INVALID_MSG at index 8 and above
Seq.placeElem(m.tags, id, "", s, SCHEMA_COUNT_8);
// schema declares no count -- compared inside, LIMIT_EXCEEDED at the cap
Seq.placeElem(m.notes, id, "", s, CAP_DYN_ARRAY_COUNT);

Why a record. HotSpot treats the final fields of a record as true constants, which it does not do for an ordinary class. A static final Bound is therefore folded into the comparison it guards — id >= 8 compiles to a compare against an immediate, exactly the guard generated code used to emit in front of the call — where a class's field would be loaded on every call. Measured on the generator's java bench row (vehicle_telemetry, decode): about 100 Ir/op, a third of a percent. Nothing is allocated per call or per message on that path.

Instances are immutable and shareable.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static enum 
    Which of §6.2.1's answers a Bound states, and so its verdict.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Bound
    The schema bounds this string or blob and the caller has already enforced its maxlen at the same header, as Sofab.invalid(String) (MESSAGE_SPEC §7.1), so §6.2.1 forbids a receiver cap on it and there is no second number for PayloadAcc to compare.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Bound(long max, Bound.Rule rule)
    Validates a bound however it is made: the rule must be stated, a stated number is at least 1, and only Bound.Rule.CALLER_CHECKED carries none (-1).
  • Method Summary

    Modifier and Type
    Method
    Description
    final boolean
    Indicates whether some other object is "equal to" this one.
    final int
    Returns a hash code value for this object.
    long
    max()
    Returns the value of the max record component.
    static Bound
    receiver(long max)
    The receiver cap this deployment configured for a field the schema leaves unbounded — one of the three max_dyn_* limits of §6.2.1.
    Returns the value of the rule record component.
    static Bound
    schema(long n)
    The bound the schema declares for this field — an array's count, or a string/blob's maxlen — compared by the call it is handed to.
    A short description naming which rule this states, for diagnostics.

    Methods inherited from class Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • SCHEMA_BOUNDED

      public static final Bound SCHEMA_BOUNDED
      The schema bounds this string or blob and the caller has already enforced its maxlen at the same header, as Sofab.invalid(String) (MESSAGE_SPEC §7.1), so §6.2.1 forbids a receiver cap on it and there is no second number for PayloadAcc to compare.

      It is not "unlimited", and it is not "unset". It is a statement about which rule applies, made by the layer that knows the schema. Passing it for a field the schema leaves unbounded lets the sender decide how much this process holds; that is a defect in the call, not a mode this library offers.

      It bounds no array index. An index has a reservation to ride, and the reservation compares the schema count itself when handed schema(long); every Seq call refuses this value as SofabError.ARGUMENT rather than growing a list uncompared. Its -1 is what makes that refusal free: every index is >= -1, so the one comparison Seq.checkIndex(int, Bound) makes always sends it to the out-of-line path.

  • Constructor Details

  • Method Details

    • schema

      public static Bound schema(long n)
      The bound the schema declares for this field — an array's count, or a string/blob's maxlen — compared by the call it is handed to. A value past it is SofabError.INVALID_MSG (MESSAGE_SPEC §7.1), never SofabError.LIMIT_EXCEEDED: the bytes contradict the schema, and no receiver configuration makes them decode.

      A count is a capacity, not a length: the destination starts empty and the wire carries the length, so an element index id breaches it when id >= count. A maxlen bounds a byte length, which breaches it when length > maxlen.

      The number is the schema's, and §6.2.1's exclusivity is the caller's to honour by handing over exactly one bound per field: where the schema declares this, no receiver cap applies.

      Parameters:
      n - the schema's count or maxlen, at least 1 — what the schema validator admits
      Returns:
      the bound to hand to the call that guards the field
      Throws:
      IllegalArgumentException - if n is below 1; it refines SofabError.ARGUMENT (§6.3). 0 is an unassigned field and a negative is the retired sentinel, neither of them a schema bound
    • receiver

      public static Bound receiver(long max)
      The receiver cap this deployment configured for a field the schema leaves unbounded — one of the three max_dyn_* limits of §6.2.1. A value at or below the cap passes; above it the call raises SofabError.LIMIT_EXCEEDED, never a clamp.

      The number is the caller's: §6.2.1 puts the value with generated code, which knows the schema and the target, and this library neither supplies one nor remembers one.

      A cap must be at least 1, and the two values below that are refused rather than interpreted, because both are how a cap gets forgotten rather than chosen:

      • 0 is what Java writes into an unassigned long or int field, so accepting it would let an unconfigured constant silently become a policy that refuses every non-empty value;
      • a negative was the retired sentinel for "the schema bounds this", and is the bit pattern this type exists to stop meaning two things.

      A caller whose policy really is "accept nothing here" says so by rejecting the field itself; that is not a cap.

      Parameters:
      max - the configured limit — a byte length for a string or blob, an element count for an array — at least 1
      Returns:
      the bound to hand to the call that guards the field
      Throws:
      IllegalArgumentException - if max is below 1; it refines SofabError.ARGUMENT (§6.3), and is raised where the value is stated rather than on the decode path
    • toString

      public String toString()
      A short description naming which rule this states, for diagnostics.
      Specified by:
      toString in class Record
      Returns:
      "Bound.SCHEMA_BOUNDED", "Bound.schema(n)" or "Bound.receiver(n)"
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared with Objects::equals(Object,Object); primitive components are compared with the compare method from their corresponding wrapper classes.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • max

      public long max()
      Returns the value of the max record component.
      Returns:
      the value of the max record component
    • rule

      public Bound.Rule rule()
      Returns the value of the rule record component.
      Returns:
      the value of the rule record component