Create a Trie class that does Regex matching

Change-Id: Iedb96f861e430e542b3aa8295b824d084e9a8fdd
diff --git a/src/com/android/tradefed/util/RegexTrie.java b/src/com/android/tradefed/util/RegexTrie.java
new file mode 100644
index 0000000..1e29807
--- /dev/null
+++ b/src/com/android/tradefed/util/RegexTrie.java
@@ -0,0 +1,126 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.tradefed.util;
+
+import java.util.Arrays;
+import java.util.LinkedHashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.regex.Pattern;
+import java.util.regex.Matcher;
+
+/**
+ * The RegexTrie is a trie where each _stored_ segment of the key is a regex {@link Pattern}.  Thus,
+ * the full _stored_ key is a List<Pattern> rather than a String as in a standard trie.  Note that
+ * the {@link get(Object key)} requires a List<String>, which will be matched against the
+ * {@link Pattern}s, rather than checked for equality as in a standard trie.  It will likely perform
+ * poorly for large datasets.
+ */
+public class RegexTrie<V> {
+    private V mValue = null;
+    private Map<Pattern, RegexTrie<V>> mChildren = new LinkedHashMap<Pattern, RegexTrie<V>>();
+
+    public void clear() {
+        mValue = null;
+        for (RegexTrie child : mChildren.values()) {
+            child.clear();
+        }
+        mChildren.clear();
+    }
+
+    boolean containsKey(String... strings) {
+        return retrieve(strings) != null;
+    }
+
+    V recursivePut(V value, List<Pattern> patterns) {
+        // Cases:
+        // 1) patterns is empty -- set our value
+        // 2) patterns is non-empty -- recurse downward, creating a child if necessary
+        if (patterns.isEmpty()) {
+            V oldValue = mValue;
+            mValue = value;
+            return oldValue;
+        } else {
+            Pattern curKey = patterns.get(0);
+            List<Pattern> nextKeys = patterns.subList(1, patterns.size());
+
+            // Create a new child to handle
+            RegexTrie<V> nextChild = mChildren.get(curKey);
+            if (nextChild == null) {
+                nextChild = new RegexTrie<V>();
+                mChildren.put(curKey, nextChild);
+            }
+            return nextChild.recursivePut(value, nextKeys);
+        }
+    }
+
+    public V put(V value, Pattern... patterns) {
+        if (patterns.length == 0) {
+            throw new IllegalArgumentException("pattern list must be non-empty");
+        }
+        List<Pattern> pList = Arrays.asList(patterns);
+        return recursivePut(value, pList);
+    }
+
+    /**
+     * This helper method takes a list of regular expressions as Strings and compiles them
+     * on-the-fly before recursing
+     */
+    public V put(V value, String... regexen) {
+        Pattern[] patterns = new Pattern[regexen.length];
+        for (int i = 0; i < regexen.length; ++i) {
+            patterns[i] = Pattern.compile(regexen[i]);
+        }
+        return put(value, patterns);
+    }
+
+    V recursiveRetrieve(List<String> strings) {
+        // Cases:
+        // 1) strings is empty -- return our value
+        // 2) strings is non-empty -- find the first child that matches, recurse downward
+        if (strings.isEmpty()) {
+            return mValue;
+        } else {
+            String curKey = strings.get(0);
+            List<String> nextKeys = strings.subList(1, strings.size());
+
+            for (Map.Entry<Pattern, RegexTrie<V>> child : mChildren.entrySet()) {
+                Matcher matcher = child.getKey().matcher(curKey);
+                if (matcher.matches()) {
+                    return child.getValue().recursiveRetrieve(nextKeys);
+                }
+            }
+
+            // no match
+            return null;
+        }
+    }
+
+    public V retrieve(String... strings) {
+        if (strings.length == 0) {
+            throw new IllegalArgumentException("string list must be non-empty");
+        }
+        List<String> sList = Arrays.asList(strings);
+        return recursiveRetrieve(sList);
+    }
+
+    @Override
+    public String toString() {
+        return String.format("{V: %s, C: %s}", mValue, mChildren);
+    }
+}
+
diff --git a/tests/src/com/android/tradefed/util/RegexTrieTest.java b/tests/src/com/android/tradefed/util/RegexTrieTest.java
new file mode 100644
index 0000000..6d10813
--- /dev/null
+++ b/tests/src/com/android/tradefed/util/RegexTrieTest.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.tradefed.util;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import junit.framework.TestCase;
+
+/**
+ * Set of unit tests to verify the behavior of the RegexTrie
+ */
+public class RegexTrieTest extends TestCase {
+    private RegexTrie<Integer> mTrie = null;
+    private static final Integer mStored = 42;
+
+    @Override
+    public void setUp() throws Exception {
+        mTrie = new RegexTrie<Integer>();
+    }
+
+    public void testStringPattern() {
+        mTrie.put(mStored, "[p]art1", "[p]art2", "[p]art3");
+        System.err.format("Trie is '%s'\n", mTrie.toString());
+        Integer retrieved = mTrie.retrieve("part1", "part2", "part3");
+        assertEquals(mStored, retrieved);
+    }
+
+    public void testAlternation_single() {
+        mTrie.put(mStored, "alpha|beta");
+        Integer retrieved;
+        retrieved = mTrie.retrieve("alpha");
+        assertEquals(mStored, retrieved);
+        retrieved = mTrie.retrieve("beta");
+        assertEquals(mStored, retrieved);
+        retrieved = mTrie.retrieve("alpha|beta");
+        assertNull(retrieved);
+        retrieved = mTrie.retrieve("gamma");
+        assertNull(retrieved);
+        retrieved = mTrie.retrieve("alph");
+        assertNull(retrieved);
+    }
+
+    public void testAlternation_multiple() {
+        mTrie.put(mStored, "a|alpha", "b|beta");
+        Integer retrieved;
+        retrieved = mTrie.retrieve("a", "b");
+        assertEquals(mStored, retrieved);
+        retrieved = mTrie.retrieve("a", "beta");
+        assertEquals(mStored, retrieved);
+        retrieved = mTrie.retrieve("alpha", "b");
+        assertEquals(mStored, retrieved);
+        retrieved = mTrie.retrieve("alpha", "beta");
+        assertEquals(mStored, retrieved);
+
+        retrieved = mTrie.retrieve("alpha");
+        assertNull(retrieved);
+        retrieved = mTrie.retrieve("beta");
+        assertNull(retrieved);
+        retrieved = mTrie.retrieve("alpha", "bet");
+        assertNull(retrieved);
+    }
+}
+