01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: */package org.apache.solr.request;
17:
18: import org.apache.solr.util.StrUtils;
19:
20: import java.util.Iterator;
21: import java.util.Map;
22: import java.io.IOException;
23:
24: /**
25: * @author yonik
26: * @version $Id$
27: */
28: public class MultiMapSolrParams extends SolrParams {
29: protected final Map<String, String[]> map;
30:
31: public static void addParam(String name, String val,
32: Map<String, String[]> map) {
33: String[] arr = map.get(name);
34: if (arr == null) {
35: arr = new String[] { val };
36: } else {
37: String[] newarr = new String[arr.length + 1];
38: System.arraycopy(arr, 0, newarr, 0, arr.length);
39: newarr[arr.length] = val;
40: arr = newarr;
41: }
42: map.put(name, arr);
43: }
44:
45: public MultiMapSolrParams(Map<String, String[]> map) {
46: this .map = map;
47: }
48:
49: public String get(String name) {
50: String[] arr = map.get(name);
51: return arr == null ? null : arr[0];
52: }
53:
54: public String[] getParams(String name) {
55: return map.get(name);
56: }
57:
58: public Iterator<String> getParameterNamesIterator() {
59: return map.keySet().iterator();
60: }
61:
62: public Map<String, String[]> getMap() {
63: return map;
64: }
65:
66: public String toString() {
67: StringBuilder sb = new StringBuilder(128);
68: try {
69: boolean first = true;
70:
71: for (Map.Entry<String, String[]> entry : map.entrySet()) {
72: String key = entry.getKey();
73: String[] valarr = entry.getValue();
74:
75: for (String val : valarr) {
76: if (!first)
77: sb.append('&');
78: first = false;
79: sb.append(key);
80: sb.append('=');
81: StrUtils.partialURLEncodeVal(sb, val == null ? ""
82: : val);
83: }
84: }
85: } catch (IOException e) {
86: throw new RuntimeException(e);
87: } // can't happen
88:
89: return sb.toString();
90: }
91:
92: }
|