You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by no...@apache.org on 2014/10/24 14:59:59 UTC
svn commit: r1634047 -
/lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/common/util/NamedList.java
Author: noble
Date: Fri Oct 24 12:59:58 2014
New Revision: 1634047
URL: http://svn.apache.org/r1634047
Log:
added an asMap() method
Modified:
lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/common/util/NamedList.java
Modified: lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/common/util/NamedList.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/common/util/NamedList.java?rev=1634047&r1=1634046&r2=1634047&view=diff
==============================================================================
--- lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/common/util/NamedList.java (original)
+++ lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/common/util/NamedList.java Fri Oct 24 12:59:58 2014
@@ -23,6 +23,7 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
+import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@@ -390,6 +391,31 @@ public class NamedList<T> implements Clo
return new NamedList<>( Collections.unmodifiableList(copy.nvPairs));
}
+ public Map asMap(int maxDepth) {
+ LinkedHashMap result = new LinkedHashMap();
+ for(int i=0;i<size();i++){
+ Object val = getVal(i);
+ if (val instanceof NamedList && maxDepth> 0) {
+ //the maxDepth check is to avoid stack overflow due to infinite recursion
+ val = ((NamedList) val).asMap(maxDepth-1);
+ }
+ Object old = result.put(getName(i), val);
+ if(old!=null){
+ if (old instanceof List) {
+ List list = (List) old;
+ list.add(val);
+ result.put(getName(i),old);
+ } else {
+ ArrayList l = new ArrayList();
+ l.add(old);
+ l.add(val);
+ result.put(getName(i), l);
+ }
+ }
+ }
+ return result;
+ }
+
/**
*
* Helper class implementing Map.Entry<String, T> to store the key-value