A practical guide to transforming values during decode/encode.
Formulas let you convert between raw on‑wire values and engineering values. You can express them as inline lambdas or as classes implementing java.util.function.Function.
Note: Lambda formulas require annotation processing for compile-time code generation. The
fastprotobundle includes this by default. For Android projects, see Android guide for setup details.
@UInt32Type).Function<RawType, FieldType>
@UInt32Type raw is Long; field may be Double (engineering).Function<FieldType, RawType>
@DecodingFormula(lambda = "...") / @EncodingFormula(lambda = "...") — included in fastproto bundle@DecodingFormula(MyFunc.class) / @EncodingFormula(MyFunc.class) where class implements Function<In, Out>Lambda expressions are supported out of the box when using the fastproto bundle:
<dependency>
<groupId>org.indunet</groupId>
<artifactId>fastproto</artifactId>
<version>4.1.0</version>
</dependency>For Android projects, see android.md for separate configuration.
DecodingException / EncodingException at call sites.import org.indunet.fastproto.annotation.*;
public class Weather {
@UInt32Type(offset = 14)
@DecodingFormula(lambda = "x -> x * 0.1") // raw uint32 -> Pa * 0.1 -> engineering
@EncodingFormula(lambda = "x -> (long) (x * 10)") // engineering -> raw
double pressure;
}import java.util.function.Function;
public class PressureDecode implements Function<Long, Double> {
@Override public Double apply(Long raw) { return raw * 0.1; }
}
public class PressureEncode implements Function<Double, Long> {
@Override public Long apply(Double eng) { return (long) (eng * 10); }
}public class Weather {
@UInt32Type(offset = 14)
@DecodingFormula(PressureDecode.class)
@EncodingFormula(PressureEncode.class)
double pressure;
}@DecodingFormula.Function classes and delegate.UInt32 raw is Long).