Class TernarySearchTree<Value>

  • Type Parameters:
    Value - type of values in this tree

    public final class TernarySearchTree<Value>
    extends Object
    A ternary search tree with String keys and generic values. TernarySearchTree is a type of trie (sometimes called a prefix tree) where nodes are arranged in a manner similar to a binary search tree, but with up to three children rather than the binary tree's limit of two. Like other prefix trees, a ternary search tree can be used as an associative map structure with the ability for incremental string search. However, ternary search trees are more space efficient compared to standard prefix trees, at the cost of speed. Keys must not be null or empty. Values cannot be null. This class is thread safe.
    Since:
    6.5
    • Constructor Detail

      • TernarySearchTree

        public TernarySearchTree()
        Construct a new ternary search tree
    • Method Detail

      • getLock

        public ReadWriteLock getLock()
        Get the lock guarding read and write access to the cache.
        Returns:
        lock guarding read and write access to the cache
      • replace

        public int replace​(Iterable<Map.Entry<String,​Value>> loader)
        Replace the tree with a new tree containing all entries provided by an iterable
        Parameters:
        loader - iterable providing key-value pairs to load
        Returns:
        number of key-value pairs after replacing finished
      • reload

        public int reload​(Iterable<Map.Entry<String,​Value>> loader)
        Reload the tree entries provided by loader
        Parameters:
        loader - iterable providing key-value pairs to load
        Returns:
        number of key-value pairs
      • delete

        public int delete​(Iterable<String> delete)
        Delete entries
        Parameters:
        delete - iterable providing keys of entries to be deleted
        Returns:
        number of key-value pairs
      • size

        public int size()
        Get the number of key value pairs in this trie
        Returns:
        number of key value pairs in this trie
      • get

        @Nullable
        public Value get​(String key)
        Get the value associated to a key or null.
        Parameters:
        key - the key
        Returns:
        the value associated to this key
      • contains

        public boolean contains​(String key)
        Check whether this tree contains the given key.
        Parameters:
        key - a key
        Returns:
        whether this tree contains this key
      • insert

        public int insert​(String key,
                          Value value)
        Insert a key-value pair. If the key already exists the old value is overwritten.
        Parameters:
        key - the key
        value - the value
        Returns:
        number of key-value pairs after adding the entry
      • insert

        public int insert​(Map<String,​Value> map)
        Insert map of key-value pairs. Values of existing keys are overwritten. Use this method to insert multiple key-value pairs.
        Parameters:
        map - map of key-value pairs to insert
        Returns:
        number of key-value pairs after adding entries
      • delete

        public int delete​(String key)
        Delete a key-value pair. Does nothing if the key doesn't exist.
        Parameters:
        key - the key
        Returns:
        number of key-value pairs after the deletion
      • clear

        public void clear()
        Remove all key value pairs from this tree
      • keyLongestPrefixOf

        @Nullable
        public String keyLongestPrefixOf​(String query)
        Find the key which is the longest prefix of the given query string.
        Parameters:
        query -
        Returns:
        the key which is the longest prefix of the given query string or null if none exists.
      • getKeys

        public Iterable<String> getKeys()
        Get all keys.
        Returns:
        all keys
      • getKeysWithPrefix

        public Iterable<String> getKeysWithPrefix​(String prefix)
        Get keys starting with given prefix
        Parameters:
        prefix - key prefix
        Returns:
        keys starting with given prefix
      • getAll

        public Map<String,​Value> getAll()
        Get all entries.
        Returns:
        all entries
      • getAllValues

        public List<Value> getAllValues()
        Get all values.
        Returns:
        all values
      • getWithPrefix

        public Map<String,​Value> getWithPrefix​(String prefix)
        Get all entries with given prefix
        Parameters:
        prefix - key prefix
        Returns:
        entries with given prefix
      • getValuesWithPrefix

        public List<Value> getValuesWithPrefix​(String prefix)
        Get all values with given key prefix
        Parameters:
        prefix - key prefix
        Returns:
        entries with given prefix
      • getKeysMatching

        public Iterable<String> getKeysMatching​(String pattern)
        Get keys matching given pattern using '?' as wildcard character.
        Parameters:
        pattern - search pattern
        Returns:
        keys matching given pattern.