public class

RegexTrie

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

Class Overview

The RegexTrie is a trie where each stored segment of the key is a regex Pattern. Thus, the full stored key is a List<Pattern> rather than a List<String> 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

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()
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public RegexTrie ()

Public Methods

public void clear ()

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

Add an entry to the trie.

Parameters
value The value to set
patterns The sequence of Patterns that must be sequentially matched to retrieve the associated value

public 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 The value to set
regexen 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 ERROR(/#put(V, Pattern...)).

public 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 A sequence of Strings to match
Returns
  • The associated value, or null if no value was found

public 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 A List> through which capture groups will be returned.
strings A sequence of Strings to match
Returns
  • The associated value, or null if no value was found

public String toString ()