public final class AlphabetConverter extends 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
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 threadsafe as they do not change internal state.
Modifier and Type | Method and Description |
---|---|
static AlphabetConverter |
createConverter(Integer[] original,
Integer[] encoding,
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(Character[] original,
Character[] encoding,
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(Map<Integer,String> originalToEncoded)
Create a new converter from a map.
|
String |
decode(String encoded)
Decode a given string.
|
String |
encode(String original)
Encode a given string.
|
boolean |
equals(Object obj) |
int |
getEncodedCharLength()
Get the length of characters in the encoded alphabet that are necessary for each character in the original
alphabet.
|
Map<Integer,String> |
getOriginalToEncoded()
Get the mapping from integer code point of source language to encoded string.
|
int |
hashCode() |
String |
toString() |
public String encode(String original) throws UnsupportedEncodingException
original
- the string to be encodednull
if the given string is nullUnsupportedEncodingException
- if chars that are not supported are encounteredpublic String decode(String encoded) throws UnsupportedEncodingException
encoded
- a string that has been encoded using this AlphabetConverternull
if the given string is nullUnsupportedEncodingException
- if unexpected characters that cannot be handled are encounteredpublic int getEncodedCharLength()
public Map<Integer,String> getOriginalToEncoded()
public static AlphabetConverter createConverterFromMap(Map<Integer,String> originalToEncoded)
originalToEncoded
- a map returned from getOriginalToEncoded()getOriginalToEncoded()
public static AlphabetConverter createConverterFromChars(Character[] original, Character[] encoding, Character[] doNotEncode)
Duplicate letters in either original or encoding will be ignored.
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 paramsIllegalArgumentException
- if an AlphabetConverter cannot be constructedpublic static AlphabetConverter createConverter(Integer[] original, Integer[] encoding, Integer[] doNotEncode)
Duplicate letters in either original or encoding will be ignored.
original
- an array of ints representing the original alphabet in codepointsencoding
- an array of ints representing the alphabet to be used for encoding, in codepointsdoNotEncode
- an array of ints representing the chars to be encoded using the original alphabet - every char
here must appear in both the previous paramsIllegalArgumentException
- if an AlphabetConverter cannot be constructedCopyright © 2014–2017 The Apache Software Foundation. All rights reserved.