Class AlphabetConverter


  • public final class AlphabetConverter
    extends java.lang.Object

    Convert from one alphabet to another, with the possibility of leaving certain characters unencoded.

    The target and do not encode languages must be in the Unicode BMP, but the source language does not.

    The encoding will all be of a fixed length, except for the 'do not encode' chars, which will be of length 1

    Sample usage

     Character[] originals;   // a, b, c, d
     Character[] encoding;    // 0, 1, d
     Character[] doNotEncode; // d
    
     AlphabetConverter ac = AlphabetConverter.createConverterFromChars(originals,
     encoding, doNotEncode);
    
     ac.encode("a");    // 00
     ac.encode("b");    // 01
     ac.encode("c");    // 0d
     ac.encode("d");    // d
     ac.encode("abcd"); // 00010dd
     

    #ThreadSafe# AlphabetConverter class methods are thread-safe as they do not change internal state.

    Since:
    1.0
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static AlphabetConverter createConverter​(java.lang.Integer[] original, java.lang.Integer[] encoding, java.lang.Integer[] doNotEncode)
      Create an alphabet converter, for converting from the original alphabet, to the encoded alphabet, while leaving the characters in doNotEncode as they are (if possible).
      static AlphabetConverter createConverterFromChars​(java.lang.Character[] original, java.lang.Character[] encoding, java.lang.Character[] doNotEncode)
      Create an alphabet converter, for converting from the original alphabet, to the encoded alphabet, while leaving the characters in doNotEncode as they are (if possible).
      static AlphabetConverter createConverterFromMap​(java.util.Map<java.lang.Integer,​java.lang.String> originalToEncoded)
      Create a new converter from a map.
      java.lang.String decode​(java.lang.String encoded)
      Decode a given string.
      java.lang.String encode​(java.lang.String original)
      Encode a given string.
      boolean equals​(java.lang.Object obj)  
      int getEncodedCharLength()
      Get the length of characters in the encoded alphabet that are necessary for each character in the original alphabet.
      java.util.Map<java.lang.Integer,​java.lang.String> getOriginalToEncoded()
      Get the mapping from integer code point of source language to encoded string.
      int hashCode()  
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        getClass, notify, notifyAll, wait, wait, wait
    • Method Detail

      • encode

        public java.lang.String encode​(java.lang.String original)
                                throws java.io.UnsupportedEncodingException
        Encode a given string.
        Parameters:
        original - the string to be encoded
        Returns:
        The encoded string, null if the given string is null
        Throws:
        java.io.UnsupportedEncodingException - if chars that are not supported are encountered
      • decode

        public java.lang.String decode​(java.lang.String encoded)
                                throws java.io.UnsupportedEncodingException
        Decode a given string.
        Parameters:
        encoded - a string that has been encoded using this AlphabetConverter
        Returns:
        The decoded string, null if the given string is null
        Throws:
        java.io.UnsupportedEncodingException - if unexpected characters that cannot be handled are encountered
      • getEncodedCharLength

        public int getEncodedCharLength()
        Get the length of characters in the encoded alphabet that are necessary for each character in the original alphabet.
        Returns:
        The length of the encoded char
      • getOriginalToEncoded

        public java.util.Map<java.lang.Integer,​java.lang.String> getOriginalToEncoded()
        Get the mapping from integer code point of source language to encoded string. Use to reconstruct converter from serialized map.
        Returns:
        The original map
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • equals

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • createConverterFromMap

        public static AlphabetConverter createConverterFromMap​(java.util.Map<java.lang.Integer,​java.lang.String> originalToEncoded)
        Create a new converter from a map.
        Parameters:
        originalToEncoded - a map returned from getOriginalToEncoded()
        Returns:
        The reconstructed AlphabetConverter
        See Also:
        getOriginalToEncoded()
      • createConverterFromChars

        public static AlphabetConverter createConverterFromChars​(java.lang.Character[] original,
                                                                 java.lang.Character[] encoding,
                                                                 java.lang.Character[] doNotEncode)
        Create an alphabet converter, for converting from the original alphabet, to the encoded alphabet, while leaving the characters in doNotEncode as they are (if possible).

        Duplicate letters in either original or encoding will be ignored.

        Parameters:
        original - an array of chars representing the original alphabet
        encoding - an array of chars representing the alphabet to be used for encoding
        doNotEncode - an array of chars to be encoded using the original alphabet - every char here must appear in both the previous params
        Returns:
        The AlphabetConverter
        Throws:
        java.lang.IllegalArgumentException - if an AlphabetConverter cannot be constructed
      • createConverter

        public static AlphabetConverter createConverter​(java.lang.Integer[] original,
                                                        java.lang.Integer[] encoding,
                                                        java.lang.Integer[] doNotEncode)
        Create an alphabet converter, for converting from the original alphabet, to the encoded alphabet, while leaving the characters in doNotEncode as they are (if possible).

        Duplicate letters in either original or encoding will be ignored.

        Parameters:
        original - an array of ints representing the original alphabet in codepoints
        encoding - an array of ints representing the alphabet to be used for encoding, in codepoints
        doNotEncode - an array of ints representing the chars to be encoded using the original alphabet - every char here must appear in both the previous params
        Returns:
        The AlphabetConverter
        Throws:
        java.lang.IllegalArgumentException - if an AlphabetConverter cannot be constructed