SOLR-967: New type-safe constructor for NamedList

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@746031 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Chris M. Hostetter 2009-02-19 22:20:32 +00:00
parent b3b94be1ac
commit f11377a06c
8 changed files with 122 additions and 12 deletions

View File

@ -312,8 +312,10 @@ Other Changes
18. SOLR-1022: Better "ignored" field in example schema.xml (Peter Wolanin via hossman)
Build
19. SOLR-967: New type-safe constructor for NamedList (Kay Kay via hossman)
Build
----------------------
1. SOLR-776: Added in ability to sign artifacts via Ant for releases (gsingers)

View File

@ -57,16 +57,62 @@ public class NamedList<T> implements Cloneable, Serializable, Iterable<Map.Entry
nvPairs = new ArrayList();
}
/**
* Creates a NamedList instance containing the "name,value" pairs contained in the
* Entry[].
*
* <p>
* Modifying the contents of the Entry[] after calling this constructor may change
* the NamedList (in future versions of Solr), but this is not garunteed and should
* not be relied upon. To modify the NamedList, refer to {@link #add(String, Object)}
* or {@link #remove(String)}.
* </p>
*
* @param nameValuePairs the name value pairs
*/
public NamedList(Map.Entry<String, ? extends T>[] nameValuePairs) {
nvPairs = nameValueMapToList(nameValuePairs);
}
/**
* Creates an instance backed by an explicitly specified list of
* pairwise names/values.
*
* @param nameValuePairs underlying List which should be used to implement a NamedList; modifying this List will affect the NamedList.
* <p>
* When using this constructor, runtime typesafety is only garunteed if the all
* even numbered elements of the input list are of type "T".
* </p>
*
* @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;
}
/**
* Method to serialize Map.Entry&lt;String, ?&gt; to a List in which the even
* indexed elements (0,2,4. ..etc) are Strings and odd elements (1,3,5,) are of
* the type "T".
*
* @param nameValuePairs
* @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&lt;String, ?&gt;.
* @see https://issues.apache.org/jira/browse/SOLR-912
*/
@Deprecated
private List nameValueMapToList(Map.Entry<String, ? extends T>[] nameValuePairs) {
List result = new ArrayList();
for (Map.Entry<String, ?> 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;
@ -212,6 +258,43 @@ public class NamedList<T> implements Cloneable, Serializable, Iterable<Map.Entry
return sb.toString();
}
/**
*
* Helper class implementing Map.Entry<String, T> to store the key-value
* relationship in NamedList (the keys of which are String-s)
*
* @param <T>
*/
public static final class NamedListEntry<T> implements Map.Entry<String, T> {
public NamedListEntry() {
}
public NamedListEntry(String _key, T _value) {
key = _key;
value = _value;
}
public String getKey() {
return key;
}
public T getValue() {
return value;
}
public T setValue(T _value) {
T oldValue = value;
value = _value;
return oldValue;
}
private String key;
private T value;
}
/**
* Iterates over the Map and sequentially adds it's key/value pairs
*/

View File

@ -49,10 +49,15 @@ public class SimpleOrderedMap<T> extends NamedList<T> {
*
* @param nameValuePairs underlying List which should be used to implement a SimpleOrderedMap; modifying this List will affect the SimpleOrderedMap.
*/
@Deprecated
public SimpleOrderedMap(List nameValuePairs) {
super(nameValuePairs);
}
public SimpleOrderedMap(Map.Entry<String, T>[] nameValuePairs) {
super(nameValuePairs);
}
@Override
public SimpleOrderedMap<T> clone() {
ArrayList newList = new ArrayList(nvPairs.size());

View File

@ -108,7 +108,7 @@ public class DebugComponent extends SearchComponent
NamedList info = null;
NamedList explain = new SimpleOrderedMap();
Object[] arr = new Object[rb.resultIds.size() * 2];
Map.Entry<String, Object>[] arr = new NamedList.NamedListEntry[rb.resultIds.size()];
for (ShardRequest sreq : rb.finished) {
if ((sreq.purpose & ShardRequest.PURPOSE_GET_DEBUG) == 0) continue;
@ -123,13 +123,12 @@ public class DebugComponent extends SearchComponent
// TODO: lookup won't work for non-string ids... String vs Float
ShardDoc sdoc = rb.resultIds.get(id);
int idx = sdoc.positionInResponse;
arr[idx<<1] = id;
arr[(idx<<1)+1] = sexplain.getVal(i);
arr[idx] = new NamedList.NamedListEntry<Object>( id, sexplain.getVal(i));
}
}
}
explain = HighlightComponent.removeNulls(new SimpleOrderedMap(Arrays.asList(arr)));
explain = HighlightComponent.removeNulls(new SimpleOrderedMap(arr));
if (info == null) {
info = new SimpleOrderedMap();

View File

@ -30,6 +30,7 @@ import org.apache.solr.request.SolrQueryRequest;
import java.io.IOException;
import java.net.URL;
import java.util.Arrays;
import java.util.Map;
/**
* TODO!
@ -115,7 +116,7 @@ public class HighlightComponent extends SearchComponent
if (rb.doHighlights && rb.stage == ResponseBuilder.STAGE_GET_FIELDS) {
NamedList hlResult = new SimpleOrderedMap();
Object[] arr = new Object[rb.resultIds.size() * 2];
Map.Entry<String, Object>[] arr = new NamedList.NamedListEntry[rb.resultIds.size()];
// TODO: make a generic routine to do automatic merging of id keyed data
for (ShardRequest sreq : rb.finished) {
@ -126,14 +127,13 @@ public class HighlightComponent extends SearchComponent
String id = hl.getName(i);
ShardDoc sdoc = rb.resultIds.get(id);
int idx = sdoc.positionInResponse;
arr[idx<<1] = id;
arr[(idx<<1)+1] = hl.getVal(i);
arr[idx] = new NamedList.NamedListEntry<Object>(id, hl.getVal(i));
}
}
}
// remove nulls in case not all docs were able to be retrieved
rb.rsp.add("highlighting", removeNulls(new SimpleOrderedMap(Arrays.asList(arr))));
rb.rsp.add("highlighting", removeNulls(new SimpleOrderedMap(arr)));
}
}

View File

@ -18,6 +18,7 @@
package org.apache.solr.util;
import java.util.List;
import java.util.Map;
/**
* This class is scheduled for deletion. Please update your code to the moved package.
@ -30,7 +31,12 @@ public class NamedList<T> extends org.apache.solr.common.util.NamedList<T> {
super();
}
@Deprecated
public NamedList(List nameValuePairs) {
super(nameValuePairs);
}
public NamedList(Map.Entry<String, T>[] nameValuePairs) {
super(nameValuePairs);
}
}

View File

@ -17,6 +17,7 @@
package org.apache.solr.util;
import java.util.List;
import java.util.Map;
/**
* This class is scheduled for deletion. Please update your code to the moved package.
@ -30,7 +31,12 @@ public class SimpleOrderedMap<T> extends org.apache.solr.common.util.SimpleOrder
super();
}
@Deprecated
public SimpleOrderedMap(List nameValuePairs) {
super(nameValuePairs);
}
public SimpleOrderedMap(Map.Entry<String, T> [] nameValuePairs) {
super(nameValuePairs);
}
}

View File

@ -33,6 +33,7 @@ import org.apache.solr.request.SolrQueryResponse;
import org.apache.solr.schema.IndexSchema;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import org.apache.solr.common.util.NamedList.NamedListEntry;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
@ -47,8 +48,10 @@ import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -554,8 +557,14 @@ public class TestHarness {
return new LocalSolrQueryRequest(TestHarness.this.getCore(),
q[0], qtype, start, limit, args);
}
return new LocalSolrQueryRequest(TestHarness.this.getCore(),new NamedList(Arrays.asList(q)));
if (q.length%2 != 0) {
throw new RuntimeException("The length of the string array (query arguments) needs to be even");
}
Map.Entry<String, String> [] entries = new NamedListEntry[q.length / 2];
for (int i = 0; i < q.length; i += 2) {
entries[i/2] = new NamedListEntry<String>(q[i], q[i+1]);
}
return new LocalSolrQueryRequest(TestHarness.this.getCore(), new NamedList(entries));
}
}
}