Class Radix

java.lang.Object
me.ahoo.cosid.codec.Radix
All Implemented Interfaces:
IdCodec

public final class Radix extends Object implements IdCodec
Encodes non-negative ids in base 2 to 62 with the digits 0-9A-Za-z; replaces the 4.x ToStringIdConverter, Radix36IdConverter and Radix62IdConverter (03-core-model.md ยง1).
  • Field Details

  • Method Details

    • of

      public static Radix of(int radix, int width, boolean padStart)
      A radix codec.
      Parameters:
      radix - the base, from 2 to 62
      width - the most digits an id takes, from 1 to maxWidth(int)
      padStart - whether shorter ids are padded with leading zeros to width
      Returns:
      the codec
    • maxWidth

      public static int maxWidth(int radix)
      The digits Long.MAX_VALUE takes in radix: the widest a radix codec can be.
      Parameters:
      radix - the base
      Returns:
      the number of digits
    • radix

      public int radix()
    • width

      public int width()
    • padStart

      public boolean padStart()
    • maxValue

      public long maxValue()
      The largest id that fits width() digits, computed with integer arithmetic.
      Returns:
      the largest encodable id
    • encode

      public String encode(long id)
      Description copied from interface: IdCodec
      Encodes an id (R1-core-16).
      Specified by:
      encode in interface IdCodec
      Parameters:
      id - the id
      Returns:
      the text, never null
    • decode

      public long decode(String text)
      Description copied from interface: IdCodec
      Decodes text produced by IdCodec.encode(long).
      Specified by:
      decode in interface IdCodec
      Parameters:
      text - the text
      Returns:
      the id