Class VersionedCodec.Builder<T>

java.lang.Object
org.jwcarman.codec.versioned.VersionedCodec.Builder<T>
Type Parameters:
T - the type the codec converts
Enclosing class:
VersionedCodec

public static final class VersionedCodec.Builder<T> extends Object
Collects the per-version codecs and the version to write, and validates them.
  • Method Details

    • version

      public VersionedCodec.Builder<T> version(int version, Codec<T> codec)
      Registers the codec that reads (and, if named by writing(int), writes) a version.
      Parameters:
      version - the format version, 1-255; 0 is reserved
      codec - the codec that produced and can read that version's payloads
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if version is outside 1-255
      IllegalStateException - if version is already registered
      NullPointerException - if codec is null
    • writing

      public VersionedCodec.Builder<T> writing(int version)
      Names the version Codec.encode(T) writes. Required. A later call replaces an earlier one — there is exactly one write version, and the last call wins.
      Parameters:
      version - the format version to write, 1-255
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if version is outside 1-255
    • build

      public Codec<T> build()
      Builds the codec.
      Returns:
      an immutable versioned codec
      Throws:
      IllegalStateException - if no version was registered, if writing(int) was never called, or if it names a version with no registered codec