SOLR-228 -- adding missing functions to SolrParams

getFieldFloat(String field, String param, float def)
 getFieldParam(String field, String param, String def)
 

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@535914 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Ryan McKinley 2007-05-07 16:03:02 +00:00
parent 17444901b3
commit a461ab4cc5
3 changed files with 46 additions and 1 deletions

View File

@ -106,4 +106,14 @@ public class RequiredSolrParams extends SolrParams {
public boolean getFieldBool(String field, String param, boolean def) {
return params.getFieldBool(field, param, def);
}
@Override
public float getFieldFloat(String field, String param, float def) {
return params.getFieldFloat(field, param, def);
}
@Override
public String getFieldParam(String field, String param, String def) {
return params.getFieldParam(field, param, def);
}
}

View File

@ -196,6 +196,14 @@ public abstract class SolrParams {
return val!=null ? val : get(param);
}
/** returns the String value of the field parameter, "f.field.param", or
* the value for "param" if that is not set. If that is not set, def
*/
public String getFieldParam(String field, String param, String def) {
String val = get(fpname(field,param));
return val!=null ? val : get(param, def);
}
/** returns the String values of the field parameter, "f.field.param", or
* the values for "param" if that is not set.
*/
@ -299,6 +307,29 @@ public abstract class SolrParams {
}
}
/** Returns the float value of the field param. */
public Float getFieldFloat(String field, String param) {
String val = getFieldParam(field, param);
try {
return val==null ? null : Float.valueOf(val);
}
catch( Exception ex ) {
throw new SolrException( 400, ex.getMessage(), ex );
}
}
/** Returns the float value of the field param,
or the value for param, or def if neither is set. */
public float getFieldFloat(String field, String param, float def) {
String val = getFieldParam(field, param);
try {
return val==null ? def : Float.parseFloat(val);
}
catch( Exception ex ) {
throw new SolrException( 400, ex.getMessage(), ex );
}
}
/** how to transform a String into a boolean... more flexible than
* Boolean.parseBoolean() to enable easier integration with html forms.
*/
@ -368,3 +399,4 @@ public abstract class SolrParams {

View File

@ -82,7 +82,8 @@ public class SolrParamTest extends TestCase
assertEquals( pstr , params.getFieldParam( "fl", "str" ) );
assertEquals( pbool , params.getFieldBool( "fl", "bool" ) );
assertEquals( pint , params.getFieldInt( "fl", "int" ) );
assertEquals( pfloat , params.getFieldFloat( "fl", "float" ) );
// Test field defaulting (fall through to non-field-specific value)
assertEquals( pint , params.getFieldInt( "fff", "int" ) );
@ -112,6 +113,8 @@ public class SolrParamTest extends TestCase
assertEquals( pfloat.floatValue() , params.getFloat( "xxx", pfloat ) );
assertEquals( pbool.booleanValue() , params.getFieldBool( "xxx", "bool", pbool ) );
assertEquals( pint.intValue() , params.getFieldInt( "xxx", "int", pint ) );
assertEquals( pfloat.floatValue() , params.getFieldFloat("xxx", "float", pfloat ) );
assertEquals( pstr , params.getFieldParam("xxx", "str", pstr ) );
// Required params testing uses decorator
final SolrParams required = params.required();