Top Description Inners Fields Constructors Methods
com.ctc.wstx.util

public final Class WordResolver

extends Object
Class Inheritance
Imports
java.util.*

A specialized Map/Symbol table - like data structure that can be used for both checking whether a word (passed in as a char array) exists in certain set of words AND getting that word as a String. It is reasonably efficient both time and speed-wise, at least for certain use cases; specifically, if there is no existing key to use, it is more efficient way to get to a shared copy of that String The general usage pattern is expected to be such that most checks are positive, ie. that the word indeed is contained in the structure.

Although this is an efficient data struct for specific set of usage patterns, one restriction is that the full set of words to include has to be known before constructing the instnace. Also, the size of the set is limited to total word content of about 20k characters.

Todo

Should document the internal data structure...

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
pack-priv static final char
public static final int
MAX_WORDS

Maximum number of words (Strings) an instance can contain

pack-priv final char[]
mData

Compressed presentation of the word set.

pack-priv static final int
MIN_BINARY_SEARCH

This is actually just a guess; but in general linear search should be faster for short sequences (definitely for 4 or less; maybe up to 8 or less?)

pack-priv final String[]
mWords

Array of actual words returned resolved for matches.

pack-priv static final int
NEGATIVE_OFFSET

Offset added to numbers to mark 'negative' numbers.

Constructor Summary

AccessConstructor and Description
pack-priv
WordResolver(String[] words, char[] index)

Method Summary

Modifier and TypeMethod and Description
public static WordResolver

Returns:

WordResolver constructed for given set of words, if the word set size is not too big; null to indicate "too big" instance.
constructInstance
(TreeSet<String> wordSet)

Tries to construct an instance given ordered set of words.

public String

Returns:

(Shared) string instance of the word, if it exists in the word set; null if not.
find
(char[]
Character array that contains the word to find
str
,
final int
Index of the first character of the word
start
,
final int
Index following the last character of the word, so that end - start equals word length (similar to the way String.substring() has).
end
)

public String

Returns:

(Shared) string instance of the word, if it exists in the word set; null if not.
find
(String str)

private String
findFromOne(char[] str, final int start, final int end)

public int

Returns:

Number of words contained
size
()

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

CHAR_NULLback to summary
pack-priv static final char CHAR_NULL
MAX_WORDSback to summary
public static final int MAX_WORDS

Maximum number of words (Strings) an instance can contain

mDataback to summary
pack-priv final char[] mData

Compressed presentation of the word set.

mWordsback to summary
pack-priv final String[] mWords

Array of actual words returned resolved for matches.

NEGATIVE_OFFSETback to summary
pack-priv static final int NEGATIVE_OFFSET

Offset added to numbers to mark 'negative' numbers. Asymmetric, since range of negative markers needed is smaller than positive numbers...

Constructor Detail

WordResolverback to summary
pack-priv WordResolver(String[] words, char[] index)

Method Detail

constructInstanceback to summary
public static WordResolver constructInstance(TreeSet<String> wordSet)

Tries to construct an instance given ordered set of words.

Note

currently maximum number of words that can be contained is limited to MAX_WORDS; additionally, maximum length of all such words can not exceed roughly 28000 characters.

Returns:WordResolver

WordResolver constructed for given set of words, if the word set size is not too big; null to indicate "too big" instance.

findback to summary
public String find(char[] str, final int start, final int end)
Parameters
str:char[]

Character array that contains the word to find

start:int

Index of the first character of the word

end:int

Index following the last character of the word, so that end - start equals word length (similar to the way String.substring() has).

Returns:String

(Shared) string instance of the word, if it exists in the word set; null if not.

Annotations
@SuppressWarnings:cast
findback to summary
public String find(String str)
Returns:String

(Shared) string instance of the word, if it exists in the word set; null if not.

Annotations
@SuppressWarnings:cast
findFromOneback to summary
private String findFromOne(char[] str, final int start, final int end)
sizeback to summary
public int size()
Returns:int

Number of words contained

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.

Returns:String

a string representation of the object

Annotations
@Override
com.ctc.wstx.util back to summary

private final Class WordResolver.Builder

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv char[]
pack-priv int
mSize

Number of characters currently used from mData

pack-priv final String[]

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public WordResolver

Returns:

Raw character data that contains compressed structure of the word set
construct
()

private void
constructBranch(int
Index of the character in words to consider for this round
charIndex
,
int
Index of the first word to be processed
start
,
int
Index of the word after last word to be processed (so that number of words is end - start - 1
end
)

Method that is called recursively to build the data representation for a branch, ie.

private char[]
expand(int needSpace)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

mDataback to summary
pack-priv char[] mData
mSizeback to summary
pack-priv int mSize

Number of characters currently used from mData

mWordsback to summary
pack-priv final String[] mWords

Constructor Detail

Builderback to summary
public Builder(TreeSet<String> wordSet)

Method Detail

constructback to summary
public WordResolver construct()
Returns:WordResolver

Raw character data that contains compressed structure of the word set

constructBranchback to summary
private void constructBranch(int charIndex, int start, int end)

Method that is called recursively to build the data representation for a branch, ie. part of word set tree that still has more than one ending

Parameters
charIndex:int

Index of the character in words to consider for this round

start:int

Index of the first word to be processed

end:int

Index of the word after last word to be processed (so that number of words is end - start - 1

Annotations
@SuppressWarnings:cast
expandback to summary
private char[] expand(int needSpace)