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

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

public class NamedList<T>
extends java.lang.Object
implements java.lang.Cloneable, java.io.Serializable, java.lang.Iterable<java.util.Map.Entry<java.lang.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, see SimpleOrderedMap, or simply use a regular Map

Version:
$Id: NamedList.java 501512 2007-01-30 18:36:32Z yonik $
Author:
yonik
See Also:
Serialized Form

Field Summary
protected  java.util.List nvPairs
           
 
Constructor Summary
NamedList()
          Creates an empty instance
NamedList(java.util.List nameValuePairs)
          Creates an instance backed by an explicitly specified list of pairwise names/values.
 
Method Summary
 void add(java.lang.String name, T val)
          Adds a name/value pair to the end of the list.
 boolean addAll(java.util.Map<java.lang.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.
 NamedList<T> clone()
          Makes a shallow copy of the named list.
 T get(java.lang.String name)
          Gets the value for the first instance of the specified name found.
 T get(java.lang.String name, int start)
          Gets the value for the first instance of the specified name found starting at the specified index.
 java.lang.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 indexOf(java.lang.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.
 java.util.Iterator<java.util.Map.Entry<java.lang.String,T>> iterator()
          Support the Iterable interface
 void setName(int idx, java.lang.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
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

nvPairs

protected final java.util.List nvPairs
Constructor Detail

NamedList

public NamedList()
Creates an empty instance


NamedList

public NamedList(java.util.List nameValuePairs)
Creates an instance backed by an explicitly specified list of pairwise names/values.

Parameters:
nameValuePairs - underlying List which should be used to implement a NamedList; modifying this List will affect the NamedList.
Method Detail

size

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


getName

public java.lang.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(java.lang.String name,
                T val)
Adds a name/value pair to the end of the list.


setName

public void setName(int idx,
                    java.lang.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

indexOf

public int indexOf(java.lang.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(java.lang.String name)
Gets the value for the first instance of the specified name found.

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(java.lang.String name,
             int start)
Gets the value for the first instance of the specified name found starting at the specified index.

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

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

addAll

public boolean addAll(java.util.Map<java.lang.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 java.lang.Object

iterator

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

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


Copyright © 2006 - 2008 The Apache Software Foundation