Class PrefixTree

  • All Implemented Interfaces:
    java.lang.Iterable<java.lang.String>, java.util.Collection<java.lang.String>

    class PrefixTree
    extends java.util.AbstractCollection<java.lang.String>
    A prefix tree (aka trie).
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected int prefixLength
      Stores the requested prefix length.
      protected int size
      Store the number of elements contained by this PrefixTree and its sub-trees.
      protected JavaScriptObject subtrees
      Field to store subtrees in.
      protected JavaScriptObject suffixes
      Field to store terminal nodes in.
    • Constructor Summary

      Constructors 
      Constructor Description
      PrefixTree()
      Constructor.
      PrefixTree​(int prefixLength)
      Constructor.
      PrefixTree​(int prefixLength, java.util.Collection<java.lang.String> source)
      Constructor.
      PrefixTree​(java.util.Collection<java.lang.String> source)
      Constructor.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean add​(java.lang.String s)
      Add a String to the PrefixTree.
      void clear()
      Initialize native state.
      boolean contains​(java.lang.Object o)  
      boolean contains​(java.lang.String s)  
      protected static PrefixTree createPrefixTree​(int prefixLength)
      Used by native methods to create an appropriately blessed PrefixTree.
      java.util.List<java.lang.String> getSuggestions​(java.lang.String search, int limit)
      Retrieve suggestions from the PrefixTree.
      java.util.Iterator<java.lang.String> iterator()  
      int size()
      Get the number of all elements contained within the PrefixTree.
      protected void suggestImpl​(java.lang.String search, java.lang.String prefix, java.util.Collection<java.lang.String> output, int limit)  
      • Methods inherited from class java.util.AbstractCollection

        addAll, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray, toString
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Collection

        equals, hashCode, parallelStream, removeIf, spliterator, stream, toArray
      • Methods inherited from interface java.lang.Iterable

        forEach
    • Field Detail

      • prefixLength

        protected final int prefixLength
        Stores the requested prefix length.
      • suffixes

        protected JavaScriptObject suffixes
        Field to store terminal nodes in.
      • size

        protected int size
        Store the number of elements contained by this PrefixTree and its sub-trees.
    • Constructor Detail

      • PrefixTree

        public PrefixTree()
        Constructor.
      • PrefixTree

        public PrefixTree​(java.util.Collection<java.lang.String> source)
        Constructor.
        Parameters:
        source - Initialize from another collection
      • PrefixTree

        public PrefixTree​(int prefixLength)
        Constructor.
        Parameters:
        prefixLength - Smaller prefix length equals faster, more direct searches, at a cost of setup time.
      • PrefixTree

        public PrefixTree​(int prefixLength,
                          java.util.Collection<java.lang.String> source)
        Constructor.
        Parameters:
        prefixLength - Smaller prefix length equals faster, more direct searches, at a cost of setup time.
        source - Initialize from another collection
    • Method Detail

      • createPrefixTree

        protected static PrefixTree createPrefixTree​(int prefixLength)
        Used by native methods to create an appropriately blessed PrefixTree.
        Parameters:
        prefixLength - Smaller prefix length equals faster, more direct searches, at a cost of setup time
        Returns:
        a newly constructed prefix tree
      • add

        public boolean add​(java.lang.String s)
        Add a String to the PrefixTree.
        Specified by:
        add in interface java.util.Collection<java.lang.String>
        Overrides:
        add in class java.util.AbstractCollection<java.lang.String>
        Parameters:
        s - The data to add
        Returns:
        true if the string was added, false otherwise
      • clear

        public void clear()
        Initialize native state.
        Specified by:
        clear in interface java.util.Collection<java.lang.String>
        Overrides:
        clear in class java.util.AbstractCollection<java.lang.String>
      • contains

        public boolean contains​(java.lang.Object o)
        Specified by:
        contains in interface java.util.Collection<java.lang.String>
        Overrides:
        contains in class java.util.AbstractCollection<java.lang.String>
      • contains

        public boolean contains​(java.lang.String s)
      • getSuggestions

        public java.util.List<java.lang.String> getSuggestions​(java.lang.String search,
                                                               int limit)
        Retrieve suggestions from the PrefixTree. The number of items returned from getSuggestions may slightly exceed limit so that all suffixes and partial stems will be returned. This prevents the search space from changing size if the PrefixTree is used in an interactive manner.
        The returned List is guaranteed to be safe; changing its contents will not affect the PrefixTree.
        Parameters:
        search - The prefix to search for
        limit - The desired number of results to retrieve
        Returns:
        A List of suggestions
      • iterator

        public java.util.Iterator<java.lang.String> iterator()
        Specified by:
        iterator in interface java.util.Collection<java.lang.String>
        Specified by:
        iterator in interface java.lang.Iterable<java.lang.String>
        Specified by:
        iterator in class java.util.AbstractCollection<java.lang.String>
      • size

        public int size()
        Get the number of all elements contained within the PrefixTree.
        Specified by:
        size in interface java.util.Collection<java.lang.String>
        Specified by:
        size in class java.util.AbstractCollection<java.lang.String>
        Returns:
        the size of the prefix tree
      • suggestImpl

        protected void suggestImpl​(java.lang.String search,
                                   java.lang.String prefix,
                                   java.util.Collection<java.lang.String> output,
                                   int limit)