org.apache.solr.common.util
Class NamedList<T>

java.lang.Object
  extended by org.apache.solr.common.util.NamedList<T>
All Implemented Interfaces:
Serializable, Cloneable, Iterable<Map.Entry<String,T>>
Direct Known Subclasses:
SimpleOrderedMap

public class NamedList<T>
extends Object
implements Cloneable, Serializable, Iterable<Map.Entry<String,T>>

A simple container class for modeling an ordered list of name/value pairs.

Unlike Maps:

A NamedList provides fast access by element number, but not by name.

When a NamedList is serialized, order is considered more important than access by key, so ResponseWriters that output to a format such as JSON will normally choose a data structure that allows order to be easily preserved in various clients (i.e. not a straight map). If access by key is more important for serialization, see SimpleOrderedMap, or simply use a regular Map

See Also:
Serialized Form

Nested Class Summary
static class NamedList.NamedListEntry<T>
          Helper class implementing Map.Entry<String, T> to store the key-value relationship in NamedList (the keys of which are String-s)
 
Field Summary
protected  List<Object> nvPairs
           
 
Constructor Summary
NamedList()
          Creates an empty instance
NamedList(List<Object> nameValuePairs)
          Deprecated. Use NamedList(java.util.Map.Entry[]) for the NamedList instantiation
NamedList(Map.Entry<String,? extends T>[] nameValuePairs)
          Creates a NamedList instance containing the "name,value" pairs contained in the Entry[].
 
Method Summary
 void add(String name, T val)
          Adds a name/value pair to the end of the list.
 boolean addAll(Map<String,T> args)
          Iterates over the Map and sequentially adds it's key/value pairs
 boolean addAll(NamedList<T> nl)
          Appends the elements of the given NamedList to this one.
 void clear()
           
 NamedList<T> clone()
          Makes a shallow copy of the named list.
 boolean equals(Object obj)
           
 Object findRecursive(String... args)
          Recursively parses the NamedList structure to arrive at a specific element.
 T get(String name)
          Gets the value for the first instance of the specified name found.
 T get(String name, int start)
          Gets the value for the first instance of the specified name found starting at the specified index.
 List<T> getAll(String name)
          Gets the values for the the specified name
 String getName(int idx)
          The name of the pair at the specified List index
 T getVal(int idx)
          The value of the pair at the specified List index
 int hashCode()
           
 int indexOf(String name, int start)
          Scans the list sequentially beginning at the specified index and returns the index of the first pair with the specified name.
 Iterator<Map.Entry<String,T>> iterator()
          Support the Iterable interface
 T remove(int idx)
          Removes the name/value pair at the specified index.
 T remove(String name)
          NOTE: this runs in linear time (it scans starting at the beginning of the list until it finds the first pair with the specified name).
 void setName(int idx, String name)
          Modifies the name of the pair at the specified index.
 T setVal(int idx, T val)
          Modifies the value of the pair at the specified index.
 int size()
          The total number of name/value pairs
 String toString()
           
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

nvPairs

protected final List<Object> nvPairs
Constructor Detail

NamedList

public NamedList()
Creates an empty instance


NamedList

public NamedList(Map.Entry<String,? extends T>[] nameValuePairs)
Creates a NamedList instance containing the "name,value" pairs contained in the 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 add(String, Object) or remove(String).

Parameters:
nameValuePairs - the name value pairs

NamedList

@Deprecated
public NamedList(List<Object> nameValuePairs)
Deprecated. Use NamedList(java.util.Map.Entry[]) for the NamedList instantiation

Creates an instance backed by an explicitly specified list of pairwise names/values.

When using this constructor, runtime type safety is only guaranteed if all even numbered elements of the input list are of type "T".

Parameters:
nameValuePairs - underlying List which should be used to implement a NamedList
Method Detail

size

public int size()
The total number of name/value pairs


getName

public String getName(int idx)
The name of the pair at the specified List index

Returns:
null if no name exists

getVal

public T getVal(int idx)
The value of the pair at the specified List index

Returns:
may be null

add

public void add(String name,
                T val)
Adds a name/value pair to the end of the list.


setName

public void setName(int idx,
                    String name)
Modifies the name of the pair at the specified index.


setVal

public T setVal(int idx,
                T val)
Modifies the value of the pair at the specified index.

Returns:
the value that used to be at index

remove

public T remove(int idx)
Removes the name/value pair at the specified index.

Returns:
the value at the index removed

indexOf

public int indexOf(String name,
                   int start)
Scans the list sequentially beginning at the specified index and returns the index of the first pair with the specified name.

Parameters:
name - name to look for, may be null
start - index to begin searching from
Returns:
The index of the first matching pair, -1 if no match

get

public T get(String name)
Gets the value for the first instance of the specified name found.

NOTE: this runs in linear time (it scans starting at the beginning of the list until it finds the first pair with the specified name).

Returns:
null if not found or if the value stored was null.
See Also:
indexOf(java.lang.String, int), get(String,int)

get

public T get(String name,
             int start)
Gets the value for the first instance of the specified name found starting at the specified index.

NOTE: this runs in linear time (it scans starting at the specified position until it finds the first pair with the specified name).

Returns:
null if not found or if the value stored was null.
See Also:
indexOf(java.lang.String, int)

getAll

public List<T> getAll(String name)
Gets the values for the the specified name

Parameters:
name - Name
Returns:
List of values

findRecursive

public Object findRecursive(String... args)
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 objects themselves. A null value is returned if the indicated hierarchy doesn't exist, but NamedList allows null values so that could be the actual value at the end of the path. This method is particularly useful for parsing the response from Solr's /admin/mbeans handler, but it also works for any complex structure. Explicitly casting the return value is recommended. An even safer option is to accept the return value as an object and then check its type. Usage examples: String coreName = (String) response.findRecursive ("solr-mbeans", "CORE", "core", "stats", "coreName"); long numDoc = (long) response.findRecursive ("solr-mbeans", "CORE", "searcher", "stats", "numDocs");

Parameters:
args - One or more strings specifying the tree to navigate.
Returns:
the last entry in the given path hierarchy, null if not found.

toString

public String toString()
Overrides:
toString in class Object

addAll

public boolean addAll(Map<String,T> args)
Iterates over the Map and sequentially adds it's key/value pairs


addAll

public boolean addAll(NamedList<T> nl)
Appends the elements of the given NamedList to this one.


clone

public NamedList<T> clone()
Makes a shallow copy of the named list.

Overrides:
clone in class Object

iterator

public Iterator<Map.Entry<String,T>> iterator()
Support the Iterable interface

Specified by:
iterator in interface Iterable<Map.Entry<String,T>>

remove

public T remove(String name)
NOTE: this runs in linear time (it scans starting at the beginning of the list until it finds the first pair with the specified name).


clear

public void clear()

hashCode

public int hashCode()
Overrides:
hashCode in class Object

equals

public boolean equals(Object obj)
Overrides:
equals in class Object


Copyright © 2000-2013 Apache Software Foundation. All Rights Reserved.