Class RadixIdConverter

java.lang.Object
me.ahoo.cosid.converter.RadixIdConverter
All Implemented Interfaces:
IdConverter, Statistical
Direct Known Subclasses:
Radix36IdConverter, Radix62IdConverter

public abstract class RadixIdConverter extends Object implements IdConverter
Abstract base for radix-based ID converters.

Converts between long IDs and string representations using various radixes (base 36, 62, etc.). Supports optional zero-padding for consistent string length.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final char
    Padding character for fixed-width output.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    RadixIdConverter(boolean padStart, int charSize)
    Creates a new RadixIdConverter.
  • Method Summary

    Modifier and Type
    Method
    Description
    long
    asLong(@NonNull String idString)
    Convert a String type ID back to its long representation.
    @NonNull String
    asString(long id)
    Convert a long type ID to its String representation.
    int
    Gets the fixed character size.
    long
    Gets the maximum ID representable with this char size.
    static int
    maxCharSize(int radix, int bits)
    Calculates maximum character size for a given radix and bit count.
    static int
    offset(char digitChar)
    Gets the offset value for a digit character.
    Get statistical information about this converter.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • PAD_CHAR

      public static final char PAD_CHAR
      Padding character for fixed-width output.
      See Also:
  • Constructor Details

    • RadixIdConverter

      protected RadixIdConverter(boolean padStart, int charSize)
      Creates a new RadixIdConverter.
      Parameters:
      padStart - whether to pad output with leading zeros
      charSize - the fixed character size for output
  • Method Details

    • offset

      public static int offset(char digitChar)
      Gets the offset value for a digit character.
      Parameters:
      digitChar - the character to convert
      Returns:
      the offset value (0-61) or -1 if invalid
    • maxCharSize

      public static int maxCharSize(int radix, int bits)
      Calculates maximum character size for a given radix and bit count.
      Parameters:
      radix - the number base
      bits - number of bits
      Returns:
      maximum characters needed
    • getCharSize

      public int getCharSize()
      Gets the fixed character size.
      Returns:
      character size
    • getMaxId

      public long getMaxId()
      Gets the maximum ID representable with this char size.
      Returns:
      maximum ID
    • asString

      public @NonNull String asString(long id)
      Description copied from interface: IdConverter
      Convert a long type ID to its String representation.

      This method transforms a numeric ID into a string format according to the specific conversion algorithm implemented by the converter. For example, a radix converter might convert the number 12345 to "3d7".

      Specified by:
      asString in interface IdConverter
      Parameters:
      id - The long type ID to convert
      Returns:
      The String representation of the ID
    • asLong

      public long asLong(@NonNull String idString)
      Description copied from interface: IdConverter
      Convert a String type ID back to its long representation.

      This method performs the reverse operation of IdConverter.asString(long), transforming a string ID back into its original numeric form. Implementations should be able to correctly parse strings generated by their IdConverter.asString(long) method.

      Specified by:
      asLong in interface IdConverter
      Parameters:
      idString - The String type ID to convert
      Returns:
      The long representation of the ID
    • stat

      public Stat stat()
      Description copied from interface: IdConverter
      Get statistical information about this converter.

      This method provides basic statistical information about the converter, including its implementation class name.

      Specified by:
      stat in interface IdConverter
      Specified by:
      stat in interface Statistical
      Returns:
      Statistical information about this converter