public interface PrefixMap<V extends Serializable> extends Serializable
An object that maps String prefixes to values. A PrefixMap cannot contain duplicate prefixes; each prefix can map to at most one value.
The PrefixMap is a key-value type collection where the key is assumed to be the prefix of the String for which later an answer is requested.
So the retrieval of the value is based on a startsWith() check of the requested input against each of the stored prefixes. The value associated with the selected prefix is then returned.
An example use case is where mobile phone device models of a certain brand all start with the same substring. This data structure can be used to efficiently retrieve the best fitting prefix to find the associated value (i.e. the brand of the device).
Note that implementations may be constructed to match either case sensitive or case insensitive.
| Modifier and Type | Method and Description |
|---|---|
default void |
clear()
The prefixmap will be empty after this call returns.
|
boolean |
containsPrefix(String prefix)
Returns true if this map contains an exact mapping
for the specified prefix.
|
V |
getLongestMatch(String input)
Return the value of the longest matching prefix.
|
V |
getShortestMatch(String input)
Return the value of the shortest matching prefix.
|
default boolean |
isEmpty() |
V |
put(String prefix,
V value)
Stored the specified value as the result for the specified prefix.
|
default void |
putAll(Map<String,V> prefixesAndValues)
Copies all of the mappings from the specified map to this prefixmap.
|
default V |
remove(String prefix)
Removes the mapping for a prefix if present.
|
int |
size() |
int size()
default boolean isEmpty()
boolean containsPrefix(String prefix)
Returns true if this map contains an exact mapping for the specified prefix.
Note that implementations may be constructed to match either case sensitive or case insensitive.
prefix - prefix whose presence in this prefixmap is to be checkeddefault void putAll(Map<String,V> prefixesAndValues)
prefixesAndValues - mappings to be stored in this mapNullPointerException - if one or more of the prefixes or values are null.
If this happens the PrefixMap is in an undefined state.V put(String prefix, V value)
Stored the specified value as the result for the specified prefix.
If for the specified prefix previously a value was present then the old value is replaced with the new value and the old value is returned.
Note that implementations may be constructed to store this value for either case sensitive or case insensitive matching during retrieval.
prefix - prefix with which the specified value is to be associatedvalue - value to be associated with the specified prefixNullPointerException - if either the prefix or value are null.
If this happens the PrefixMap is unchanged.default V remove(String prefix)
Removes the mapping for a prefix if present.
Note that implementations may be constructed to remove either case sensitive (only this exact value) or case insensitive (all case variations).
prefix - prefix whose mapping is to be removed from the prefixmapUnsupportedOperationException - if not implemented.default void clear()
UnsupportedOperationException - if the clear operation
is not supported by this mapV getShortestMatch(String input)
Return the value of the shortest matching prefix.
The value returned is the shortest stored prefix for which is true: input.startsWith(prefix).
Note that implementations may be constructed to match either case sensitive or case insensitive.
input - The string for which we need value of the stored prefixV getLongestMatch(String input)
Return the value of the longest matching prefix.
The value returned is the longest stored prefix for which is true: input.startsWith(prefix).
Note that implementations may be constructed to match either case sensitive or case insensitive.
input - The string for which we need value of the stored prefixCopyright © 2019. All rights reserved.