From 9c8dcba5d9e0378870203273ca1a74b842753e0f Mon Sep 17 00:00:00 2001 From: Ryan McKinley Date: Thu, 14 Jun 2007 16:33:22 +0000 Subject: [PATCH] SOLR-244, adding ModifiableSolrParams required for SOLR-20 (nice for testing) git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@547297 13f79535-47bb-0310-9956-ffa450edef68 --- .../common/params/ModifiableSolrParams.java | 187 ++++++++++++++++++ 1 file changed, 187 insertions(+) create mode 100644 src/java/org/apache/solr/common/params/ModifiableSolrParams.java 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(); + } +}