mirror of https://github.com/apache/lucene.git
SOLR-4048: add findRecursive method to NamedList
git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1484135 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
1a7228353e
commit
927308b528
|
@ -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
|
||||
----------------------
|
||||
|
||||
|
|
|
@ -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<T> implements Cloneable, Serializable, Iterable<Map.Entry
|
|||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively parses the NamedList structure to arrive at a
|
||||
* specific element. As you descend the NamedList tree, the
|
||||
* last element can be any type, including NamedList, but
|
||||
* the previous elements MUST be NamedList ojects themselves.
|
||||
* This method is particularly useful for parsing the response
|
||||
* from Solr's /admin/mbeans handler.
|
||||
*
|
||||
* Explicit casts are recommended.
|
||||
* Usage examples:
|
||||
*
|
||||
* String coreName = (String) response.findRecursive(
|
||||
* "solr-mbeans", "CORE", "core", "stats", "coreName");
|
||||
* long numDoc = (long) response.findRecursive(
|
||||
* "solr-mbeans", "CORE", "searcher", "stats", "numDocs");
|
||||
*
|
||||
* @param args One or more strings specifying the tree to navigate.
|
||||
* @return the last entry in the tree, null if not found.
|
||||
*/
|
||||
public T findRecursive(String... args) {
|
||||
NamedList<T> 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<T>) value;
|
||||
} else {
|
||||
value = null;
|
||||
break;
|
||||
}
|
||||
}
|
||||
value = list.get(key);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
|
|
@ -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<Object> nl2b = new NamedList<Object>();
|
||||
nl2b.add("key2b1", "value2b1");
|
||||
nl2b.add("key2b2", "value2b2");
|
||||
NamedList<Object> nl3a = new NamedList<Object>();
|
||||
nl3a.add("key3a1", "value3a1");
|
||||
nl3a.add("key3a2", "value3a2");
|
||||
nl3a.add("key3a3", "value3a3");
|
||||
NamedList<Object> nl2 = new NamedList<Object>();
|
||||
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<Object> nl3 = new NamedList<Object>();
|
||||
nl3.add("key3a", nl3a);
|
||||
nl3.add("key3b", "value3b");
|
||||
nl3.add("key3c", "value3c");
|
||||
NamedList<Object> nl = new NamedList<Object>();
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue