Class Radix62IdConverter

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

public final class Radix62IdConverter extends RadixIdConverter
62-bit radix string ID converter that converts long IDs to compact string representations.

This converter uses a radix-62 character set consisting of:

  • Digits: 0-9 (10 characters)
  • Uppercase letters: A-Z (26 characters)
  • Lowercase letters: a-z (26 characters)

The resulting string IDs are more compact than decimal representations, using only 11 characters to represent the full range of long values.

Important: If you use the string IDs generated by this converter as database primary keys, you must configure the database to treat the primary key column as case-sensitive, since the character set includes both uppercase and lowercase letters.

Example conversions:

  • 12345 → "3d7"
  • 9876543210L → "1Zi6jE"
  • Field Details

    • MAX_CHAR_SIZE

      public static final int MAX_CHAR_SIZE
      The maximum character size for radix-62 encoded IDs.

      This is the maximum number of characters needed to represent any long value in radix-62 format, which is 11 characters.

      See Also:
    • RADIX

      public static final int RADIX
      The radix (base) used for conversion: 62 characters.

      This represents the size of the character set used for encoding: 10 digits + 26 uppercase letters + 26 lowercase letters = 62 characters.

      See Also:
    • INSTANCE

      public static final Radix62IdConverter INSTANCE
      A shared instance without padding.

      This instance converts IDs without padding the result to a fixed length, producing variable-length string IDs.

    • PAD_START

      public static final Radix62IdConverter PAD_START
      A shared instance with padding.

      This instance converts IDs with padding to ensure all string IDs have the same length (11 characters), which can be useful for sorting and fixed-width storage requirements.

  • Constructor Details

    • Radix62IdConverter

      public Radix62IdConverter(boolean padStart, int charSize)
      Create a new Radix62IdConverter with the specified configuration.

      This constructor creates a converter with custom padding and character size settings. For most use cases, consider using the of(boolean, int) factory method which may return shared instances for better efficiency.

      Parameters:
      padStart - Whether to pad the result with leading zeros
      charSize - The character size for padding (if enabled)
  • Method Details

    • of

      public static Radix62IdConverter of(boolean padStart, int charSize)
      Get a Radix62IdConverter instance with the specified parameters.

      This factory method returns an appropriate converter instance based on the requested parameters. For common configurations, it returns shared cached instances to improve performance and reduce memory usage.

      Parameters:
      padStart - Whether to pad the result with leading zeros
      charSize - The character size for padding (if enabled)
      Returns:
      A Radix62IdConverter instance with the specified configuration