package org.apache.solr.common.util;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.solr.common.SolrException;
/**
* 一个简单的容器类 用来模拟一个有序的 name/value 键值对列表
*
* 不同于 Maps:
*
*
* Names可以重复
* 元素具有顺序性
* 元素可以通过数值索引来访问
* Names和 Values可以都为 null
*
*
*
* A NamedList provides fast access by element number, but not by name.
*
*
* NamedList被序列化后,元素的顺序就比较重要了.所以ResponseWriters输出格式如JSON通常会选择一个容易在不同客户端保存并持有顺序的数据结构.
*
* 如果通过key访问比序列化更重要是,参考{@link SimpleOrderedMap},或者简单使用常规的{@link Map}
*
*
*/
public class NamedList implements Cloneable, Serializable, Iterable {
private static final long serialVersionUID = 1957981902839867821L;
protected final List nvPairs;
/** Creates an empty instance */
public NamedList() {
nvPairs = new ArrayList();
}
/**
*创建一个实例,支持Map.Entry[]类型
*
*
* Modifying the contents of the Entry[] after calling this constructor may change
* the NamedList (in future versions of Solr), but this is not guaranteed and should
* not be relied upon. To modify the NamedList, refer to {@link #add(String, Object)}
* or {@link #remove(String)}.
*
*
* @param nameValuePairs the name value pairs
*/
public NamedList(Map.Entry[] nameValuePairs) {
nvPairs = nameValueMapToList(nameValuePairs);
}
/**
*创建一个实例,支持明确的name/value配对键值.
*
* When using this constructor, runtime type safety is only guaranteed if
* all even numbered elements of the input list are of type "T".
*
*
* @param nameValuePairs underlying List which should be used to implement a NamedList
* @deprecated Use {@link #NamedList(java.util.Map.Entry[])} for the NamedList instantiation
*/
@Deprecated
public NamedList(List nameValuePairs) {
nvPairs=nameValuePairs;
}
/**
*
* 序列化Map.Entry<String,?> 为一个List.这个List中索引为(0,2,4. ..etc)的是String,奇数元素(1,3,5...etc)为"T"类型.
*
* @return Modified List as per the above description
* @deprecated This a temporary placeholder method until the guts of the class
* are actually replaced by List<String, ?>.
* @see SOLR-912
*/
@Deprecated
private List nameValueMapToList(Map.Entry[] nameValuePairs) {
List result = new ArrayList();
for (Map.Entry ent : nameValuePairs) {
result.add(ent.getKey());
result.add(ent.getValue());
}
return result;
}
/** The total number of name/value pairs */
public int size() {
return nvPairs.size() >> 1;
}
/**
* The name of the pair at the specified List index
*
* @return null if no name exists
*/
public String getName(int idx) {
return (String)nvPairs.get(idx