generics for NamedList: SOLR-107

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@500050 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yonik Seeley 2007-01-25 23:22:52 +00:00
parent 8cf4a87acb
commit dcb98c1eca
2 changed files with 71 additions and 15 deletions

View File

@ -57,6 +57,9 @@ New Features
6. SOLR-117: Limit a field faceting to constraints with a prefix specified 6. SOLR-117: Limit a field faceting to constraints with a prefix specified
by facet.prefix or f.<field>.facet.prefix. (yonik) by facet.prefix or f.<field>.facet.prefix. (yonik)
7. SOLR-107: JAVA API: Change NamedList to use Java5 generics
and implement Iterable<Map.Entry> (Ryan McKinley via yonik)
Changes in runtime behavior Changes in runtime behavior
1. Highlighting using DisMax will only pick up terms from the main 1. Highlighting using DisMax will only pick up terms from the main
user query, not boost or filter queries (klaas). user query, not boost or filter queries (klaas).

View File

@ -20,6 +20,7 @@ package org.apache.solr.util;
import java.util.*; import java.util.*;
import java.io.Serializable; import java.io.Serializable;
/** /**
* A simple container class for modeling an ordered list of name/value pairs. * A simple container class for modeling an ordered list of name/value pairs.
* *
@ -44,7 +45,7 @@ import java.io.Serializable;
* @author yonik * @author yonik
* @version $Id$ * @version $Id$
*/ */
public class NamedList implements Cloneable, Serializable { public class NamedList<T> implements Cloneable, Serializable, Iterable<Map.Entry<String,T>> {
protected final List nvPairs; protected final List nvPairs;
/** Creates an empty instance */ /** Creates an empty instance */
@ -81,14 +82,15 @@ public class NamedList implements Cloneable, Serializable {
* *
* @return may be null * @return may be null
*/ */
public Object getVal(int idx) { @SuppressWarnings("unchecked")
return nvPairs.get((idx << 1) + 1); public T getVal(int idx) {
return (T)nvPairs.get((idx << 1) + 1);
} }
/** /**
* Adds a name/value pair to the end of the list. * Adds a name/value pair to the end of the list.
*/ */
public void add(String name, Object val) { public void add(String name, T val) {
nvPairs.add(name); nvPairs.add(name);
nvPairs.add(val); nvPairs.add(val);
} }
@ -102,9 +104,13 @@ public class NamedList implements Cloneable, Serializable {
/** /**
* Modifies the value of the pair at the specified index. * Modifies the value of the pair at the specified index.
* @return the value that used to be at index
*/ */
public void setVal(int idx, Object val) { public T setVal(int idx, T val) {
nvPairs.set((idx<<1)+1, val); int index = (idx<<1)+1;
T old = (T)nvPairs.get( index );
nvPairs.set(index, val);
return old;
} }
/** /**
@ -136,7 +142,7 @@ public class NamedList implements Cloneable, Serializable {
* @see #indexOf * @see #indexOf
* @see #get(String,int) * @see #get(String,int)
*/ */
public Object get(String name) { public T get(String name) {
return get(name,0); return get(name,0);
} }
@ -147,7 +153,7 @@ public class NamedList implements Cloneable, Serializable {
* @return null if not found or if the value stored was null. * @return null if not found or if the value stored was null.
* @see #indexOf * @see #indexOf
*/ */
public Object get(String name, int start) { public T get(String name, int start) {
int sz = size(); int sz = size();
for (int i=start; i<sz; i++) { for (int i=start; i<sz; i++) {
String n = getName(i); String n = getName(i);
@ -178,18 +184,15 @@ public class NamedList implements Cloneable, Serializable {
/** /**
* Iterates over the Map and sequentially adds it's key/value pairs * Iterates over the Map and sequentially adds it's key/value pairs
*/ */
public boolean addAll(Map args) { public boolean addAll(Map<String,T> args) {
Set eset = args.entrySet(); for( Map.Entry<String, T> entry : args.entrySet() ) {
Iterator iter = eset.iterator(); add( entry.getKey(), entry.getValue() );
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry)iter.next();
add(entry.getKey().toString(), entry.getValue());
} }
return args.size()>0; return args.size()>0;
} }
/** Appends the elements of the given NamedList to this one. */ /** Appends the elements of the given NamedList to this one. */
public boolean addAll(NamedList nl) { public boolean addAll(NamedList<T> nl) {
nvPairs.addAll(nl.nvPairs); nvPairs.addAll(nl.nvPairs);
return nl.size()>0; return nl.size()>0;
} }
@ -203,4 +206,54 @@ public class NamedList implements Cloneable, Serializable {
return new NamedList(newList); return new NamedList(newList);
} }
//----------------------------------------------------------------------------
// Iterable interface
//----------------------------------------------------------------------------
/**
* Support the Iterable interface
*/
public Iterator<Map.Entry<String,T>> iterator() {
final NamedList list = this;
Iterator<Map.Entry<String,T>> iter = new Iterator<Map.Entry<String,T>>() {
int idx = 0;
public boolean hasNext() {
return idx < list.size();
}
public Map.Entry<String,T> next() {
final int index = idx++;
Map.Entry<String,T> nv = new Map.Entry<String,T>() {
public String getKey() {
return list.getName( index );
}
@SuppressWarnings("unchecked")
public T getValue() {
return (T)list.getVal( index );
}
public String toString()
{
return getKey()+"="+getValue();
}
public T setValue(T value) {
return (T) list.setVal(index, value);
}
};
return nv;
}
public void remove() {
throw new UnsupportedOperationException();
}
};
return iter;
}
} }