Top Description Fields Constructors Methods
org.apache.commons.text

public final Class AlphabetConverter

extends Object
Class Inheritance
Imports
java.io.UnsupportedEncodingException, java.util.Arrays, .Collection, .Collections, .HashMap, .Iterator, .LinkedHashMap, .LinkedHashSet, .Map, .Map.Entry, .Objects, .Set, org.apache.commons.lang3.ArrayUtils, .StringUtils

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

Modifier and TypeField and Description
private static final String
ARROW

Arrow constant, used for converting the object into a string.

private final int
encodedLetterLength

Length of the encoded letter.

private final Map<String, String>
encodedToOriginal

Encoding alphabet.

private final Map<Integer, String>
originalToEncoded

Original string to be encoded.

Constructor Summary

AccessConstructor and Description
private
AlphabetConverter(final Map<Integer, String>
original string to be encoded.
originalToEncoded
,
final Map<String, String>
encoding alphabet.
encodedToOriginal
,
final int
length of the encoded letter.
encodedLetterLength
)

Hidden constructor for alphabet converter.

Method Summary

Modifier and TypeMethod and Description
private void
addSingleEncoding(final int
at which point it should add a single encoding.
level
,
final String
current encoding.
currentEncoding
,
final Collection<Integer>
letters encoding.
encoding
,
final Iterator<Integer>
original values.
originals
,
final Map<Integer, String>
map of values that should not be encoded.
doNotEncodeMap
)

Recursive method used when creating encoder/decoder.

private static String

Returns:

a new string with the new code point.
codePointToString
(final int
code point.
i
)

Creates new String that contains just the given code point.

private static Integer[]

Returns:

an equivalent array of integers.
convertCharsToIntegers
(final Character[]
array of characters.
chars
)

Converts characters to integers.

public static AlphabetConverter

Returns:

The AlphabetConverter.
createConverter
(final Integer[]
an array of ints representing the original alphabet in code points.
original
,
final Integer[]
an array of ints representing the alphabet to be used for encoding, in code points.
encoding
,
final Integer[]
an array of ints representing the chars to be encoded using the original alphabet - every char here must appear in both the previous params.
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).

public static AlphabetConverter

Returns:

The AlphabetConverter
createConverterFromChars
(final Character[]
an array of chars representing the original alphabet
original
,
final Character[]
an array of chars representing the alphabet to be used for encoding
encoding
,
final Character[]
an array of chars to be encoded using the original alphabet - every char here must appear in both the previous params
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).

public static AlphabetConverter

Returns:

The reconstructed AlphabetConverter.
createConverterFromMap
(final Map<Integer, String>
a map returned from getOriginalToEncoded().
originalToEncoded
)

Creates a new converter from a map.

public String

Returns:

The decoded string, null if the given string is null.
decode
(final String
a string that has been encoded using this AlphabetConverter.
encoded
)

Decodes a given string.

public String

Returns:

The encoded string, null if the given string is null.
encode
(final String
the string to be encoded.
original
)

Encodes a given string.

public boolean
equals(final Object obj)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

public int

Returns:

The length of the encoded char.
getEncodedCharLength
()

Gets the length of characters in the encoded alphabet that are necessary for each character in the original alphabet.

public Map<Integer, String>

Returns:

The original map.
getOriginalToEncoded
()

Gets the mapping from integer code point of source language to encoded string.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

ARROWback to summary
private static final String ARROW

Arrow constant, used for converting the object into a string.

encodedLetterLengthback to summary
private final int encodedLetterLength

Length of the encoded letter.

encodedToOriginalback to summary
private final Map<String, String> encodedToOriginal

Encoding alphabet.

originalToEncodedback to summary
private final Map<Integer, String> originalToEncoded

Original string to be encoded.

Constructor Detail

AlphabetConverterback to summary
private AlphabetConverter(final Map<Integer, String> originalToEncoded, final Map<String, String> encodedToOriginal, final int encodedLetterLength)

Hidden constructor for alphabet converter. Used by static helper methods.

Parameters
originalToEncoded:Map<Integer, String>

original string to be encoded.

encodedToOriginal:Map<String, String>

encoding alphabet.

encodedLetterLength:int

length of the encoded letter.

Method Detail

addSingleEncodingback to summary
private void addSingleEncoding(final int level, final String currentEncoding, final Collection<Integer> encoding, final Iterator<Integer> originals, final Map<Integer, String> doNotEncodeMap)

Recursive method used when creating encoder/decoder.

Parameters
level:int

at which point it should add a single encoding.

currentEncoding:String

current encoding.

encoding:Collection<Integer>

letters encoding.

originals:Iterator<Integer>

original values.

doNotEncodeMap:Map<Integer, String>

map of values that should not be encoded.

codePointToStringback to summary
private static String codePointToString(final int i)

Creates new String that contains just the given code point.

Parameters
i:int

code point.

Returns:String

a new string with the new code point.

See Also
http://www.oracle.com/us/technologies/java/supplementary-142654.html
convertCharsToIntegersback to summary
private static Integer[] convertCharsToIntegers(final Character[] chars)

Converts characters to integers.

Parameters
chars:Character[]

array of characters.

Returns:Integer[]

an equivalent array of integers.

createConverterback to summary
public static AlphabetConverter createConverter(final Integer[] original, final Integer[] encoding, final 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:Integer[]

an array of ints representing the original alphabet in code points.

encoding:Integer[]

an array of ints representing the alphabet to be used for encoding, in code points.

doNotEncode:Integer[]

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:AlphabetConverter

The AlphabetConverter.

Exceptions
IllegalArgumentException:
if an AlphabetConverter cannot be constructed.
createConverterFromCharsback to summary
public static AlphabetConverter createConverterFromChars(final Character[] original, final Character[] encoding, final 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:Character[]

an array of chars representing the original alphabet

encoding:Character[]

an array of chars representing the alphabet to be used for encoding

doNotEncode:Character[]

an array of chars to be encoded using the original alphabet - every char here must appear in both the previous params

Returns:AlphabetConverter

The AlphabetConverter

Exceptions
IllegalArgumentException:
if an AlphabetConverter cannot be constructed
createConverterFromMapback to summary
public static AlphabetConverter createConverterFromMap(final Map<Integer, String> originalToEncoded)

Creates a new converter from a map.

Parameters
originalToEncoded:Map<Integer, String>

a map returned from getOriginalToEncoded().

Returns:AlphabetConverter

The reconstructed AlphabetConverter.

See Also
AlphabetConverter#getOriginalToEncoded()
decodeback to summary
public String decode(final String encoded) throws UnsupportedEncodingException

Decodes a given string.

Parameters
encoded:String

a string that has been encoded using this AlphabetConverter.

Returns:String

The decoded string, null if the given string is null.

Exceptions
UnsupportedEncodingException:
if unexpected characters that cannot be handled are encountered.
encodeback to summary
public String encode(final String original) throws UnsupportedEncodingException

Encodes a given string.

Parameters
original:String

the string to be encoded.

Returns:String

The encoded string, null if the given string is null.

Exceptions
UnsupportedEncodingException:
if chars that are not supported are encountered.
equalsback to summary
public boolean equals(final Object obj)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Annotations
@Override
getEncodedCharLengthback to summary
public int getEncodedCharLength()

Gets the length of characters in the encoded alphabet that are necessary for each character in the original alphabet.

Returns:int

The length of the encoded char.

getOriginalToEncodedback to summary
public Map<Integer, String> getOriginalToEncoded()

Gets the mapping from integer code point of source language to encoded string. Use to reconstruct converter from serialized map.

Returns:Map<Integer, String>

The original map.

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Annotations
@Override
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Annotations
@Override