/src/common/org/apache/solr/common/params/SolrParams.java

https://github.com/stewi2/solr · Java · 270 lines · 166 code · 40 blank · 64 comment · 7 complexity · a124e18c83e78fa71ce43d961adca60b MD5 · raw file

  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.solr.common.params;
  18. import java.io.Serializable;
  19. import java.util.HashMap;
  20. import java.util.Iterator;
  21. import java.util.Map;
  22. import org.apache.solr.common.SolrException;
  23. import org.apache.solr.common.util.NamedList;
  24. import org.apache.solr.common.util.SimpleOrderedMap;
  25. import org.apache.solr.common.util.StrUtils;
  26. /** SolrParams hold request parameters.
  27. *
  28. * @version $Id$
  29. */
  30. public abstract class SolrParams implements Serializable {
  31. /** returns the String value of a param, or null if not set */
  32. public abstract String get(String param);
  33. /** returns an array of the String values of a param, or null if none */
  34. public abstract String[] getParams(String param);
  35. /** returns an Iterator over the parameter names */
  36. public abstract Iterator<String> getParameterNamesIterator();
  37. /** returns the value of the param, or def if not set */
  38. public String get(String param, String def) {
  39. String val = get(param);
  40. return val==null ? def : val;
  41. }
  42. /** returns a RequiredSolrParams wrapping this */
  43. public RequiredSolrParams required()
  44. {
  45. // TODO? should we want to stash a reference?
  46. return new RequiredSolrParams(this);
  47. }
  48. protected String fpname(String field, String param) {
  49. return "f."+field+'.'+param;
  50. }
  51. /** returns the String value of the field parameter, "f.field.param", or
  52. * the value for "param" if that is not set.
  53. */
  54. public String getFieldParam(String field, String param) {
  55. String val = get(fpname(field,param));
  56. return val!=null ? val : get(param);
  57. }
  58. /** returns the String value of the field parameter, "f.field.param", or
  59. * the value for "param" if that is not set. If that is not set, def
  60. */
  61. public String getFieldParam(String field, String param, String def) {
  62. String val = get(fpname(field,param));
  63. return val!=null ? val : get(param, def);
  64. }
  65. /** returns the String values of the field parameter, "f.field.param", or
  66. * the values for "param" if that is not set.
  67. */
  68. public String[] getFieldParams(String field, String param) {
  69. String[] val = getParams(fpname(field,param));
  70. return val!=null ? val : getParams(param);
  71. }
  72. /** Returns the Boolean value of the param, or null if not set */
  73. public Boolean getBool(String param) {
  74. String val = get(param);
  75. return val==null ? null : StrUtils.parseBool(val);
  76. }
  77. /** Returns the boolean value of the param, or def if not set */
  78. public boolean getBool(String param, boolean def) {
  79. String val = get(param);
  80. return val==null ? def : StrUtils.parseBool(val);
  81. }
  82. /** Returns the Boolean value of the field param,
  83. or the value for param, or null if neither is set. */
  84. public Boolean getFieldBool(String field, String param) {
  85. String val = getFieldParam(field, param);
  86. return val==null ? null : StrUtils.parseBool(val);
  87. }
  88. /** Returns the boolean value of the field param,
  89. or the value for param, or def if neither is set. */
  90. public boolean getFieldBool(String field, String param, boolean def) {
  91. String val = getFieldParam(field, param);
  92. return val==null ? def : StrUtils.parseBool(val);
  93. }
  94. /** Returns the Integer value of the param, or null if not set */
  95. public Integer getInt(String param) {
  96. String val = get(param);
  97. try {
  98. return val==null ? null : Integer.valueOf(val);
  99. }
  100. catch( Exception ex ) {
  101. throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, ex.getMessage(), ex );
  102. }
  103. }
  104. /** Returns the int value of the param, or def if not set */
  105. public int getInt(String param, int def) {
  106. String val = get(param);
  107. try {
  108. return val==null ? def : Integer.parseInt(val);
  109. }
  110. catch( Exception ex ) {
  111. throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, ex.getMessage(), ex );
  112. }
  113. }
  114. /**
  115. * @return The int value of the field param, or the value for param
  116. * or <code>null</code> if neither is set.
  117. **/
  118. public Integer getFieldInt(String field, String param) {
  119. String val = getFieldParam(field, param);
  120. try {
  121. return val==null ? null : Integer.valueOf(val);
  122. }
  123. catch( Exception ex ) {
  124. throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, ex.getMessage(), ex );
  125. }
  126. }
  127. /** Returns the int value of the field param,
  128. or the value for param, or def if neither is set. */
  129. public int getFieldInt(String field, String param, int def) {
  130. String val = getFieldParam(field, param);
  131. try {
  132. return val==null ? def : Integer.parseInt(val);
  133. }
  134. catch( Exception ex ) {
  135. throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, ex.getMessage(), ex );
  136. }
  137. }
  138. /** Returns the Float value of the param, or null if not set */
  139. public Float getFloat(String param) {
  140. String val = get(param);
  141. try {
  142. return val==null ? null : Float.valueOf(val);
  143. }
  144. catch( Exception ex ) {
  145. throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, ex.getMessage(), ex );
  146. }
  147. }
  148. /** Returns the float value of the param, or def if not set */
  149. public float getFloat(String param, float def) {
  150. String val = get(param);
  151. try {
  152. return val==null ? def : Float.parseFloat(val);
  153. }
  154. catch( Exception ex ) {
  155. throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, ex.getMessage(), ex );
  156. }
  157. }
  158. /** Returns the float value of the field param. */
  159. public Float getFieldFloat(String field, String param) {
  160. String val = getFieldParam(field, param);
  161. try {
  162. return val==null ? null : Float.valueOf(val);
  163. }
  164. catch( Exception ex ) {
  165. throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, ex.getMessage(), ex );
  166. }
  167. }
  168. /** Returns the float value of the field param,
  169. or the value for param, or def if neither is set. */
  170. public float getFieldFloat(String field, String param, float def) {
  171. String val = getFieldParam(field, param);
  172. try {
  173. return val==null ? def : Float.parseFloat(val);
  174. }
  175. catch( Exception ex ) {
  176. throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, ex.getMessage(), ex );
  177. }
  178. }
  179. /** how to transform a String into a boolean... more flexible than
  180. * Boolean.parseBoolean() to enable easier integration with html forms.
  181. * @deprecated Use org.apache.solr.common.util.StrUtils.parseBool
  182. */
  183. @Deprecated
  184. protected boolean parseBool(String s) {
  185. return StrUtils.parseBool(s);
  186. }
  187. /** Create a Map<String,String> from a NamedList given no keys are repeated */
  188. public static Map<String,String> toMap(NamedList params) {
  189. HashMap<String,String> map = new HashMap<String,String>();
  190. for (int i=0; i<params.size(); i++) {
  191. map.put(params.getName(i), params.getVal(i).toString());
  192. }
  193. return map;
  194. }
  195. /** Create a Map<String,String[]> from a NamedList */
  196. public static Map<String,String[]> toMultiMap(NamedList params) {
  197. HashMap<String,String[]> map = new HashMap<String,String[]>();
  198. for (int i=0; i<params.size(); i++) {
  199. String name = params.getName(i);
  200. String val = params.getVal(i).toString();
  201. MultiMapSolrParams.addParam(name,val,map);
  202. }
  203. return map;
  204. }
  205. /** Create SolrParams from NamedList. */
  206. public static SolrParams toSolrParams(NamedList params) {
  207. // if no keys are repeated use the faster MapSolrParams
  208. HashMap<String,String> map = new HashMap<String,String>();
  209. for (int i=0; i<params.size(); i++) {
  210. String prev = map.put(params.getName(i), params.getVal(i).toString());
  211. if (prev!=null) return new MultiMapSolrParams(toMultiMap(params));
  212. }
  213. return new MapSolrParams(map);
  214. }
  215. /** Convert this to a NamedList */
  216. public NamedList<Object> toNamedList() {
  217. final SimpleOrderedMap<Object> result = new SimpleOrderedMap<Object>();
  218. for(Iterator<String> it=getParameterNamesIterator(); it.hasNext(); ) {
  219. final String name = it.next();
  220. final String [] values = getParams(name);
  221. if(values.length==1) {
  222. result.add(name,values[0]);
  223. } else {
  224. // currently no reason not to use the same array
  225. result.add(name,values);
  226. }
  227. }
  228. return result;
  229. }
  230. }