Interface PrefixMap<V>

Type Parameters:
V - value type
All Superinterfaces:
Map<String,V>
All Known Implementing Classes:
StringPrefixMap

public interface PrefixMap<V> extends 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 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).

  • Method Details

    • size

      int size()
      Return the number of prefix-value mappings in this prefixmap.
      Specified by:
      size in interface Map<String,V>
      Returns:
      the number of prefix-value mappings in this prefixmap
    • isEmpty

      default boolean isEmpty()
      Return true if empty.
      Specified by:
      isEmpty in interface Map<String,V>
      Returns:
      true if empty.
    • putAll

      default void putAll(Map<? extends String, ? extends V> prefixesAndValues)
      Copies all of the mappings from the specified map to this prefixmap.
      Specified by:
      putAll in interface Map<String,V>
      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.

      Specified by:
      put in interface Map<String,V>
      Parameters:
      prefix - prefix with which the specified value is to be associated
      value - value to be associated with the specified prefix
      Returns:
      the previous value of the specified prefix, or null if there was no mapping for prefix.
      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.
      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:
      remove in interface Map<String,V>
    • containsKey

      default boolean containsKey(Object o)
      Specified by:
      containsKey in interface Map<String,V>
    • containsValue

      default boolean containsValue(Object o)
      Specified by:
      containsValue in interface Map<String,V>
    • get

      V get(String prefix)
      Return the value of the exact matching prefix.

      The value returned is the stored prefix for which is true: input.equals(prefix) .

      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 exact matching prefix.

      The value returned is the stored prefix for which is true: input.equals(prefix) .

      Specified by:
      get in interface Map<String,V>
      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:
      keySet in interface Map<String,V>
    • values

      default Collection<V> values()
      Specified by:
      values in interface Map<String,V>
    • clear

      default void clear()
      The prefixmap will be empty after this call returns.
      Specified by:
      clear in interface Map<String,V>
      Throws:
      UnsupportedOperationException - if the clear operation is not supported by this map
    • containsPrefix

      default boolean containsPrefix(String prefix)
      Returns true if this map contains an exact mapping for the specified prefix.
      Parameters:
      prefix - prefix whose presence in this prefixmap is to be checked
      Returns:
      true if this map contains an the exact mapping for the specified prefix.
    • containsPrefix

      default boolean containsPrefix(int[] prefix)
      Returns true if this map contains an exact mapping for the specified prefix.
      Parameters:
      prefix - prefix whose presence in this prefixmap is to be checked
      Returns:
      true if this map contains an the exact mapping for the specified prefix.
    • containsPrefix

      boolean containsPrefix(PrimitiveIterator.OfInt prefix)
      Returns true if this map contains an exact mapping for the specified prefix.
      Parameters:
      prefix - prefix whose presence in this prefixmap is to be checked
      Returns:
      true if this map contains an the exact mapping for the specified prefix.
    • getShortestMatch

      default V 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).

      Parameters:
      input - The string for which we need value of the stored prefix
      Returns:
      The value, null if not found.
    • getShortestMatch

      default V getShortestMatch(int[] input)
      Return the value of the shortest matching prefix.

      The value returned is the shortest stored prefix for which is true: input.startsWith(prefix).

      Parameters:
      input - The string (iterator of Unicode CodePoints) for which we need value of the stored prefix.
      Returns:
      The value, null if not found.
    • getShortestMatch

      V getShortestMatch(PrimitiveIterator.OfInt input)
      Return the value of the shortest matching prefix.

      The value returned is the shortest stored prefix for which is true: input.startsWith(prefix).

      Parameters:
      input - The string (iterator of Unicode CodePoints) for which we need value of the stored prefix.
      Returns:
      The value, null if not found.
    • getLongestMatch

      default 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).

      Parameters:
      input - The string for which we need value of the stored prefix
      Returns:
      The value, null if not found.
    • getLongestMatch

      default V getLongestMatch(int[] input)
      Return the value of the longest matching prefix.

      The value returned is the longest stored prefix for which is true: input.startsWith(prefix).

      Parameters:
      input - The string (iterator of Unicode CodePoints) for which we need value of the stored prefix.
      Returns:
      The value, null if not found.
    • getLongestMatch

      V getLongestMatch(PrimitiveIterator.OfInt input)
      Return the value of the longest matching prefix.

      The value returned is the longest stored prefix for which is true: input.startsWith(prefix).

      Parameters:
      input - The string (iterator of Unicode CodePoints) for which we need value of the stored prefix.
      Returns:
      The value, null if not found.
    • getAllMatches

      default Iterator<V> getAllMatches(String input)
      Returns List of all matches that have a value.

      The list contains all non-null values for the prefix values where this is true: input.startsWith(prefix).

      Parameters:
      input - The string for which we need value of the stored prefix
      Returns:
      The list of values, an empty List if nothing is found.
    • getAllMatches

      default Iterator<V> getAllMatches(int[] input)
      Returns List of all matches that have a value.

      The list contains all non-null values for the prefix values where this is true: input.startsWith(prefix).

      Parameters:
      input - The string (iterator of Unicode CodePoints) for which we need value of the stored prefix.
      Returns:
      The list of values, an empty List if nothing is found.
    • getAllMatches

      Iterator<V> getAllMatches(PrimitiveIterator.OfInt input)
      Returns List of all matches that have a value.

      The list contains all non-null values for the prefix values where this is true: input.startsWith(prefix).

      Parameters:
      input - The string (iterator of Unicode CodePoints) for which we need value of the stored prefix.
      Returns:
      The list of values, an empty List if nothing is found.