Skip to content

io7m-com/jserial

Repository files navigation

jserial

Maven Central Maven Central (snapshot) Codecov Java Version

com.io7m.jserial

JVM Platform Status
OpenJDK (Temurin) Current Linux Build (OpenJDK (Temurin) Current, Linux)
OpenJDK (Temurin) LTS Linux Build (OpenJDK (Temurin) LTS, Linux)
OpenJDK (Temurin) Current Windows Build (OpenJDK (Temurin) Current, Windows)
OpenJDK (Temurin) LTS Windows Build (OpenJDK (Temurin) LTS, Windows)

jserial

A Java implementation of enhanced serial number arithmetic.

Features

  • 8-bit serial number arithmetic
  • 16-bit serial number arithmetic
  • 24-bit serial number arithmetic
  • 32-bit serial number arithmetic
  • 40-bit serial number arithmetic
  • 48-bit serial number arithmetic
  • 56-bit serial number arithmetic
  • 62-bit serial number arithmetic
  • High coverage test suite.
  • OSGi-ready
  • JPMS-ready
  • ISC license.

Motivation

Many communications protocols use fixed-sized fields to store sequence numbers. Unfortunately, given that the fields are of a fixed size, it is inevitable that the sequence numbers will eventually reach the maximum possible value that can be stored in fields of that size and, using unsigned arithmetic, will then wrap around to 0. This can cause serious problems for systems tracking sequence numbers as the numbers are no longer monotonically increasing.

Using serial number arithmetic, it's possible to have sequence numbers that can wrap around indefinitely, but that the application can still treat as being conceptually monotonically increasing.

Usage

Choose an implementation based on the number of bits used to store serial numbers in your intended protocol. Most protocols will want to use 32 bits:

var s = SerialNumber32.get();

Given a starting number x, get the next number in the sequence according to serial number arithmetic at the specified implementation size:

var y = s.add(x, 1L);

The SerialNumberLongType and SerialNumberIntType interfaces expose numerous methods to increment and compare numbers. In the above code, the value y can be compared with x, and the compare implementation will indicate that y is greater than x, even if y has wrapped around to zero.