Class PatchCommandEncoder
The historical Egothor patch language is defined for backward traversal, that is, from the logical end of a word toward its beginning. This implementation preserves that proven opcode semantics as the single internal representation. Forward traversal is implemented by translating source and target words to the equivalent reversed logical form at the API boundary and then delegating to the same backward encoder and decoder.
This design keeps the patch language stable, avoids maintaining two distinct opcode interpreters, and guarantees that forward traversal is semantically equivalent to running the historical algorithm on the reversed logical word form.
The encoder computes a minimum-cost edit script using weighted insert, delete, replace, and match transitions. The resulting trace is then serialized into the compact patch language.
This class is stateful and reuses internal dynamic-programming matrices
across invocations to reduce allocation pressure during repeated use.
Instances are therefore not suitable for unsynchronized concurrent access.
The encode(String, String) method is synchronized so that a shared
instance can still be used safely when needed.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classFluent builder for creating direction-specializedPatchCommandEncoderinstances. -
Method Summary
Modifier and TypeMethodDescriptionstatic StringApplies a compact patch command to the supplied source word using the historical backward traversal direction.static Stringapply(String source, String patchCommand, WordTraversalDirection traversalDirection) Applies a compact patch command to the supplied source word using the specified traversal direction.applyWithConfiguredDirection(String source, String patchCommand) Applies a compact patch command using this encoder instance traversal direction.static PatchCommandEncoder.Builderbuilder()Creates a fluent builder for constructing a direction-specialized encoder.Produces a compact patch command that transformssourceintotarget.
-
Method Details
-
builder
Creates a fluent builder for constructing a direction-specialized encoder.- Returns:
- new builder instance
-
encode
Produces a compact patch command that transformssourceintotarget.- Parameters:
source- source word formtarget- target word form- Returns:
- compact patch command, or
nullwhen any argument isnull
-
applyWithConfiguredDirection
Applies a compact patch command using this encoder instance traversal direction.This is the branch-free instance-level fast path for repeated patch application in a known traversal direction.
- Parameters:
source- original source wordpatchCommand- compact patch command- Returns:
- transformed word, or
nullwhensourceisnull
-
apply
Applies a compact patch command to the supplied source word using the historical backward traversal direction.- Parameters:
source- original source wordpatchCommand- compact patch command- Returns:
- transformed word, or
nullwhensourceisnull
-
apply
public static String apply(String source, String patchCommand, WordTraversalDirection traversalDirection) Applies a compact patch command to the supplied source word using the specified traversal direction.The implementation uses dedicated direction-specific patch decoders.
- Parameters:
source- original source wordpatchCommand- compact patch commandtraversalDirection- traversal direction used by the patch command- Returns:
- transformed word, or
nullwhensourceisnull
-