Class AbbreviationMap<V>
- java.lang.Object
-
- joptsimple.internal.AbbreviationMap<V>
-
- Type Parameters:
V- a constraint on the types of the values in the map
- All Implemented Interfaces:
OptionNameMap<V>
public 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 yieldnull, since it would no longer be a unique abbreviation.The data structure is much like a "trie".
- See Also:
- Perl's Text::Abbrev module, Radix tree
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.Map<java.lang.Character,AbbreviationMap<V>>childrenprivate java.lang.Stringkeyprivate intkeysBeyondprivate Vvalue
-
Constructor Summary
Constructors Constructor Description AbbreviationMap()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private booleanadd(char[] chars, V newValue, int offset, int length)private voidaddToMappings(java.util.Map<java.lang.String,V> mappings)private static char[]charsOf(java.lang.String aKey)booleancontains(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.Vget(java.lang.String key)Answers the value associated with the given key.voidput(java.lang.String key, V newValue)Associates a given value with a given key.voidputAll(java.lang.Iterable<java.lang.String> keys, V newValue)Associates a given value with a given set of keys.private booleanremove(char[] aKey, int offset, int length)voidremove(java.lang.String key)If the map contains the given key, dissociates the key from its value.private booleanremoveAtEndOfKey()private voidsetValueToThatOfOnlyChild()java.util.Map<java.lang.String,V>toJavaUtilMap()Gives a Java map representation of this abbreviation map.
-
-
-
Field Detail
-
children
private final java.util.Map<java.lang.Character,AbbreviationMap<V>> children
-
key
private java.lang.String key
-
value
private V value
-
keysBeyond
private int keysBeyond
-
-
Method Detail
-
contains
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.
- Specified by:
containsin interfaceOptionNameMap<V>- Parameters:
key- key to look up- Returns:
trueifkeyis present in the map- Throws:
java.lang.NullPointerException- ifkeyisnull
-
get
public 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.
- Specified by:
getin interfaceOptionNameMap<V>- Parameters:
key- key to look up- Returns:
- the value associated with
aKey; ornullif there is no such value oraKeyis not a unique abbreviation of a key in the map - Throws:
java.lang.NullPointerException- ifaKeyisnull
-
put
public 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.
- Specified by:
putin interfaceOptionNameMap<V>- Parameters:
key- key to create in the mapnewValue- value to associate with the key- Throws:
java.lang.NullPointerException- ifaKeyornewValueisnulljava.lang.IllegalArgumentException- ifaKeyis a zero-length string
-
putAll
public 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.
- Specified by:
putAllin interfaceOptionNameMap<V>- Parameters:
keys- keys to create in the mapnewValue- value to associate with the key- Throws:
java.lang.NullPointerException- ifkeysornewValueisnulljava.lang.IllegalArgumentException- if any ofkeysis a zero-length string
-
add
private boolean add(char[] chars, V newValue, int offset, int length)
-
remove
public void remove(java.lang.String key)
If the map contains the given key, dissociates the key from its value.
- Specified by:
removein interfaceOptionNameMap<V>- Parameters:
key- key to remove- Throws:
java.lang.NullPointerException- ifaKeyisnulljava.lang.IllegalArgumentException- ifaKeyis a zero-length string
-
remove
private boolean remove(char[] aKey, int offset, int length)
-
setValueToThatOfOnlyChild
private void setValueToThatOfOnlyChild()
-
removeAtEndOfKey
private boolean removeAtEndOfKey()
-
toJavaUtilMap
public java.util.Map<java.lang.String,V> toJavaUtilMap()
Gives a Java map representation of this abbreviation map.- Specified by:
toJavaUtilMapin interfaceOptionNameMap<V>- Returns:
- a Java map corresponding to this abbreviation map
-
addToMappings
private void addToMappings(java.util.Map<java.lang.String,V> mappings)
-
charsOf
private static char[] charsOf(java.lang.String aKey)
-
-