Class AbbreviationMap<V>
- Type Parameters:
V
- a constraint on the types of the values in the map
- All Implemented Interfaces:
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".
- See Also:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate boolean
private void
addToMappings
(Map<String, V> mappings) private static char[]
boolean
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.Answers the value associated with the given key.void
Associates a given value with a given key.void
Associates a given value with a given set of keys.private boolean
remove
(char[] aKey, int offset, int length) void
If the map contains the given key, dissociates the key from its value.private boolean
private void
Gives a Java map representation of this abbreviation map.
-
Field Details
-
children
-
key
-
value
-
keysBeyond
private int keysBeyond
-
-
Constructor Details
-
AbbreviationMap
public AbbreviationMap()
-
-
Method Details
-
contains
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.
- Specified by:
contains
in interfaceOptionNameMap<V>
- Parameters:
key
- key to look up- Returns:
true
ifkey
is present in the map- Throws:
NullPointerException
- ifkey
isnull
-
get
Answers the value associated with the given key. The key can be a unique abbreviation of a key that is in the map.
- Specified by:
get
in interfaceOptionNameMap<V>
- Parameters:
key
- key to look up- Returns:
- the value associated with
aKey
; ornull
if there is no such value oraKey
is not a unique abbreviation of a key in the map - Throws:
NullPointerException
- ifaKey
isnull
-
put
Associates a given value with a given key. If there was a previous association, the old value is replaced with the new one.
- Specified by:
put
in interfaceOptionNameMap<V>
- Parameters:
key
- key to create in the mapnewValue
- value to associate with the key- Throws:
NullPointerException
- ifaKey
ornewValue
isnull
IllegalArgumentException
- ifaKey
is a zero-length string
-
putAll
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.
- Specified by:
putAll
in interfaceOptionNameMap<V>
- Parameters:
keys
- keys to create in the mapnewValue
- value to associate with the key- Throws:
NullPointerException
- ifkeys
ornewValue
isnull
IllegalArgumentException
- if any ofkeys
is a zero-length string
-
add
-
remove
If the map contains the given key, dissociates the key from its value.
- Specified by:
remove
in interfaceOptionNameMap<V>
- Parameters:
key
- key to remove- Throws:
NullPointerException
- ifaKey
isnull
IllegalArgumentException
- ifaKey
is a zero-length string
-
remove
private boolean remove(char[] aKey, int offset, int length) -
setValueToThatOfOnlyChild
private void setValueToThatOfOnlyChild() -
removeAtEndOfKey
private boolean removeAtEndOfKey() -
toJavaUtilMap
Gives a Java map representation of this abbreviation map.- Specified by:
toJavaUtilMap
in interfaceOptionNameMap<V>
- Returns:
- a Java map corresponding to this abbreviation map
-
addToMappings
-
charsOf
-