Interface PrefixMap<V extends Serializable>
-
- All Superinterfaces:
Map<String,V>,Serializable
- All Known Implementing Classes:
ASCIIPrefixMap,StringPrefixMap
public interface PrefixMap<V extends Serializable> extends Serializable, Map<String,V>
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
inputagainst each of the storedprefixes. 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.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default voidclear()The prefixmap will be empty after this call returns.default booleancontainsKey(Object o)booleancontainsPrefix(String prefix)Returnstrueif this map contains an exact mapping for the specified prefix.default booleancontainsValue(Object o)default Vget(Object prefix)Return the value of theexactmatching prefix.Vget(String prefix)Return the value of theexactmatching prefix.VgetLongestMatch(String input)Return the value of the longest matching prefix.VgetShortestMatch(String input)Return the value of theshortestmatching prefix.default booleanisEmpty()default Set<String>keySet()Vput(String prefix, V value)Stored the specified value as the result for the specified prefix.default voidputAll(Map<? extends String,? extends V> prefixesAndValues)Copies all of the mappings from the specified map to this prefixmap.default Vremove(Object o)default Vremove(String prefix)Removes the mapping for a prefix if present.intsize()default Collection<V>values()-
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, entrySet, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAll
-
-
-
-
Method Detail
-
size
int size()
- Specified by:
sizein interfaceMap<String,V extends Serializable>- Returns:
- the number of prefix-value mappings in this prefixmap
-
isEmpty
default boolean isEmpty()
- Specified by:
isEmptyin interfaceMap<String,V extends Serializable>- Returns:
- true if empty.
-
containsPrefix
boolean containsPrefix(String prefix)
Returns
trueif this map contains an exact mapping for the specified prefix.Note that implementations may be constructed to match either case sensitive or case insensitive.
- Parameters:
prefix- prefix whose presence in this prefixmap is to be checked- Returns:
trueif this map contains an the exact mapping for the specified prefix.
-
putAll
default void putAll(Map<? extends String,? extends V> prefixesAndValues)
Copies all of the mappings from the specified map to this prefixmap.- Specified by:
putAllin interfaceMap<String,V extends Serializable>- Parameters:
prefixesAndValues- mappings to be stored in this map- Throws:
NullPointerException- if one or more of the prefixes or values are null. If this happens the PrefixMap is in an undefined state.
-
put
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.
- Specified by:
putin interfaceMap<String,V extends Serializable>- Parameters:
prefix- prefix with which the specified value is to be associatedvalue- value to be associated with the specified prefix- Returns:
- the previous value of the specified
prefix, ornullif there was no mapping forprefix. - Throws:
NullPointerException- if either the prefix or value are null. If this happens the PrefixMap is unchanged.
-
remove
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).
- Parameters:
prefix- prefix whose mapping is to be removed from the prefixmap- Returns:
- the previous value associated with
prefix(may be null). - Throws:
UnsupportedOperationException- if not implemented.
-
remove
default V remove(Object o)
- Specified by:
removein interfaceMap<String,V extends Serializable>
-
containsKey
default boolean containsKey(Object o)
- Specified by:
containsKeyin interfaceMap<String,V extends Serializable>
-
containsValue
default boolean containsValue(Object o)
- Specified by:
containsValuein interfaceMap<String,V extends Serializable>
-
get
V get(String prefix)
Return the value of the
exactmatching prefix.The value returned is the stored prefix for which is true:
input.equals(prefix).Note that implementations may be constructed to match either case sensitive or case insensitive.
- Parameters:
prefix- The string for which we need value of the stored prefix- Returns:
- The value, null if not found.
-
get
default V get(Object prefix)
Return the value of the
exactmatching prefix.The value returned is the stored prefix for which is true:
input.equals(prefix).Note that implementations may be constructed to match either case sensitive or case insensitive.
- Specified by:
getin interfaceMap<String,V extends Serializable>- Parameters:
prefix- The string for which we need the stored value- Returns:
- The value, null if not found.
-
keySet
default Set<String> keySet()
- Specified by:
keySetin interfaceMap<String,V extends Serializable>
-
values
default Collection<V> values()
- Specified by:
valuesin interfaceMap<String,V extends Serializable>
-
clear
default void clear()
The prefixmap will be empty after this call returns.- Specified by:
clearin interfaceMap<String,V extends Serializable>- Throws:
UnsupportedOperationException- if theclearoperation is not supported by this map
-
getShortestMatch
V getShortestMatch(String input)
Return the value of the
shortestmatching 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.
- Parameters:
input- The string for which we need value of the stored prefix- Returns:
- The value, null if not found.
-
getLongestMatch
V 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.
- Parameters:
input- The string for which we need value of the stored prefix- Returns:
- The value, null if not found.
-
-