RegexTrie

public class RegexTrie
extends Object

java.lang.Object
   ↳ com.android.tradefed.util.RegexTrie<V>


The RegexTrie is a trie where each stored segment of the key is a regex Pattern. Thus, the full stored key is a List&lt;Pattern&gt; rather than a List&lt;String&gt; as in a standard trie. Note that the retrieve(String) method will be pointwise matched against the Patterns, rather than checked for pointwise equality as in a standard trie. Because of this, it may perform poorly for large datasets.

One can also use a null entry in the Pattern sequence to serve as a wildcard. If a null is encountered, all subsequent entries in the sequence will be ignored. When the retrieval code encounters a null Pattern, it will first wait to see if a more-specific entry matches the sequence. If one does, that more-specific entry will proceed, even if it subsequently fails to match.

If no more-specific entry matches, the wildcard match will add all remaining Strings to the list of captures (if enabled) and return the value associated with the wildcard.

A short sample of the wildcard functionality:

 List<List<String>> captures = new LinkedList<List<String>>();
 RegexTrie<Integer> trie = new RegexTrie<Integer>();
 trie.put(2, "a", null);
 trie.put(4, "a", "b");
 trie.retrieve(captures, "a", "c", "e");
 // returns 2.  captures is now [[], ["c"], ["e"]]
 trie.retrieve(captures, "a", "b");
 // returns 4.  captures is now [[], []]
 trie.retrieve(captures, "a", "b", "c");
 // returns null.  captures is now [[], []]
 

Summary

Nested classes

class RegexTrie.CompPattern

Patterns aren't comparable by default, which prevents you from retrieving them from a HashTable. 

Public constructors

RegexTrie()

Public methods

void clear()
V put(V value, Pattern... patterns)

Add an entry to the trie.

V put(V value, String... regexen)

This helper method takes a list of regular expressions as Strings and compiles them on-the-fly before adding the subsequent Patterns to the trie

V retrieve(String... strings)

Fetch a value from the trie, by matching the provided sequence of Strings to a sequence of Patterns stored in the trie.

V retrieve(List<List<String>> captures, String... strings)

Fetch a value from the trie, by matching the provided sequence of Strings to a sequence of Patterns stored in the trie.

String toString()

Public constructors

RegexTrie

RegexTrie ()

Public methods

clear

void clear ()

put

V put (V value, 
                Pattern... patterns)

Add an entry to the trie.

Parameters
value V: The value to set

patterns Pattern: The sequence of Patterns that must be sequentially matched to retrieve the associated value

Returns
V

put

V put (V value, 
                String... regexen)

This helper method takes a list of regular expressions as Strings and compiles them on-the-fly before adding the subsequent Patterns to the trie

Parameters
value V: The value to set

regexen String: The sequence of regular expressions (as Strings) that must be sequentially matched to retrieve the associated value. Each String will be compiled as a Pattern before invoking put(Object, Pattern).

Returns
V

retrieve

V retrieve (String... strings)

Fetch a value from the trie, by matching the provided sequence of Strings to a sequence of Patterns stored in the trie.

Parameters
strings String: A sequence of Strings to match

Returns
V The associated value, or null if no value was found

retrieve

V retrieve (List<List<String>> captures, 
                String... strings)

Fetch a value from the trie, by matching the provided sequence of Strings to a sequence of Patterns stored in the trie. This version of the method also returns a List of capture groups for each Pattern that was matched.

Each entry in the outer List corresponds to one level of Pattern in the trie. For each level, the list of capture groups will be stored. If there were no captures for a particular level, an empty list will be stored.

Note that captures will be clear()ed before the retrieval begins. Also, if the retrieval fails after a partial sequence of matches, captures will still reflect the capture groups from the partial match.

Parameters
captures List: A List<List<String>> through which capture groups will be returned.

strings String: A sequence of Strings to match

Returns
V The associated value, or null if no value was found

toString

String toString ()

Returns
String