joptsimple.internal
Class AbbreviationMap<V>

java.lang.Object
  extended by joptsimple.internal.AbbreviationMap<V>
Type Parameters:
V - a constraint on the types of the values in the map

public class AbbreviationMap<V>
extends java.lang.Object

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".

Version:
$Id: AbbreviationMap.java,v 1.11 2009/03/06 20:35:08 pholser Exp $
Author:
Paul Holser
See Also:
Perl's Text::Abbrev module

Field Summary
private  java.util.Map<java.lang.Character,AbbreviationMap<V>> children
           
private  java.lang.String key
           
private  int keysBeyond
           
private  V value
           
 
Constructor Summary
AbbreviationMap()
           
 
Method Summary
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 aKey)
          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 aKey)
          Answers the value associated with the given key.
 void put(java.lang.String aKey, V newValue)
          Associates a given value with a given key.
 void putAll(java.util.Collection<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 aKey)
          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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

key

private java.lang.String key

value

private V value

children

private final java.util.Map<java.lang.Character,AbbreviationMap<V>> children

keysBeyond

private int keysBeyond
Constructor Detail

AbbreviationMap

public AbbreviationMap()
Method Detail

contains

public boolean contains(java.lang.String aKey)

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.

Parameters:
aKey - key to look up
Returns:
true if key is present in the map
Throws:
java.lang.NullPointerException - if key is null

get

public V get(java.lang.String aKey)

Answers the value associated with the given key. The key can be a unique abbreviation of a key that is in the map.

Parameters:
aKey - key to look up
Returns:
the value associated with aKey; or null if there is no such value or aKey is not a unique abbreviation of a key in the map
Throws:
java.lang.NullPointerException - if aKey is null

put

public void put(java.lang.String aKey,
                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.

Parameters:
aKey - key to create in the map
newValue - value to associate with the key
Throws:
java.lang.NullPointerException - if aKey or newValue is null
java.lang.IllegalArgumentException - if aKey is a zero-length string

putAll

public void putAll(java.util.Collection<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.

Parameters:
keys - keys to create in the map
newValue - value to associate with the key
Throws:
java.lang.NullPointerException - if keys or newValue is null
java.lang.IllegalArgumentException - if any of keys is a zero-length string

add

private boolean add(char[] chars,
                    V newValue,
                    int offset,
                    int length)

remove

public void remove(java.lang.String aKey)

If the map contains the given key, dissociates the key from its value.

Parameters:
aKey - key to remove
Throws:
java.lang.NullPointerException - if aKey is null
java.lang.IllegalArgumentException - if aKey is 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.

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)