Record Class Bound
- Record Components:
max- the stated number — acount,maxlenor cap, at least1— or-1forSCHEMA_BOUNDED, whose rule carries nonerule- which rule the number states, and so which verdict a breach earns
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 ceilingn. The call it is handed to compares against it, and a breach isSofabError.INVALID_MSG(MESSAGE_SPEC §7.1): the bytes contradict the schema both peers agreed on. It never reportsSofabError.LIMIT_EXCEEDED.receiver(long)— the schema declares none, and this is the deployment'smax_dyn_string_len/max_dyn_blob_len/max_dyn_array_countfor the field. A breach isSofabError.LIMIT_EXCEEDED: the bytes are well-formed and the same message decodes under a looser cap.SCHEMA_BOUNDED— the schema declares amaxlenfor astring/blobthe caller has already enforced at the same header, so aPayloadAcclength check has no second number to compare. It carries no number, and so it cannot bound an array index: everySeqreservation refuses it asSofabError.ARGUMENTand wantsschema(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 ClassesModifier and TypeClassDescriptionstatic enumWhich of §6.2.1's answers aBoundstates, and so its verdict. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final BoundThe schema bounds thisstringorbloband the caller has already enforced itsmaxlenat the same header, asSofab.invalid(String)(MESSAGE_SPEC §7.1), so §6.2.1 forbids a receiver cap on it and there is no second number forPayloadAccto compare. -
Constructor Summary
ConstructorsConstructorDescriptionBound(long max, Bound.Rule rule) Validates a bound however it is made: the rule must be stated, a stated number is at least1, and onlyBound.Rule.CALLER_CHECKEDcarries none (-1). -
Method Summary
Modifier and TypeMethodDescriptionfinal booleanIndicates whether some other object is "equal to" this one.final inthashCode()Returns a hash code value for this object.longmax()Returns the value of themaxrecord component.static Boundreceiver(long max) The receiver cap this deployment configured for a field the schema leaves unbounded — one of the threemax_dyn_*limits of §6.2.1.rule()Returns the value of therulerecord component.static Boundschema(long n) The bound the schema declares for this field — an array'scount, or astring/blob'smaxlen— compared by the call it is handed to.toString()A short description naming which rule this states, for diagnostics.
-
Field Details
-
SCHEMA_BOUNDED
The schema bounds thisstringorbloband the caller has already enforced itsmaxlenat the same header, asSofab.invalid(String)(MESSAGE_SPEC §7.1), so §6.2.1 forbids a receiver cap on it and there is no second number forPayloadAccto 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
countitself when handedschema(long); everySeqcall refuses this value asSofabError.ARGUMENTrather than growing a list uncompared. Its-1is what makes that refusal free: every index is>= -1, so the one comparisonSeq.checkIndex(int, Bound)makes always sends it to the out-of-line path.
-
-
Constructor Details
-
Bound
Validates a bound however it is made: the rule must be stated, a stated number is at least1, and onlyBound.Rule.CALLER_CHECKEDcarries none (-1).- Throws:
IllegalArgumentException- for any other combination; it refinesSofabError.ARGUMENT(§6.3)
-
-
Method Details
-
schema
The bound the schema declares for this field — an array'scount, or astring/blob'smaxlen— compared by the call it is handed to. A value past it isSofabError.INVALID_MSG(MESSAGE_SPEC §7.1), neverSofabError.LIMIT_EXCEEDED: the bytes contradict the schema, and no receiver configuration makes them decode.A
countis a capacity, not a length: the destination starts empty and the wire carries the length, so an element indexidbreaches it whenid >= count. Amaxlenbounds a byte length, which breaches it whenlength > 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'scountormaxlen, at least1— what the schema validator admits- Returns:
- the bound to hand to the call that guards the field
- Throws:
IllegalArgumentException- ifnis below1; it refinesSofabError.ARGUMENT(§6.3).0is an unassigned field and a negative is the retired sentinel, neither of them a schema bound
-
receiver
The receiver cap this deployment configured for a field the schema leaves unbounded — one of the threemax_dyn_*limits of §6.2.1. A value at or below the cap passes; above it the call raisesSofabError.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:
0is what Java writes into an unassignedlongorintfield, 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 astringorblob, an element count for an array — at least1- Returns:
- the bound to hand to the call that guards the field
- Throws:
IllegalArgumentException- ifmaxis below1; it refinesSofabError.ARGUMENT(§6.3), and is raised where the value is stated rather than on the decode path
-
toString
-
hashCode
-
equals
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 withObjects::equals(Object,Object); primitive components are compared with thecomparemethod from their corresponding wrapper classes. -
max
-
rule
-