V - a constraint on the types of the values in the mappublic class AbbreviationMap<V> extends java.lang.Object implements OptionNameMap<V>
A map whose keys are strings; when a key/value pair is added to the map, the longest unique abbreviations of that key are added as well, and associated with the value. Thus:
abbreviations.put( "good", "bye" );
would make it such that you could retrieve the value "bye" from the map using the keys "good",
"goo", "go", and "g". A subsequent invocation of:
abbreviations.put( "go", "fish" );
would make it such that you could retrieve the value "bye" using the keys "good" and
"goo", and the value "fish" using the key "go". The key "g" would yield
null, since it would no longer be a unique abbreviation.
The data structure is much like a "trie".
| Modifier and Type | Field and Description |
|---|---|
private java.util.Map<java.lang.Character,AbbreviationMap<V>> |
children |
private java.lang.String |
key |
private int |
keysBeyond |
private V |
value |
| Constructor and Description |
|---|
AbbreviationMap() |
| Modifier and Type | Method and Description |
|---|---|
private boolean |
add(char[] chars,
V newValue,
int offset,
int length) |
private void |
addToMappings(java.util.Map<java.lang.String,V> mappings) |
private static char[] |
charsOf(java.lang.String aKey) |
boolean |
contains(java.lang.String key)
Tells whether the given key is in the map, or whether the given key is a unique
abbreviation of a key that is in the map.
|
V |
get(java.lang.String key)
Answers the value associated with the given key.
|
void |
put(java.lang.String key,
V newValue)
Associates a given value with a given key.
|
void |
putAll(java.lang.Iterable<java.lang.String> keys,
V newValue)
Associates a given value with a given set of keys.
|
private boolean |
remove(char[] aKey,
int offset,
int length) |
void |
remove(java.lang.String key)
If the map contains the given key, dissociates the key from its value.
|
private boolean |
removeAtEndOfKey() |
private void |
setValueToThatOfOnlyChild() |
java.util.Map<java.lang.String,V> |
toJavaUtilMap()
Gives a Java map representation of this abbreviation map.
|
private final java.util.Map<java.lang.Character,AbbreviationMap<V>> children
private java.lang.String key
private V value
private int keysBeyond
public boolean contains(java.lang.String key)
Tells whether the given key is in the map, or whether the given key is a unique abbreviation of a key that is in the map.
contains in interface OptionNameMap<V>key - key to look uptrue if key is present in the mapjava.lang.NullPointerException - if key is nullpublic V get(java.lang.String key)
Answers the value associated with the given key. The key can be a unique abbreviation of a key that is in the map.
get in interface OptionNameMap<V>key - key to look upaKey; or null if there is no
such value or aKey is not a unique abbreviation of a key in the mapjava.lang.NullPointerException - if aKey is nullpublic void put(java.lang.String key,
V newValue)
Associates a given value with a given key. If there was a previous association, the old value is replaced with the new one.
put in interface OptionNameMap<V>key - key to create in the mapnewValue - value to associate with the keyjava.lang.NullPointerException - if aKey or newValue is nulljava.lang.IllegalArgumentException - if aKey is a zero-length stringpublic void putAll(java.lang.Iterable<java.lang.String> keys,
V newValue)
Associates a given value with a given set of keys. If there was a previous association, the old value is replaced with the new one.
putAll in interface OptionNameMap<V>keys - keys to create in the mapnewValue - value to associate with the keyjava.lang.NullPointerException - if keys or newValue is nulljava.lang.IllegalArgumentException - if any of keys is a zero-length stringprivate boolean add(char[] chars,
V newValue,
int offset,
int length)
public void remove(java.lang.String key)
If the map contains the given key, dissociates the key from its value.
remove in interface OptionNameMap<V>key - key to removejava.lang.NullPointerException - if aKey is nulljava.lang.IllegalArgumentException - if aKey is a zero-length stringprivate boolean remove(char[] aKey,
int offset,
int length)
private void setValueToThatOfOnlyChild()
private boolean removeAtEndOfKey()
public java.util.Map<java.lang.String,V> toJavaUtilMap()
toJavaUtilMap in interface OptionNameMap<V>private void addToMappings(java.util.Map<java.lang.String,V> mappings)
private static char[] charsOf(java.lang.String aKey)