diff --git a/src/java/org/apache/solr/common/params/ModifiableSolrParams.java b/src/java/org/apache/solr/common/params/ModifiableSolrParams.java new file mode 100644 index 00000000000..0ced1e33847 --- /dev/null +++ b/src/java/org/apache/solr/common/params/ModifiableSolrParams.java @@ -0,0 +1,187 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.solr.common.params; + +import java.io.IOException; +import java.net.URLEncoder; +import java.util.LinkedHashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + + +/** + * This class is similar to MultiMapSolrParams except you can edit the + * parameters after it is initialized. It has helper functions to set/add + * integer and boolean param values. + * + * @author ryan + * @since solr 1.3 + */ +public class ModifiableSolrParams extends SolrParams +{ + private Map vals; + + public ModifiableSolrParams() + { + // LinkedHashMap so params show up in CGI in the same order as they are entered + vals = new LinkedHashMap(); + } + + public ModifiableSolrParams( Map v ) + { + vals = v; + } + + //---------------------------------------------------------------- + //---------------------------------------------------------------- + + /** + * Replace any existing parameter with the given name. if val==null remove key from params completely. + */ + public void set( String name, String ... val ) { + if (val==null || (val.length==1 && val[0]==null)) { + vals.remove(name); + } else { + vals.put( name, val ); + } + } + + public void set( String name, int val ) { + set( name, String.valueOf(val) ); + } + + public void set( String name, boolean val ) { + set( name, String.valueOf(val) ); + } + + /** + * Add the given values to any existing name + */ + public void add( String name, String ... val ) { + String[] old = vals.get( name ); + if( old != null ) { + int i =0; + if( val == null || val.length < 1 ) { + String[] both = new String[old.length+1]; + for( String v : old ) { + both[i++] = v; + } + both[i++] = null; + vals.put( name, both ); + } + else { + String[] both = new String[old.length+val.length]; + for( String v : old ) { + both[i++] = v; + } + for( String v : val ) { + both[i++] = v; + } + vals.put( name, both ); + } + } + else { + vals.put( name, val ); + } + } + + /** + * remove a field at the given name + */ + public String[] remove( String name ) + { + return vals.remove( name ); + } + + /** + * remove the given value for the given name + * + * @return true if the item was removed, false if null or not present + */ + public boolean remove(String name, String value) { + String[] tmp = vals.get(name); + if (tmp==null) return false; + for (int i=0; i 0 ) { + return v[0]; + } + return null; + } + + @Override + public Iterator getParameterNamesIterator() { + return vals.keySet().iterator(); + } + + public Set getParameterNames() { + return vals.keySet(); + } + + @Override + public String[] getParams(String param) { + return vals.get( param ); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(128); + try { + boolean first=true; + + for (Map.Entry entry : vals.entrySet()) { + String key = entry.getKey(); + String[] valarr = entry.getValue(); + for (String val : valarr) { + if (!first) sb.append('&'); + first=false; + sb.append(key); + sb.append('='); + if( val != null ) { + sb.append( URLEncoder.encode( val, "UTF-8" ) ); + } + } + } + } + catch (IOException e) {throw new RuntimeException(e);} // can't happen + + return sb.toString(); + } +}