diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index 966dce8b4ac..125ed5f54e7 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -82,6 +82,8 @@ New Features * SOLR-4234: Add support for binary files in ZooKeeper. (Eric Pugh via Mark Miller) +* SOLR-4048: Add findRecursive method to NamedList. (Shawn Heisey) + Bug Fixes ---------------------- diff --git a/solr/solrj/src/java/org/apache/solr/common/util/NamedList.java b/solr/solrj/src/java/org/apache/solr/common/util/NamedList.java index 9ef19e5fded..f94c8ef50be 100644 --- a/solr/solrj/src/java/org/apache/solr/common/util/NamedList.java +++ b/solr/solrj/src/java/org/apache/solr/common/util/NamedList.java @@ -17,8 +17,11 @@ package org.apache.solr.common.util; -import java.util.*; import java.io.Serializable; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Map; @@ -252,6 +255,47 @@ public class NamedList implements Cloneable, Serializable, Iterable list = null; + T value = null; + for (String key : args) { + /* First pass: this list. Later passes: previous value. */ + if (list == null) { + list = this; + } + else + { + if (NamedList.class.isInstance(value)) { + list = (NamedList) value; + } else { + value = null; + break; + } + } + value = list.get(key); + } + return value; + } + @Override public String toString() { StringBuilder sb = new StringBuilder(); diff --git a/solr/solrj/src/test/org/apache/solr/common/util/NamedListTest.java b/solr/solrj/src/test/org/apache/solr/common/util/NamedListTest.java index d66269514e5..176c2195621 100644 --- a/solr/solrj/src/test/org/apache/solr/common/util/NamedListTest.java +++ b/solr/solrj/src/test/org/apache/solr/common/util/NamedListTest.java @@ -29,4 +29,65 @@ public class NamedListTest extends LuceneTestCase { assertEquals("value1", value); assertEquals(1, nl.size()); } + public void testRecursive() { + // key1 + // key2 + // - key2a + // - key2b + // --- key2b1 + // --- key2b2 + // - key2c + // key3 + // - key3a + // --- key3a1 + // --- key3a2 + // --- key3a3 + // - key3b + // - key3c + NamedList nl2b = new NamedList(); + nl2b.add("key2b1", "value2b1"); + nl2b.add("key2b2", "value2b2"); + NamedList nl3a = new NamedList(); + nl3a.add("key3a1", "value3a1"); + nl3a.add("key3a2", "value3a2"); + nl3a.add("key3a3", "value3a3"); + NamedList nl2 = new NamedList(); + nl2.add("key2a", "value2a"); + nl2.add("key2b", nl2b); + int int1 = 5; + Integer int2 = 7; + int int3 = 48; + nl2.add("k2int1", int1); + nl2.add("k2int2", int2); + nl2.add("k2int3", int3); + NamedList nl3 = new NamedList(); + nl3.add("key3a", nl3a); + nl3.add("key3b", "value3b"); + nl3.add("key3c", "value3c"); + NamedList nl = new NamedList(); + nl.add("key1", "value1"); + nl.add("key2", nl2); + nl.add("key3", nl3); + + String test1 = (String) nl.findRecursive("key2", "key2b", "key2b2"); + assertEquals(test1, "value2b2"); + String test2 = (String) nl.findRecursive("key3", "key3a", "key3a3"); + assertEquals(test2, "value3a3"); + String test3 = (String) nl.findRecursive("key3", "key3c"); + assertEquals(test3, "value3c"); + String test4 = (String) nl.findRecursive("key3", "key3c", "invalid"); + assertEquals(test4, null); + String test5 = (String) nl.findRecursive("key3", "invalid", "invalid"); + assertEquals(test5, null); + String test6 = (String) nl.findRecursive("invalid", "key3c"); + assertEquals(test6, null); + Object nltest = nl.findRecursive("key2", "key2b"); + assertTrue(nltest instanceof NamedList); + Integer int1test = (Integer) nl.findRecursive("key2", "k2int1"); + assertEquals(int1test, (Integer) 5); + int int2test = (int) nl.findRecursive("key2", "k2int2"); + assertEquals(int2test, 7); + int int3test = (int) nl.findRecursive("key2", "k2int3"); + assertEquals(int3test, 48); + } }