Coverage Report - org.apache.tapestry.util.QueryParameterMap
 
Classes in this File Line Coverage Branch Coverage Complexity
QueryParameterMap
0%
0/28
0%
0/12
2
 
 1  
 // Copyright 2004, 2005 The Apache Software Foundation
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package org.apache.tapestry.util;
 16  
 
 17  
 import org.apache.hivemind.util.Defense;
 18  
 
 19  
 import java.util.Arrays;
 20  
 import java.util.HashMap;
 21  
 import java.util.Map;
 22  
 import java.util.TreeMap;
 23  
 
 24  
 /**
 25  
  * A wrapper around a Map that stores query parameter values. Map keys are
 26  
  * strings. Map values can be simple strings or array of string (or null).
 27  
  * 
 28  
  * @author Howard M. Lewis Ship
 29  
  * @since 4.0
 30  
  */
 31  
 public class QueryParameterMap
 32  
 {
 33  
     private final Map _parameters;
 34  
 
 35  
     public QueryParameterMap()
 36  
     {
 37  0
         this(new HashMap());
 38  0
     }
 39  
 
 40  
     /**
 41  
      * Constructor around an existing Map whose keys and values are expected to
 42  
      * conform expected use (keys are strings, values are null, string or string
 43  
      * array). The map passed in is retained ( not copied).
 44  
      */
 45  
 
 46  
     public QueryParameterMap(Map parameterMap)
 47  0
     {
 48  0
         Defense.notNull(parameterMap, "parameterMap");
 49  
 
 50  0
         _parameters = parameterMap;
 51  0
     }
 52  
 
 53  
     /**
 54  
      * Replaces the parameter value for the given name wit the new value (which
 55  
      * may be null).
 56  
      */
 57  
 
 58  
     public void setParameterValue(String name, String value)
 59  
     {
 60  0
         Defense.notNull(name, "name");
 61  
 
 62  0
         _parameters.put(name, value);
 63  0
     }
 64  
 
 65  
     /**
 66  
      * Replaces the parameter value for the given name wit the new list of
 67  
      * values (which may be empty or null).
 68  
      */
 69  
 
 70  
     public void setParameterValues(String name, String[] values)
 71  
     {
 72  0
         Defense.notNull(name, "name");
 73  
 
 74  0
         _parameters.put(name, values);
 75  0
     }
 76  
 
 77  
     /**
 78  
      * Gets a query parameter value. If an array of values was stored, this
 79  
      * returns the first value. May return null.
 80  
      */
 81  
 
 82  
     public String getParameterValue(String name)
 83  
     {
 84  0
         Defense.notNull(name, "name");
 85  
 
 86  0
         Object values = _parameters.get(name);
 87  
 
 88  0
         if (values == null || values instanceof String) return (String) values;
 89  
 
 90  0
         String[] array = (String[]) values;
 91  
 
 92  0
         return array[0];
 93  
     }
 94  
 
 95  
     /**
 96  
      * Returns the array of values for the specified parameter. If only a lone
 97  
      * value was stored (via {@link #setParameterValue(String, String)}, then
 98  
      * the value is wrapped as a string array and returned.
 99  
      */
 100  
     public String[] getParameterValues(String name)
 101  
     {
 102  0
         Defense.notNull(name, "name");
 103  
 
 104  0
         Object values = _parameters.get(name);
 105  
 
 106  0
         if (values == null || values instanceof String[])
 107  0
             return (String[]) values;
 108  
 
 109  0
         String loneValue = (String) values;
 110  
 
 111  0
         return new String[] { loneValue };
 112  
     }
 113  
 
 114  
     /**
 115  
      * Returns the names of all parameters, sorted alphabetically.
 116  
      */
 117  
     public String[] getParameterNames()
 118  
     {
 119  0
         int count = _parameters.size();
 120  
 
 121  0
         String[] result = (String[]) _parameters.keySet().toArray(new String[count]);
 122  
 
 123  0
         if (!TreeMap.class.isInstance(_parameters))
 124  0
             Arrays.sort(result);
 125  
 
 126  0
         return result;
 127  
     }
 128  
 }