Package org.apache.commons.text
Class AlphabetConverter
- java.lang.Object
-
- org.apache.commons.text.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
-
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.String
ARROW
Arrow constant, used for converting the object into a string.private int
encodedLetterLength
Length of the encoded letter.private java.util.Map<java.lang.String,java.lang.String>
encodedToOriginal
Encoding alphabet.private java.util.Map<java.lang.Integer,java.lang.String>
originalToEncoded
Original string to be encoded.
-
Constructor Summary
Constructors Modifier Constructor Description private
AlphabetConverter(java.util.Map<java.lang.Integer,java.lang.String> originalToEncoded, java.util.Map<java.lang.String,java.lang.String> encodedToOriginal, int encodedLetterLength)
Hidden constructor for alphabet converter.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
addSingleEncoding(int level, java.lang.String currentEncoding, java.util.Collection<java.lang.Integer> encoding, java.util.Iterator<java.lang.Integer> originals, java.util.Map<java.lang.Integer,java.lang.String> doNotEncodeMap)
Recursive method used when creating encoder/decoder.private static java.lang.String
codePointToString(int i)
Creates new String that contains just the given code point.private static java.lang.Integer[]
convertCharsToIntegers(java.lang.Character[] chars)
Converts characters to integers.static AlphabetConverter
createConverter(java.lang.Integer[] original, java.lang.Integer[] encoding, java.lang.Integer[] doNotEncode)
Creates 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)
Creates 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)
Creates a new converter from a map.java.lang.String
decode(java.lang.String encoded)
Decodes a given string.java.lang.String
encode(java.lang.String original)
Encodes a given string.boolean
equals(java.lang.Object obj)
int
getEncodedCharLength()
Gets 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()
Gets the mapping from integer code point of source language to encoded string.int
hashCode()
java.lang.String
toString()
-
-
-
Field Detail
-
ARROW
private static final java.lang.String ARROW
Arrow constant, used for converting the object into a string.- See Also:
- Constant Field Values
-
originalToEncoded
private final java.util.Map<java.lang.Integer,java.lang.String> originalToEncoded
Original string to be encoded.
-
encodedToOriginal
private final java.util.Map<java.lang.String,java.lang.String> encodedToOriginal
Encoding alphabet.
-
encodedLetterLength
private final int encodedLetterLength
Length of the encoded letter.
-
-
Constructor Detail
-
AlphabetConverter
private AlphabetConverter(java.util.Map<java.lang.Integer,java.lang.String> originalToEncoded, java.util.Map<java.lang.String,java.lang.String> encodedToOriginal, int encodedLetterLength)
Hidden constructor for alphabet converter. Used by static helper methods.- Parameters:
originalToEncoded
- original string to be encodedencodedToOriginal
- encoding alphabetencodedLetterLength
- length of the encoded letter
-
-
Method Detail
-
codePointToString
private static java.lang.String codePointToString(int i)
Creates new String that contains just the given code point.- Parameters:
i
- code point- Returns:
- a new string with the new code point
- See Also:
- "http://www.oracle.com/us/technologies/java/supplementary-142654.html"
-
convertCharsToIntegers
private static java.lang.Integer[] convertCharsToIntegers(java.lang.Character[] chars)
Converts characters to integers.- Parameters:
chars
- array of characters- Returns:
- an equivalent array of integers
-
createConverter
public static AlphabetConverter createConverter(java.lang.Integer[] original, java.lang.Integer[] encoding, java.lang.Integer[] doNotEncode)
Creates 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 code pointsencoding
- an array of ints representing the alphabet to be used for encoding, in code pointsdoNotEncode
- 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
-
createConverterFromChars
public static AlphabetConverter createConverterFromChars(java.lang.Character[] original, java.lang.Character[] encoding, java.lang.Character[] doNotEncode)
Creates 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 alphabetencoding
- an array of chars representing the alphabet to be used for encodingdoNotEncode
- 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
-
createConverterFromMap
public static AlphabetConverter createConverterFromMap(java.util.Map<java.lang.Integer,java.lang.String> originalToEncoded)
Creates a new converter from a map.- Parameters:
originalToEncoded
- a map returned from getOriginalToEncoded()- Returns:
- The reconstructed AlphabetConverter
- See Also:
getOriginalToEncoded()
-
addSingleEncoding
private void addSingleEncoding(int level, java.lang.String currentEncoding, java.util.Collection<java.lang.Integer> encoding, java.util.Iterator<java.lang.Integer> originals, java.util.Map<java.lang.Integer,java.lang.String> doNotEncodeMap)
Recursive method used when creating encoder/decoder.- Parameters:
level
- at which point it should add a single encodingcurrentEncoding
- current encodingencoding
- letters encodingoriginals
- original valuesdoNotEncodeMap
- map of values that should not be encoded
-
decode
public java.lang.String decode(java.lang.String encoded) throws java.io.UnsupportedEncodingException
Decodes 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
-
encode
public java.lang.String encode(java.lang.String original) throws java.io.UnsupportedEncodingException
Encodes 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
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equals
in classjava.lang.Object
-
getEncodedCharLength
public int getEncodedCharLength()
Gets 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()
Gets the mapping from integer code point of source language to encoded string. Use to reconstruct converter from serialized map.- Returns:
- The original map
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-